Skip to content

Commit eac9c77

Browse files
committedOct 10, 2006
- Added UniqueResult<T> to ICriteria
- Added UniqueResult<T> to IQuery - Ported DetachedCriteria from Hibernate 3 SVN: trunk@2338
1 parent 942bfc9 commit eac9c77

File tree

8 files changed

+205
-1
lines changed

8 files changed

+205
-1
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Collections;
2+
using NHibernate.DomainModel;
3+
using NHibernate.Expression;
4+
using NExpression = NHibernate.Expression.Expression;
5+
using NUnit.Framework;
6+
7+
namespace NHibernate.Test.ExpressionTest
8+
{
9+
[TestFixture]
10+
public class DetachedCriteriaFixture : TestCase
11+
{
12+
13+
protected override IList Mappings
14+
{
15+
get
16+
{
17+
return new string[] { "Componentizable.hbm.xml" };
18+
}
19+
}
20+
21+
[Test]
22+
public void CanUseDetachedCriteriaToQuery()
23+
{
24+
using (ISession s = OpenSession())
25+
{
26+
Componentizable master = new Componentizable();
27+
master.NickName = "master";
28+
s.Save(master);
29+
s.Flush();
30+
}
31+
32+
DetachedCriteria detachedCriteria = DetachedCriteria.For(typeof(Componentizable));
33+
detachedCriteria.Add(NExpression.Eq("NickName","master"));
34+
35+
using (ISession s = OpenSession())
36+
{
37+
Componentizable componentizable = (Componentizable)detachedCriteria.GetExecutableCriteria(s).UniqueResult();
38+
Assert.AreEqual("master", componentizable.NickName);
39+
s.Delete(componentizable);
40+
s.Flush();
41+
}
42+
}
43+
}
44+
}

‎src/NHibernate.Test/NHibernate.Test-2.0.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
<Compile Include="ExceptionsTest\PropertyAccessExceptionFixture.cs" />
9191
<Compile Include="ExpressionTest\BaseExpressionFixture.cs" />
9292
<Compile Include="ExpressionTest\BetweenExpressionFixture.cs" />
93+
<Compile Include="ExpressionTest\DetachedCriteriaFixture.cs" />
9394
<Compile Include="ExpressionTest\InExpressionFixture.cs" />
9495
<Compile Include="ExpressionTest\InsensitiveLikeExpressionFixture.cs" />
9596
<Compile Include="ExpressionTest\JunctionFixture.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using NHibernate.Impl;
5+
using NHibernate.Transform;
6+
7+
namespace NHibernate.Expression
8+
{
9+
/// <summary>
10+
/// Some applications need to create criteria queries in "detached
11+
/// mode", where the Hibernate session is not available. This class
12+
/// may be instantiated anywhere, and then a <c>ICriteria</c>
13+
/// may be obtained by passing a session to
14+
/// <c>GetExecutableCriteria()</c>. All methods have the
15+
/// same semantics and behavior as the corresponding methods of the
16+
/// <c>ICriteria</c> interface.
17+
/// </summary>
18+
public class DetachedCriteria
19+
{
20+
private CriteriaImpl impl;
21+
private ICriteria criteria;
22+
23+
protected DetachedCriteria(System.Type entityType)
24+
{
25+
impl = new CriteriaImpl(entityType, null);
26+
criteria = impl;
27+
}
28+
29+
protected DetachedCriteria(System.Type entityType, String alias)
30+
{
31+
impl = new CriteriaImpl(entityType, alias, null);
32+
criteria = impl;
33+
}
34+
35+
protected DetachedCriteria(CriteriaImpl impl, ICriteria criteria)
36+
{
37+
this.impl = impl;
38+
this.criteria = criteria;
39+
}
40+
41+
/// <summary>
42+
/// Get an executable instance of <c>Criteria</c>,
43+
/// to actually run the query.</summary>
44+
public ICriteria GetExecutableCriteria(ISession session)
45+
{
46+
impl.Session = (SessionImpl) session;
47+
return impl;
48+
}
49+
50+
public static DetachedCriteria For(System.Type entityType)
51+
{
52+
return new DetachedCriteria(entityType);
53+
}
54+
55+
#if NET_2_0
56+
public static DetachedCriteria For<T>()
57+
{
58+
return new DetachedCriteria(typeof (T));
59+
}
60+
#endif
61+
62+
public static DetachedCriteria For(System.Type entityType, String alias)
63+
{
64+
return new DetachedCriteria(entityType, alias);
65+
}
66+
67+
68+
public DetachedCriteria Add(ICriterion criterion)
69+
{
70+
criteria.Add(criterion);
71+
return this;
72+
}
73+
74+
public DetachedCriteria AddOrder(Order order)
75+
{
76+
criteria.AddOrder(order);
77+
return this;
78+
}
79+
80+
public DetachedCriteria CreateAlias(String associationPath, String alias)
81+
{
82+
criteria.CreateAlias(associationPath, alias);
83+
return this;
84+
}
85+
86+
public DetachedCriteria createCriteria(String associationPath, String alias)
87+
{
88+
return new DetachedCriteria(impl, criteria.CreateCriteria(associationPath));
89+
}
90+
91+
public DetachedCriteria createCriteria(String associationPath)
92+
{
93+
return new DetachedCriteria(impl, criteria.CreateCriteria(associationPath));
94+
}
95+
96+
public String Alias
97+
{
98+
get { return criteria.Alias; }
99+
}
100+
101+
public DetachedCriteria SetFetchMode(String associationPath, FetchMode mode)
102+
{
103+
criteria.SetFetchMode(associationPath, mode);
104+
return this;
105+
}
106+
107+
public DetachedCriteria SetProjection(IProjection projection)
108+
{
109+
criteria.SetProjection(projection);
110+
return this;
111+
}
112+
113+
public DetachedCriteria SetResultTransformer(IResultTransformer resultTransformer)
114+
{
115+
criteria.SetResultTransformer(resultTransformer);
116+
return this;
117+
}
118+
119+
public override String ToString()
120+
{
121+
return "DetachableCriteria(" + criteria.ToString() + ')';
122+
}
123+
}
124+
}

