|
From: <Ric...@us...> - 2009-06-29 19:38:36
|
Revision: 4544
http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4544&view=rev
Author: RicBrown
Date: 2009-06-29 19:38:33 +0000 (Mon, 29 Jun 2009)
Log Message:
-----------
Added first cut of DetachedCriteria<T>.
Modified Paths:
--------------
trunk/nhibernate/src/NHibernate/Criterion/DetachedCriteria.cs
trunk/nhibernate/src/NHibernate/NHibernate.csproj
trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj
Added Paths:
-----------
trunk/nhibernate/src/NHibernate/Criterion/DetachedCriteriaOfT.cs
trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/DetachedCriteriaOfTFixture.cs
Modified: trunk/nhibernate/src/NHibernate/Criterion/DetachedCriteria.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Criterion/DetachedCriteria.cs 2009-06-29 18:52:24 UTC (rev 4543)
+++ trunk/nhibernate/src/NHibernate/Criterion/DetachedCriteria.cs 2009-06-29 19:38:33 UTC (rev 4544)
@@ -95,6 +95,11 @@
return new DetachedCriteria(entityName, alias);
}
+ public static DetachedCriteria<T> QueryOver<T>()
+ {
+ return new DetachedCriteria<T>(new DetachedCriteria(typeof(T)));
+ }
+
public DetachedCriteria Add(ICriterion criterion)
{
criteria.Add(criterion);
Added: trunk/nhibernate/src/NHibernate/Criterion/DetachedCriteriaOfT.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Criterion/DetachedCriteriaOfT.cs (rev 0)
+++ trunk/nhibernate/src/NHibernate/Criterion/DetachedCriteriaOfT.cs 2009-06-29 19:38:33 UTC (rev 4544)
@@ -0,0 +1,53 @@
+
+using System;
+using System.Linq.Expressions;
+
+using NHibernate.Impl;
+
+namespace NHibernate.Criterion
+{
+
+ /// <summary>
+ /// Some applications need to create criteria queries in "detached
+ /// mode", where the Hibernate session is not available. This class
+ /// may be instantiated anywhere, and then a <c>ICriteria</c>
+ /// may be obtained by passing a session to
+ /// <c>GetExecutableCriteria()</c>. All methods have the
+ /// same semantics and behavior as the corresponding methods of the
+ /// <c>ICriteria<T></c> interface.
+ /// </summary>
+ [Serializable]
+ public class DetachedCriteria<T>
+ {
+
+ private DetachedCriteria _criteria;
+
+ protected internal DetachedCriteria(DetachedCriteria detachedCriteria)
+ {
+ _criteria = detachedCriteria;
+ }
+
+ public DetachedCriteria<T> And(Expression<Func<T, bool>> expression)
+ {
+ return Add(expression);
+ }
+
+ public DetachedCriteria<T> Where(Expression<Func<T, bool>> expression)
+ {
+ return Add(expression);
+ }
+
+ private DetachedCriteria<T> Add(Expression<Func<T, bool>> expression)
+ {
+ _criteria.Add(ExpressionProcessor.ProcessExpression<T>(expression));
+ return this;
+ }
+
+ public static implicit operator DetachedCriteria(DetachedCriteria<T> detachedCriteria)
+ {
+ return detachedCriteria._criteria;
+ }
+
+ }
+
+}
\ No newline at end of file
Modified: trunk/nhibernate/src/NHibernate/NHibernate.csproj
===================================================================
--- trunk/nhibernate/src/NHibernate/NHibernate.csproj 2009-06-29 18:52:24 UTC (rev 4543)
+++ trunk/nhibernate/src/NHibernate/NHibernate.csproj 2009-06-29 19:38:33 UTC (rev 4544)
@@ -477,6 +477,7 @@
<Compile Include="Cfg\MappingSchema\HbmTimestamp.cs" />
<Compile Include="Cfg\MappingSchema\HbmVersion.cs" />
<Compile Include="Cfg\MappingSchema\IDecoratable.cs" />
+ <Compile Include="Criterion\DetachedCriteriaOfT.cs" />
<Compile Include="Criterion\IPropertyProjection.cs" />
<Compile Include="Dialect\MsSql2008Dialect.cs" />
<Compile Include="Dialect\InformixDialect0940.cs" />
Added: trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/DetachedCriteriaOfTFixture.cs
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/DetachedCriteriaOfTFixture.cs (rev 0)
+++ trunk/nhibernate/src/NHibernate.Test/Criteria/Lambda/DetachedCriteriaOfTFixture.cs 2009-06-29 19:38:33 UTC (rev 4544)
@@ -0,0 +1,48 @@
+using System;
+using System.Collections;
+
+using NUnit.Framework;
+
+using NHibernate.Criterion;
+using NHibernate.Transform;
+using NHibernate.Type;
+using NHibernate.Util;
+
+namespace NHibernate.Test.Criteria.Lambda
+{
+
+ [TestFixture]
+ public class DetachedCriteriaOfTFixture : LambdaFixtureBase
+ {
+
+ [Test]
+ public void SimpleCriterion_NoAlias()
+ {
+ DetachedCriteria expected =
+ DetachedCriteria.For<Person>()
+ .Add(Restrictions.Eq("Name", "test name"));
+
+ DetachedCriteria<Person> actual =
+ DetachedCriteria.QueryOver<Person>()
+ .Where(p => p.Name == "test name");
+
+ AssertCriteriaAreEqual(expected, actual);
+ }
+
+ [Test]
+ public void Where_BehavesTheSameAs_And()
+ {
+ DetachedCriteria<Person> expected =
+ DetachedCriteria.QueryOver<Person>()
+ .And(p => p.Name == "test name");
+
+ DetachedCriteria<Person> actual =
+ DetachedCriteria.QueryOver<Person>()
+ .Where(p => p.Name == "test name");
+
+ AssertCriteriaAreEqual(expected, actual);
+ }
+
+ }
+
+}
\ No newline at end of file
Modified: trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2009-06-29 18:52:24 UTC (rev 4543)
+++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2009-06-29 19:38:33 UTC (rev 4544)
@@ -146,6 +146,7 @@
<Compile Include="Criteria\Enrolment.cs" />
<Compile Include="Criteria\Lambda\CriteriaAssertFixture.cs" />
<Compile Include="Criteria\Lambda\CriteriaOfTFixture.cs" />
+ <Compile Include="Criteria\Lambda\DetachedCriteriaOfTFixture.cs" />
<Compile Include="Criteria\Lambda\IntegrationFixture.cs" />
<Compile Include="Criteria\Lambda\LambdaFixtureBase.cs" />
<Compile Include="Criteria\Lambda\Model.cs" />
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|