|
From: <fab...@us...> - 2010-07-29 13:11:06
|
Revision: 5081
http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5081&view=rev
Author: fabiomaulo
Date: 2010-07-29 13:10:59 +0000 (Thu, 29 Jul 2010)
Log Message:
-----------
Minor (example of custom LINQ extensions)
Modified Paths:
--------------
trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj
Added Paths:
-----------
trunk/nhibernate/src/NHibernate.Test/Linq/CustomExtensionsExample.cs
Added: trunk/nhibernate/src/NHibernate.Test/Linq/CustomExtensionsExample.cs
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/Linq/CustomExtensionsExample.cs (rev 0)
+++ trunk/nhibernate/src/NHibernate.Test/Linq/CustomExtensionsExample.cs 2010-07-29 13:10:59 UTC (rev 5081)
@@ -0,0 +1,67 @@
+using System.Collections.ObjectModel;
+using System.Linq;
+using System.Linq.Expressions;
+using System.Reflection;
+using System.Text.RegularExpressions;
+using NHibernate.Cfg.Loquacious;
+using NHibernate.Hql.Ast;
+using NHibernate.Linq;
+using NHibernate.Linq.Functions;
+using NHibernate.Linq.Visitors;
+using NUnit.Framework;
+using SharpTestsEx;
+
+namespace NHibernate.Test.Linq
+{
+ public static class MyLinqExtensions
+ {
+ public static bool IsLike(this string source, string pattern)
+ {
+ pattern = Regex.Escape(pattern);
+ pattern = pattern.Replace("%", ".*?").Replace("_", ".");
+ pattern = pattern.Replace(@"\[", "[").Replace(@"\]","]").Replace(@"\^", "^");
+
+ return Regex.IsMatch(source, pattern);
+ }
+ }
+
+ public class MyLinqToHqlGeneratorsRegistry: DefaultLinqToHqlGeneratorsRegistry
+ {
+ public MyLinqToHqlGeneratorsRegistry():base()
+ {
+ RegisterGenerator(ReflectionHelper.GetMethod(() => MyLinqExtensions.IsLike(null, null)),
+ new IsLikeGenerator());
+ }
+ }
+
+ public class IsLikeGenerator : BaseHqlGeneratorForMethod
+ {
+ public IsLikeGenerator()
+ {
+ SupportedMethods = new[] {ReflectionHelper.GetMethod(() => MyLinqExtensions.IsLike(null, null))};
+ }
+
+ public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject,
+ ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
+ {
+ return treeBuilder.Like(visitor.Visit(arguments[0]).AsExpression(),
+ visitor.Visit(arguments[1]).AsExpression());
+ }
+ }
+
+ public class CustomExtensionsExample : LinqTestCase
+ {
+ protected override void Configure(NHibernate.Cfg.Configuration configuration)
+ {
+ configuration.LinqToHqlGeneratorsRegistry<MyLinqToHqlGeneratorsRegistry>();
+ }
+
+ [Test]
+ public void CanUseMyCustomExtension()
+ {
+ var contacts = (from c in db.Customers where c.ContactName.IsLike("%Thomas%") select c).ToList();
+ contacts.Count.Should().Be.GreaterThan(0);
+ contacts.Select(customer => customer.ContactName).All(c => c.Satisfy(customer => customer.Contains("Thomas")));
+ }
+ }
+}
\ No newline at end of file
Modified: trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2010-07-28 22:34:55 UTC (rev 5080)
+++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2010-07-29 13:10:59 UTC (rev 5081)
@@ -395,6 +395,7 @@
<Compile Include="Linq\BinaryExpressionOrdererTests.cs" />
<Compile Include="Linq\CasingTest.cs" />
<Compile Include="Linq\CollectionAssert.cs" />
+ <Compile Include="Linq\CustomExtensionsExample.cs" />
<Compile Include="Linq\DateTimeTests.cs" />
<Compile Include="Linq\DynamicQueryTests.cs" />
<Compile Include="Linq\EagerLoadTests.cs" />
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|