‎src/NHibernate/ICriteria.cs

+5
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ public interface ICriteria
103103
/// Strongly-typed version of <see cref="List()" />.
104104
/// </summary>
105105
IList<T> List<T>();
106+
107+
/// <summary>
108+
/// Strongly-typed version of <see cref="UniqueResult()" />.
109+
/// </summary>
110+
T UniqueResult<T>();
106111
#endif
107112

108113
/// <summary>

‎src/NHibernate/IQuery.cs

+7
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ public interface IQuery
130130
/// </exception>
131131
object UniqueResult();
132132

133+
134+
#if NET_2_0
135+
/// <summary>
136+
/// Strongly-typed version of <see cref="UniqueResult()"/>.
137+
/// </summary>
138+
T UniqueResult<T>();
139+
#endif
133140
/// <summary>
134141
/// Set the maximum number of rows to retrieve.
135142
/// </summary>

‎src/NHibernate/Impl/AbstractQueryImpl.cs

+5
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,11 @@ internal ISessionImplementor Session
597597
get { return session; }
598598
}
599599

600+
public T UniqueResult<T>()
601+
{
602+
return (T) UniqueResult();
603+
}
604+
600605
public object UniqueResult()
601606
{
602607
return UniqueElement( List() );

‎src/NHibernate/Impl/CriteriaImpl.cs

+17
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ public IList<T> List<T>()
117117
{
118118
return root.List<T>();
119119
}
120+
121+
public T UniqueResult<T>()
122+
{
123+
return (T) UniqueResult();
124+
}
120125
#endif
121126

122127
public object UniqueResult()
@@ -307,6 +312,12 @@ public IList<T> List<T>()
307312
{
308313
return session.Find<T>( this );
309314
}
315+
316+
public T UniqueResult<T>()
317+
{
318+
return (T) UniqueResult();
319+
}
320+
310321
#endif
311322

312323
public IEnumerable IterateExpressionEntries()
@@ -434,6 +445,12 @@ public bool Cacheable
434445
get { return cacheable; }
435446
}
436447

448+
internal SessionImpl Session
449+
{
450+
get { return session; }
451+
set { session = value; }
452+
}
453+
437454
public string CacheRegion
438455
{
439456
get { return cacheRegion; }

‎src/NHibernate/NHibernate-2.0.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@
519519
<Compile Include="Driver\ISqlParameterFormatter.cs" />
520520
<Compile Include="Driver\SqlStringFormatter.cs" />
521521
<Compile Include="Engine\FilterDefinition.cs" />
522+
<Compile Include="Expression\DetachedCriteria.cs" />
522523
<Compile Include="Engine\JoinSequence.cs" />
523524
<Compile Include="Hql\CollectionSubqueryFactory.cs" />
524525
<Compile Include="Expression\IsEmptyExpression.cs" />
@@ -722,4 +723,4 @@
722723
<Target Name="AfterBuild">
723724
</Target>
724725
-->
725-
</Project>
726+
</Project>

0 commit comments

Comments
 (0)
Please sign in to comment.