Skip to content

Commit 20f8098

Browse files
committed
Merge branch 'jeffreyabecker-usertype-serializable'. Fixes NH-3904.
2 parents 394781b + a96e826 commit 20f8098

File tree

11 files changed

+354
-1
lines changed

11 files changed

+354
-1
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.EntityWithUserTypeCanHaveLinqGenerators
4+
{
5+
[Serializable]
6+
public class BarExample : IExample
7+
{
8+
public string Value { get; set; }
9+
public override string ToString()
10+
{
11+
return string.Format("Bar:{0}", Value);
12+
}
13+
public bool IsEquivalentTo(IExample that)
14+
{
15+
return this.Value == that.Value;
16+
}
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+

2+
namespace NHibernate.Test.NHSpecificTest.EntityWithUserTypeCanHaveLinqGenerators
3+
{
4+
public class EntityWithUserTypeProperty
5+
{
6+
public virtual int Id { get; set; }
7+
public virtual string Name { get; set; }
8+
public virtual IExample Example { get; set; }
9+
}
10+
11+
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using NHibernate.Linq;
2+
using NHibernate.Linq.Functions;
3+
4+
namespace NHibernate.Test.NHSpecificTest.EntityWithUserTypeCanHaveLinqGenerators
5+
{
6+
public class EntityWithUserTypePropertyGeneratorsRegistry : DefaultLinqToHqlGeneratorsRegistry
7+
{
8+
public EntityWithUserTypePropertyGeneratorsRegistry()
9+
{
10+
RegisterGenerator(ReflectionHelper.GetMethod((IExample e) => e.IsEquivalentTo(null)),
11+
new EntityWithUserTypePropertyIsEquivalentGenerator());
12+
}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Collections.ObjectModel;
2+
using System.Linq;
3+
using System.Linq.Expressions;
4+
using System.Reflection;
5+
using NHibernate.Hql.Ast;
6+
using NHibernate.Linq;
7+
using NHibernate.Linq.Functions;
8+
using NHibernate.Linq.Visitors;
9+
10+
namespace NHibernate.Test.NHSpecificTest.EntityWithUserTypeCanHaveLinqGenerators
11+
{
12+
public class EntityWithUserTypePropertyIsEquivalentGenerator : BaseHqlGeneratorForMethod
13+
{
14+
public EntityWithUserTypePropertyIsEquivalentGenerator()
15+
{
16+
SupportedMethods = new[] {ReflectionHelper.GetMethodDefinition((IExample e) => e.IsEquivalentTo(null))};
17+
}
18+
19+
public override HqlTreeNode BuildHql(
20+
MethodInfo method,
21+
Expression targetObject,
22+
ReadOnlyCollection<Expression> arguments,
23+
HqlTreeBuilder treeBuilder,
24+
IHqlExpressionVisitor visitor)
25+
{
26+
var left = treeBuilder.Cast(visitor.Visit(targetObject).AsExpression(), typeof(string));
27+
var right = treeBuilder.Cast(visitor.Visit(arguments.First()).AsExpression(), typeof(string));
28+
29+
var leftSubstring = treeBuilder.MethodCall("substring", left, treeBuilder.Constant(4));
30+
var rightSubstring = treeBuilder.MethodCall("substring", right, treeBuilder.Constant(4));
31+
var equals = treeBuilder.Equality(leftSubstring, rightSubstring);
32+
return equals;
33+
}
34+
}
35+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System;
2+
using System.Data;
3+
using NHibernate.SqlTypes;
4+
using NHibernate.UserTypes;
5+
6+
namespace NHibernate.Test.NHSpecificTest.EntityWithUserTypeCanHaveLinqGenerators
7+
{
8+
public class ExampleUserType : IUserType
9+
{
10+
public new bool Equals(object a, object b)
11+
{
12+
IExample ga = a as IExample;
13+
IExample gb = b as IExample;
14+
if (ga == null && gb == null)
15+
{
16+
return true;
17+
}
18+
if (ga != null && gb != null && (ga.GetType() == gb.GetType()))
19+
{
20+
return ga.Value == gb.Value;
21+
}
22+
return false;
23+
}
24+
25+
public int GetHashCode(object x)
26+
{
27+
return ((IExample)x).Value.GetHashCode();
28+
}
29+
30+
public object DeepCopy(object value)
31+
{
32+
if (value == null) return null;
33+
if (value.GetType() == typeof(BarExample))
34+
{
35+
return new BarExample { Value = ((IExample)value).Value };
36+
}
37+
return new FooExample { Value = ((IExample)value).Value };
38+
}
39+
40+
public object Replace(object original, object target, object owner)
41+
{
42+
return original;
43+
}
44+
45+
public object Assemble(object cached, object owner)
46+
{
47+
return cached;
48+
}
49+
50+
public object Disassemble(object value)
51+
{
52+
return value;
53+
}
54+
55+
public SqlType[] SqlTypes { get { return new SqlType[] { SqlTypeFactory.GetString(255) }; } }
56+
57+
public System.Type ReturnedType { get { return typeof(IExample); } }
58+
public bool IsMutable { get { return true; } }
59+
60+
public void NullSafeSet(IDbCommand cmd, object value, int index)
61+
{
62+
var dataParameter = (IDataParameter)cmd.Parameters[index];
63+
var example = (IExample)value;
64+
dataParameter.DbType = DbType.String;
65+
if (value == null || example.Value == null)
66+
{
67+
dataParameter.Value = DBNull.Value;
68+
}
69+
else
70+
{
71+
dataParameter.Value = example.ToString();
72+
}
73+
}
74+
75+
public object NullSafeGet(IDataReader rs, string[] names, object owner)
76+
{
77+
var index = rs.GetOrdinal(names[0]);
78+
if (rs.IsDBNull(index))
79+
{
80+
return null;
81+
}
82+
var val = rs.GetString(index);
83+
84+
var parts = val.Split(':');
85+
if (parts[0] == "Bar")
86+
{
87+
return new BarExample { Value = parts[1] };
88+
}
89+
return new FooExample { Value = parts[1] };
90+
}
91+
}
92+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
using System.Collections;
2+
using System.Linq;
3+
using NHibernate.Cfg;
4+
using NHibernate.Linq;
5+
using NUnit.Framework;
6+
7+
8+
namespace NHibernate.Test.NHSpecificTest.EntityWithUserTypeCanHaveLinqGenerators
9+
{
10+
11+
[TestFixture]
12+
public class Fixture : TestCase
13+
{
14+
protected override IList Mappings
15+
{
16+
get { return new[] { "NHSpecificTest.EntityWithUserTypeCanHaveLinqGenerators.Mappings.hbm.xml" }; }
17+
}
18+
19+
protected override string MappingsAssembly
20+
{
21+
get { return "NHibernate.Test"; }
22+
}
23+
24+
protected override void Configure(Configuration configuration)
25+
{
26+
base.Configure(configuration);
27+
configuration.LinqToHqlGeneratorsRegistry<EntityWithUserTypePropertyGeneratorsRegistry>();
28+
}
29+
30+
protected override void OnSetUp()
31+
{
32+
using (var session = OpenSession())
33+
using (var transaction = session.BeginTransaction())
34+
{
35+
var e1 = new EntityWithUserTypeProperty
36+
{
37+
Id = 1,
38+
Name = "Bob",
39+
Example = new BarExample
40+
{
41+
Value = "Larry"
42+
}
43+
};
44+
session.Save(e1);
45+
46+
var e2 = new EntityWithUserTypeProperty
47+
{
48+
Id = 2,
49+
Name = "Sally",
50+
Example = new FooExample
51+
{
52+
Value = "Larry"
53+
}
54+
};
55+
session.Save(e2);
56+
57+
session.Flush();
58+
transaction.Commit();
59+
}
60+
}
61+
62+
protected override void OnTearDown()
63+
{
64+
using (var session = OpenSession())
65+
using (var transaction = session.BeginTransaction())
66+
{
67+
session.Delete("from EntityWithUserTypeProperty");
68+
69+
session.Flush();
70+
transaction.Commit();
71+
}
72+
}
73+
74+
75+
[Test]
76+
public void EqualityWorksForUserType()
77+
{
78+
using (var session = OpenSession())
79+
using (session.BeginTransaction())
80+
{
81+
var newItem = new BarExample { Value = "Larry" };
82+
var entities = session.Query<EntityWithUserTypeProperty>()
83+
.Where(x=>x.Example == newItem)
84+
.ToList();
85+
86+
Assert.AreEqual(1, entities.Count);
87+
}
88+
}
89+
90+
[Test]
91+
public void LinqMethodWorksForUserType()
92+
{
93+
using (var session = OpenSession())
94+
using (session.BeginTransaction())
95+
{
96+
var newItem = new BarExample { Value = "Larry" };
97+
var entities = session.Query<EntityWithUserTypeProperty>()
98+
.Where(x => x.Example.IsEquivalentTo(newItem))
99+
.ToList();
100+
101+
Assert.AreEqual(2, entities.Count);
102+
}
103+
}
104+
105+
[Test]
106+
public void CanQueryWithHql()
107+
{
108+
using (var session = OpenSession())
109+
using (session.BeginTransaction())
110+
{
111+
var newItem = new BarExample { Value = "Larry" };
112+
var q = session.CreateQuery("from EntityWithUserTypeProperty e where e.Example = :exampleItem");
113+
q.SetParameter("exampleItem", newItem);
114+
var entities = q.List<EntityWithUserTypeProperty>();
115+
116+
Assert.AreEqual(1, entities.Count);
117+
}
118+
}
119+
}
120+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.EntityWithUserTypeCanHaveLinqGenerators
4+
{
5+
[Serializable]
6+
public class FooExample : IExample
7+
{
8+
public string Value { get; set; }
9+
public bool IsEquivalentTo(IExample that)
10+
{
11+
return this.Value == that.Value;
12+
}
13+
14+
public override string ToString()
15+
{
16+
return string.Format("Foo:{0}", Value);
17+
}
18+
}
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
namespace NHibernate.Test.NHSpecificTest.EntityWithUserTypeCanHaveLinqGenerators
3+
{
4+
public interface IExample
5+
{
6+
string Value { get; set; }
7+
bool IsEquivalentTo(IExample that);
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<hibernate-mapping xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" namespace="NHibernate.Test.NHSpecificTest.EntityWithUserTypeCanHaveLinqGenerators" assembly="NHibernate.Test, Version=4.1.0.1001, Culture=neutral, PublicKeyToken=null" xmlns="urn:nhibernate-mapping-2.2">
3+
<class name="EntityWithUserTypeProperty">
4+
<id name="Id" type="Int32">
5+
<generator class="assigned" />
6+
</id>
7+
<property name="Name" />
8+
<property name="Example" type="NHibernate.Test.NHSpecificTest.EntityWithUserTypeCanHaveLinqGenerators.ExampleUserType, NHibernate.Test" />
9+
</class>
10+
</hibernate-mapping>

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,14 @@
723723
<Compile Include="NHSpecificTest\BagWithLazyExtraAndFilter\Fixture.cs" />
724724
<Compile Include="Linq\ByMethod\DistinctTests.cs" />
725725
<Compile Include="Component\Basic\ComponentWithUniqueConstraintTests.cs" />
726+
<Compile Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\BarExample.cs" />
727+
<Compile Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\EntityWithUserTypeProperty.cs" />
728+
<Compile Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\EntityWithUserTypePropertyIsEquivalentGenerator.cs" />
729+
<Compile Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\EntityWithUserTypePropertyGeneratorsRegistry.cs" />
730+
<Compile Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\ExampleUserType.cs" />
731+
<Compile Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\Fixture.cs" />
732+
<Compile Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\FooExample.cs" />
733+
<Compile Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\IExample.cs" />
726734
<Compile Include="NHSpecificTest\NH3414\Entity.cs" />
727735
<Compile Include="NHSpecificTest\NH3414\FixtureByCode.cs" />
728736
<Compile Include="NHSpecificTest\NH2218\Fixture.cs" />
@@ -3177,6 +3185,7 @@
31773185
</ItemGroup>
31783186
<ItemGroup>
31793187
<EmbeddedResource Include="NHSpecificTest\NH3874\Mappings.hbm.xml" />
3188+
<EmbeddedResource Include="NHSpecificTest\EntityWithUserTypeCanHaveLinqGenerators\Mappings.hbm.xml" />
31803189
<EmbeddedResource Include="NHSpecificTest\NH2218\Mappings.hbm.xml" />
31813190
<EmbeddedResource Include="NHSpecificTest\NH3046\Mappings.hbm.xml" />
31823191
<EmbeddedResource Include="NHSpecificTest\NH3518\Mappings.hbm.xml" />

0 commit comments

Comments
 (0)