Skip to content

Commit 56d130e

Browse files
committed
added specs for user type convention appliance
1 parent ce3722d commit 56d130e

File tree

6 files changed

+210
-78
lines changed

6 files changed

+210
-78
lines changed

src/FluentNHibernate.Specs/Automapping/AutoPersistenceModelSpecs.Conventions.cs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
using System.Xml;
1+
using System;
2+
using System.Data;
3+
using System.Xml;
24
using FluentNHibernate.Automapping;
35
using FluentNHibernate.Conventions;
46
using FluentNHibernate.Conventions.Inspections;
57
using FluentNHibernate.Conventions.Instances;
68
using FluentNHibernate.Specs.Automapping.Fixtures;
79
using FluentNHibernate.Specs.ExternalFixtures;
810
using Machine.Specifications;
11+
using NHibernate.SqlTypes;
12+
using NHibernate.UserTypes;
913

1014
namespace FluentNHibernate.Specs.Automapping
1115
{
@@ -56,4 +60,45 @@ public void Apply(IManyToOneInstance instance)
5660
}
5761
}
5862
}
63+
64+
public class when_the_automapper_maps_with_a_usertype_convention_to_property_of_value_type
65+
{
66+
Establish context = () =>
67+
mapper = AutoMap.Source(new StubTypeSource(typeof(TypeWithNullableUT), typeof(TypeWithValueUT)))
68+
.Conventions.Add<ValueTypeConvention>();
69+
70+
Because of = () =>
71+
{
72+
nullableMapping = mapper.BuildMappingFor<TypeWithNullableUT>().ToXml();
73+
notNullableMapping = mapper.BuildMappingFor<TypeWithValueUT>().ToXml();
74+
};
75+
76+
It shold_apply_convention_to_properties_of_corresponding_nullable_type = () =>
77+
nullableMapping.Element("class/property[@name='Simple']/column").HasAttribute("name", "arbitraryName");
78+
79+
It shold_apply_convention_to_properties_of_corresponding_non_nullable_value_type = () =>
80+
notNullableMapping.Element("class/property[@name='Definite']/column").HasAttribute("name", "arbitraryName");
81+
82+
static XmlDocument nullableMapping;
83+
static XmlDocument notNullableMapping;
84+
static AutoPersistenceModel mapper;
85+
}
86+
87+
public class when_the_automapper_maps_with_a_usertype_convention_to_property_of_reference_type
88+
{
89+
Establish context = () =>
90+
mapper = AutoMap.Source(new StubTypeSource(typeof(NotNullableUT)))
91+
.Conventions.Add<CustomTypeConvention>();
92+
93+
Because of = () =>
94+
xml = mapper.BuildMappingFor<NotNullableUT>().ToXml();
95+
96+
It shold_apply_convention_to_property = () =>
97+
xml.Element("class/property[@name='Complex']/column").HasAttribute("name", "someOtherName");
98+
99+
static XmlDocument xml;
100+
static AutoPersistenceModel mapper;
101+
102+
103+
}
59104
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using System.Linq;
5+
using System.Text;
6+
using FluentNHibernate.Conventions;
7+
using FluentNHibernate.Conventions.Instances;
8+
using NHibernate.SqlTypes;
9+
using NHibernate.UserTypes;
10+
11+
namespace FluentNHibernate.Specs.Automapping.Fixtures
12+
{
13+
public class TypeWithNullableUT
14+
{
15+
public int ID { get; set; }
16+
public UserValueType? Simple { get; set; }
17+
}
18+
19+
public class TypeWithValueUT
20+
{
21+
public int Id { get; set; }
22+
public UserValueType Definite { get; set; }
23+
}
24+
25+
public class NotNullableUT
26+
{
27+
public int ID { get; set; }
28+
public CustomUserType Complex { get; set; }
29+
}
30+
31+
public class ValueTypeConvention : UserTypeConvention<UserValueType>
32+
{
33+
public override void Apply(IPropertyInstance instance)
34+
{
35+
base.Apply(instance);
36+
instance.Column("arbitraryName");
37+
}
38+
}
39+
40+
public class CustomTypeConvention : UserTypeConvention<CustomUserType>
41+
{
42+
public override void Apply(IPropertyInstance instance)
43+
{
44+
base.Apply(instance);
45+
instance.Column("someOtherName");
46+
}
47+
}
48+
49+
public class CustomUserType : IUserType
50+
{
51+
public new bool Equals(object x, object y)
52+
{
53+
return x == y;
54+
}
55+
56+
public int GetHashCode(object x)
57+
{
58+
return x.GetHashCode();
59+
}
60+
61+
public object NullSafeGet(IDataReader rs, string[] names, object owner)
62+
{
63+
return null;
64+
}
65+
66+
public void NullSafeSet(IDbCommand cmd, object value, int index)
67+
{
68+
69+
}
70+
71+
public object DeepCopy(object value)
72+
{
73+
return value;
74+
}
75+
76+
public object Replace(object original, object target, object owner)
77+
{
78+
return original;
79+
}
80+
81+
public object Assemble(object cached, object owner)
82+
{
83+
return cached;
84+
}
85+
86+
public object Disassemble(object value)
87+
{
88+
return value;
89+
}
90+
91+
public SqlType[] SqlTypes
92+
{
93+
get { return new[] { new SqlType(DbType.String) }; }
94+
}
95+
96+
public Type ReturnedType
97+
{
98+
get { return typeof(CustomUserType); }
99+
}
100+
101+
public bool IsMutable
102+
{
103+
get { return true; }
104+
}
105+
}
106+
107+
public struct UserValueType : IUserType
108+
{
109+
public string AddressLine1 { get; set; }
110+
public string AddressLine2 { get; set; }
111+
112+
public bool Equals(object x, object y)
113+
{
114+
throw new NotImplementedException();
115+
}
116+
117+
public int GetHashCode(object x)
118+
{
119+
throw new NotImplementedException();
120+
}
121+
122+
public object NullSafeGet(IDataReader rs, string[] names, object owner)
123+
{
124+
throw new NotImplementedException();
125+
}
126+
127+
public void NullSafeSet(IDbCommand cmd, object value, int index)
128+
{
129+
throw new NotImplementedException();
130+
}
131+
132+
public object DeepCopy(object value)
133+
{
134+
throw new NotImplementedException();
135+
}
136+
137+
public object Replace(object original, object target, object owner)
138+
{
139+
throw new NotImplementedException();
140+
}
141+
142+
public object Assemble(object cached, object owner)
143+
{
144+
throw new NotImplementedException();
145+
}
146+
147+
public object Disassemble(object value)
148+
{
149+
throw new NotImplementedException();
150+
}
151+
152+
public SqlType[] SqlTypes { get; private set; }
153+
public Type ReturnedType { get { return typeof(UserValueType); } }
154+
public bool IsMutable { get; private set; }
155+
}
156+
}

src/FluentNHibernate.Specs/FluentNHibernate.Specs.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
<Compile Include="Automapping\Fixtures\FakeMembers.cs" />
9494
<Compile Include="Automapping\Fixtures\Overrides\EntityTableOverride.cs" />
9595
<Compile Include="Automapping\Fixtures\StubTypeSource.cs" />
96+
<Compile Include="Automapping\Fixtures\UserTypes.cs" />
9697
<Compile Include="Automapping\Fixtures\VersionedEntity.cs" />
9798
<Compile Include="Automapping\PrivateAutomapperSpecs.cs" />
9899
<Compile Include="Automapping\PrivateFieldsSpecs.cs" />

src/FluentNHibernate.Testing/AutoMapping/Apm/AutoPersistenceModelTests.Conventions.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,6 @@ public void TestAutoMapClassAppliesConventions()
8282
.Element("class").HasAttribute("table", "test");
8383
}
8484

85-
[Test]
86-
public void UserTypeConventionAppliesToNullableType()
87-
{
88-
new ValueTypeConvention().Accept(new ConcreteAcceptanceCriteria<IPropertyInspector>());
89-
}
90-
9185
[Test]
9286
public void TypeConventionShouldForcePropertyToBeMapped()
9387
{

src/FluentNHibernate.Testing/AutoMapping/TestFixtures.cs

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -156,61 +156,7 @@ public class ClassWithUserType
156156
public Custom Custom { get; set; }
157157
}
158158

159-
public class ContainingUserValueType
160-
{
161-
public int Id { get; set; }
162-
public UserValueType UserValueType { get; set; }
163-
}
164-
165-
public struct UserValueType: IUserType
166-
{
167-
public string AddressLine1 { get; set; }
168-
public string AddressLine2 { get; set; }
169-
170-
public bool Equals(object x, object y)
171-
{
172-
throw new NotImplementedException();
173-
}
174-
175-
public int GetHashCode(object x)
176-
{
177-
throw new NotImplementedException();
178-
}
179-
180-
public object NullSafeGet(IDataReader rs, string[] names, object owner)
181-
{
182-
throw new NotImplementedException();
183-
}
184-
185-
public void NullSafeSet(IDbCommand cmd, object value, int index)
186-
{
187-
throw new NotImplementedException();
188-
}
189-
190-
public object DeepCopy(object value)
191-
{
192-
throw new NotImplementedException();
193-
}
194-
195-
public object Replace(object original, object target, object owner)
196-
{
197-
throw new NotImplementedException();
198-
}
199-
200-
public object Assemble(object cached, object owner)
201-
{
202-
throw new NotImplementedException();
203-
}
204-
205-
public object Disassemble(object value)
206-
{
207-
throw new NotImplementedException();
208-
}
209-
210-
public SqlType[] SqlTypes { get; private set; }
211-
public Type ReturnedType { get { return typeof(Double); } }
212-
public bool IsMutable { get; private set; }
213-
}
159+
214160

215161
public class ClassWithCompositeUserType
216162
{
@@ -279,16 +225,6 @@ public class DoubleString
279225
public class CustomTypeConvention : UserTypeConvention<CustomUserType>
280226
{}
281227

282-
public class ValueTypeConvention: UserTypeConvention<UserValueType>
283-
{
284-
public override void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
285-
{
286-
base.Accept(criteria);
287-
criteria.Expect(x => x.Length > 15);
288-
}
289-
290-
}
291-
292228
public class CustomCompositeTypeConvention : IUserTypeConvention
293229
{
294230
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)

src/FluentNHibernate/Conventions/UserTypeConvention.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ public virtual void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
1919
{
2020
var userType = Activator.CreateInstance<TUserType>();
2121
var returnedType = userType.ReturnedType;
22-
//if (returnedType.IsValueType)
23-
//{
24-
// var nullableReturnedType = typeof(Nullable<>).MakeGenericType(returnedType);
25-
// criteria.Expect(x => x.Type == returnedType || x.Type == nullableReturnedType);
26-
//}
27-
//else
22+
if (returnedType.IsValueType)
23+
{
24+
var nullableReturnedType = typeof(Nullable<>).MakeGenericType(returnedType);
25+
criteria.Expect(x => x.Type == returnedType || x.Type == nullableReturnedType);
26+
}
27+
else
2828
criteria.Expect(x => x.Type == returnedType);
2929
}
3030

0 commit comments

Comments
 (0)