User: mzywitza
Date: 2010/01/10 11:09 AM

Added:
 /ActiveRecord/trunk/src/Castle.ActiveRecord.Linq/
  QueryProvider.cs

Modified:
 /ActiveRecord/trunk/src/Castle.ActiveRecord.Linq.Tests/
  ActiveRecordLinqTestCase.cs
 /ActiveRecord/trunk/src/Castle.ActiveRecord.Linq/
  ActiveRecordLinq.cs, ActiveRecordLinqBase.cs, 
Castle.ActiveRecord.Linq-vs2008.csproj, LinqQuery.cs

Log:
 Patch from xtoff allowing to use LINQ without SessionScope.

File Changes:

Directory: /ActiveRecord/trunk/src/Castle.ActiveRecord.Linq/
============================================================

File [modified]: ActiveRecordLinq.cs
Delta lines: +29 -27
===================================================================

--- ActiveRecord/trunk/src/Castle.ActiveRecord.Linq/ActiveRecordLinqBase.cs     
2010-01-10 13:38:17 UTC (rev 6619)
+++ ActiveRecord/trunk/src/Castle.ActiveRecord.Linq/ActiveRecordLinqBase.cs     
2010-01-10 18:09:16 UTC (rev 6620)
@@ -14,32 +14,34 @@
 
 namespace Castle.ActiveRecord.Linq
 {
-    using System;
-    using System.Linq;
+       using System.Linq;
 
-    /// <summary>
-    /// A variation of the ActiveRecordBase class which provides the
-    /// ability to use the record type in a linq expression.
-    /// </summary>
-    /// <typeparam name="T">The class which defines the active record 
entity.</typeparam>
-    public class ActiveRecordLinqBase<T> : ActiveRecordBase<T>
-    {
-        /// <summary>
-        /// The static property Table on the active record class is used as a 
Linq collection
-        /// or as the in argument in a Linq expression. 
-        /// 
-        /// Examples include:
-        /// var items = from f in Foo.Table select f;
-        /// var item = Foo.Table.First();
-        /// var items = from f in Foo.Table where f.Name == theName select f;
-        /// var item = Foo.Table.First(f => f.Name == theName);
-        /// </summary>
-        public static IOrderedQueryable<T> Queryable
-        {
-            get
-            {
-                return ExecuteQuery2(new LinqQuery<T>());
-            }
-        }
-    }
+       using NHibernate.Linq;
+
+       /// <summary>
+       /// A variation of the ActiveRecordBase class which provides the
+       /// ability to use the record type in a linq expression.
+       /// </summary>
+       /// <typeparam name="T">The class which defines the active record 
entity.</typeparam>
+       public class ActiveRecordLinqBase<T> : ActiveRecordBase<T>
+       {
+               /// <summary>
+               /// The static property Table on the active record class is 
used as a Linq collection
+               /// or as the in argument in a Linq expression. 
+               /// 
+               /// Examples include:
+               /// var items = from f in Foo.Table select f;
+               /// var item = Foo.Table.First();
+               /// var items = from f in Foo.Table where f.Name == theName 
select f;
+               /// var item = Foo.Table.First(f => f.Name == theName);
+               /// </summary>
+               public static IOrderedQueryable<T> Queryable
+               {
+                       get
+                       {
+                               var options = new QueryOptions();
+                               return new Query<T>(new 
QueryProvider<T>(options), options);
+                       }
+               }
+       }

File [modified]: ActiveRecordLinqBase.cs
Delta lines: +1 -0
===================================================================

--- 
ActiveRecord/trunk/src/Castle.ActiveRecord.Linq/Castle.ActiveRecord.Linq-vs2008.csproj
      2010-01-10 13:38:17 UTC (rev 6619)
+++ 
ActiveRecord/trunk/src/Castle.ActiveRecord.Linq/Castle.ActiveRecord.Linq-vs2008.csproj
      2010-01-10 18:09:16 UTC (rev 6620)
@@ -55,6 +55,7 @@
   <ItemGroup>
     <Compile Include="ActiveRecordLinq.cs" />
     <Compile Include="ActiveRecordLinqBase.cs" />
+    <Compile Include="QueryProvider.cs" />
     <Compile Include="LinqQuery.cs" />
   </ItemGroup>

File [modified]: Castle.ActiveRecord.Linq-vs2008.csproj
Delta lines: +46 -56
===================================================================

--- ActiveRecord/trunk/src/Castle.ActiveRecord.Linq/LinqQuery.cs        
2010-01-10 13:38:17 UTC (rev 6619)
+++ ActiveRecord/trunk/src/Castle.ActiveRecord.Linq/LinqQuery.cs        
2010-01-10 18:09:16 UTC (rev 6620)
@@ -15,68 +15,58 @@
 
 namespace Castle.ActiveRecord.Linq
 {
-    using System;
-    using System.Linq;
-    using NHibernate;
-    using NHibernate.Linq;
+       using System;
+       using System.Collections;
+       using System.Collections.Generic;
+       using System.Linq.Expressions;
 
-    /// <summary>
-    /// Class used internally to glue NHibernate.Linq, NHibernate, and 
ActiveRecord together
-    /// via ExecuteQuery. This appeared to be the cleanest way of connecting 
to the appropriate
-    /// type and scope specific ISession instance. 
-    /// </summary>
-    /// <typeparam name="T">The type of the active record class the Linq 
collection 
-    /// is being bound against.</typeparam>
-    public class LinqQuery<T> : IActiveRecordQuery<IOrderedQueryable<T>>
-    {
-        #region IActiveRecordQuery<IOrderedQueryable<T>> Members
+       using NHibernate;
+       using NHibernate.Linq;
 
-               /// <summary>
-               /// Executes the query using specified session.
-               /// </summary>
-               /// <param name="session">The session.</param>
-               /// <returns></returns>
-        public IOrderedQueryable<T> Execute(ISession session)
-        {
-                       QueryOptions options = new QueryOptions();
+       /// <summary>
+       /// Linq Active Record Query
+       /// </summary>
+       public class LinqQuery<T>:IActiveRecordQuery
+       {
+               private readonly QueryOptions options;
+               private readonly Expression expression;
+               private readonly Type rootType;
 
-            return new Query<T>(new NHibernateQueryProvider(session, options), 
options);
-        }
-
-        #endregion
-
-        #region IActiveRecordQuery Members
-
                /// <summary>
-               /// Enumerates over the result of the query.
-               /// Note: Only use if you expect most of your values to already 
exist in the second level cache!
+               /// 
                /// </summary>
-               /// <param name="session"></param>
-               /// <returns></returns>
-        System.Collections.IEnumerable IActiveRecordQuery.Enumerate(ISession 
session)
-        {
-            return Execute(session);
-        }
+               /// <param name="options"></param>
+               /// <param name="expression"></param>
+               /// <param name="rootType"></param>
+               public LinqQuery(QueryOptions options, Expression 
expression,Type rootType)
+               {
+                       this.options = options;
+                       this.expression = expression;
+                       this.rootType = rootType;
+               }
 
-               /// <summary>
-               /// Executes the specified query and return the results
-               /// </summary>
-               /// <param name="session">The session to execute the query 
in.</param>
-               /// <returns></returns>
-        object IActiveRecordQuery.Execute(ISession session)
-        {
-            return Execute(session);
-        }
+               /// <inheritDoc/>
+               public Type RootType
+               {
+                       get { return rootType; }
+               }
 
-               /// <summary>
-               /// Gets the target type of this query
-               /// </summary>
-               /// <value></value>
-        Type IActiveRecordQuery.RootType
-        {
-            get { return typeof(T); }
-        }
+               /// <inheritDoc/>
+               public List<T> Result { get; private set; }
 
-        #endregion
-    }
+               /// <inheritDoc />
+               public object Execute(ISession session)
+               {
+                       var result = new NHibernateQueryProvider(session, 
options).Execute(expression);
+                       if (result is IEnumerable<T>)
+                               Result = new List<T>(result as IEnumerable<T>);
+                       return result;
+               }
+
+               /// <inheritDoc />
+               public IEnumerable Enumerate(ISession session)
+               {
+                       return (IEnumerable)Execute(session);
+               }
+       }

File [modified]: LinqQuery.cs
Delta lines: +125 -0
===================================================================

--- ActiveRecord/trunk/src/Castle.ActiveRecord.Linq/QueryProvider.cs            
                (rev 0)
+++ ActiveRecord/trunk/src/Castle.ActiveRecord.Linq/QueryProvider.cs    
2010-01-10 18:09:16 UTC (rev 6620)
@@ -0,0 +1,126 @@
+namespace Castle.ActiveRecord.Linq
+{
+       using System;
+       using System.Collections;
+       using System.Collections.Generic;
+       using System.Linq.Expressions;
+
+       using Castle.ActiveRecord.Framework;
+
+       using NHibernate.Linq;
+
+       /// <summary>
+       /// Default Active Record implementation of <see cref="QueryProvider"/>.
+       /// </summary>
+       /// <typeparam name="T"></typeparam>
+       public class QueryProvider<T> : QueryProvider
+       {
+               private readonly QueryOptions options;
+
+               static QueryProvider()
+               {
+                       Experimental = true;
+               }
+
+               /// <summary>
+               /// 
+               /// </summary>
+               /// <param name="options"></param>
+               public QueryProvider(QueryOptions options)
+               {
+                       this.options = options;
+               }
+
+               /// <summary>
+               /// when set to true, enables experimental support for LINQ 
queries without need to use explicit session scope.
+               /// </summary>
+               public static bool Experimental
+               {
+                       get; set;
+               }
+
+               /// <inheritDoc />
+               public override object Execute(Expression expression)
+               {
+                       if(Experimental)
+                       {
+                               var type = expression.Type;
+                               if (type.IsGenericType)
+                               {
+                                       var closingType = GetClosingType(type);
+                                       if(closingType!=null)
+                                       {
+                                               return 
Activator.CreateInstance(typeof(LinqResultWrapper<>).MakeGenericType(closingType),
 options, expression,
+                                                                               
typeof(T));
+                                       }
+                               }
+                       }
+
+                       var linqQuery = new LinqQuery<T>(options, expression, 
typeof(T));
+                       return ActiveRecordBase.ExecuteQuery(linqQuery);
+               }
+
+               private Type GetClosingType(Type type)
+               {
+                       var arguments = type.GetGenericArguments();
+                       if (arguments.Length == 1)
+                       {
+                               return arguments[0];
+                       }
+
+                       foreach (var argument in arguments)
+                       {
+                               var closedEnumerable = 
typeof(IEnumerable<>).MakeGenericType(argument);
+                               if(closedEnumerable.IsAssignableFrom(type))
+                               {
+                                       return argument;
+                               }
+                       }
+
+                       return null;
+               }
+       }
+
+       /// <inheritDoc/>
+       public class LinqResultWrapper<T> : IEnumerable<T>
+       {
+               private readonly QueryOptions options;
+               private readonly Expression expression;
+               private readonly Type rootType;
+               private IList<T> list;
+
+               /// <summary>
+               /// 
+               /// </summary>
+               /// <param name="options"></param>
+               /// <param name="expression"></param>
+               /// <param name="rootType"></param>
+               public LinqResultWrapper(QueryOptions options, Expression 
expression, Type rootType)
+               {
+                       this.options = options;
+                       this.rootType = rootType;
+                       this.expression = expression;
+               }
+
+               /// <inheritDoc/>
+               public IEnumerator<T> GetEnumerator()
+               {
+                       Populate();
+                       return list.GetEnumerator();
+               }
+
+               private void Populate()
+               {
+                       var linqQuery = new LinqQuery<T>(options, 
expression,rootType);
+                       ActiveRecordBase.ExecuteQuery(linqQuery);
+                       list = linqQuery.Result;
+               }
+
+               /// <inheritDoc/>
+               IEnumerator IEnumerable.GetEnumerator()
+               {
+                       Populate();
+                       return list.GetEnumerator();
+               }
+       }

File [added]: QueryProvider.cs
Delta lines: +250 -190
===================================================================

--- 
ActiveRecord/trunk/src/Castle.ActiveRecord.Linq.Tests/ActiveRecordLinqTestCase.cs
   2010-01-10 13:38:17 UTC (rev 6619)
+++ 
ActiveRecord/trunk/src/Castle.ActiveRecord.Linq.Tests/ActiveRecordLinqTestCase.cs
   2010-01-10 18:09:16 UTC (rev 6620)
@@ -12,254 +12,314 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-
-using System.Collections.Generic;
 using Castle.ActiveRecord.Linq.Tests.Model;
 
 namespace Castle.ActiveRecord.Linq.Tests
 {
-    using System;
-    using System.Linq;
-    using System.Threading;
-    using Castle.ActiveRecord.Framework;
-    using NHibernate.Criterion;
-    using NUnit.Framework;
+       using System.Linq;
 
-    [TestFixture]
-    public class ActiveRecordLinqTestCase : AbstractActiveRecordTest
-    {
-        [Test,
-         ExpectedException(typeof(ActiveRecordInitializationException),
-            ExpectedMessage = "You can't invoke ActiveRecordStarter.Initialize 
more than once")]
-        public void InitializeCantBeInvokedMoreThanOnce()
-        {
-            ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post));
-            ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Blog));
-        }
+       using Castle.ActiveRecord.Framework;
 
-        [Test]
-        public void SimpleOperations()
-        {
-            ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), 
typeof(Blog));
-            using (new SessionScope())
-            {
-                Recreate();
+       using NUnit.Framework;
 
+       [TestFixture]
+       public class ActiveRecordLinqTestCase : AbstractActiveRecordTest
+       {
+               [Test,
+                ExpectedException(typeof(ActiveRecordInitializationException),
+                       ExpectedMessage = "You can't invoke 
ActiveRecordStarter.Initialize more than once")]
+               public void InitializeCantBeInvokedMoreThanOnce()
+               {
+                       ActiveRecordStarter.Initialize(GetConfigSource(), 
typeof(Post));
+                       ActiveRecordStarter.Initialize(GetConfigSource(), 
typeof(Blog));
+               }
 
-                Post.DeleteAll();
-                Blog.DeleteAll();
+               [Test]
+               public void SimpleOperations()
+               {
+                       ActiveRecordStarter.Initialize(GetConfigSource(), 
typeof(Post), typeof(Blog));
 
-                var blogs = from b in Blog.Queryable select b;
+                       Recreate();
 
-                Assert.IsNotNull(blogs);
-                Assert.AreEqual(0, blogs.Count());
 
-                var blog = new Blog
-                {
-                    Name = "hammett's blog",
-                    Author = "hamilton verissimo"
-                };
-                blog.Save();
+                       Post.DeleteAll();
+                       Blog.DeleteAll();
 
+                       var blogs = from b in Blog.Queryable select b;
 
-                blogs = from b in Blog.Queryable select b;
-                Assert.IsNotNull(blogs);
-                Assert.AreEqual(1, blogs.Count());
+                       Assert.IsNotNull(blogs);
+                       Assert.AreEqual(0, blogs.Count());
 
-                var retrieved = Blog.Queryable.First();
-                Assert.IsNotNull(retrieved);
+                       var blog = new Blog
+                       {
+                               Name = "hammett's blog",
+                               Author = "hamilton verissimo"
+                       };
+                       blog.Save();
 
-                Assert.AreEqual(blog.Name, retrieved.Name);
-                Assert.AreEqual(blog.Author, retrieved.Author);
-            }
-        }
 
-        [Test]
-        public void SimpleOperationsShowingBug()
-        {
-            ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), 
typeof(Blog));
-            using (new SessionScope())
-            {
-                Recreate();
+                       blogs = from b in Blog.Queryable select b;
+                       Assert.IsNotNull(blogs);
+                       Assert.AreEqual(1, blogs.Count());
 
+                       var retrieved = Blog.Queryable.First();
+                       Assert.IsNotNull(retrieved);
 
-                Post.DeleteAll();
-                Blog.DeleteAll();
+                       Assert.AreEqual(blog.Name, retrieved.Name);
+                       Assert.AreEqual(blog.Author, retrieved.Author);
 
-                var blogs = from b in Blog.Queryable select b;
+               }
 
-                Assert.IsNotNull(blogs);
-                Assert.AreEqual(0, blogs.Count());
+               [Test]
+               public void SimpleOperationsShowingBug()
+               {
+                       ActiveRecordStarter.Initialize(GetConfigSource(), 
typeof(Post), typeof(Blog));
 
-                var blog = new Blog
-                {
-                    Name = "hammett's blog",
-                    Author = "hamilton verissimo"
-                };
-                blog.Save();
+                       Recreate();
 
 
-                blogs = from b in Blog.Queryable select b;
-                Assert.IsNotNull(blogs);
-                Assert.AreEqual(1, blogs.Count());
+                       Post.DeleteAll();
+                       Blog.DeleteAll();
 
-                // this line will fail because of blogs.Count above
-                var retrieved = blogs.First();
-                Assert.IsNotNull(retrieved);
+                       var blogs = from b in Blog.Queryable select b;
 
-                Assert.AreEqual(blog.Name, retrieved.Name);
-                Assert.AreEqual(blog.Author, retrieved.Author);
-            }
-        }
+                       Assert.IsNotNull(blogs);
+                       Assert.AreEqual(0, blogs.Count());
 
+                       var blog = new Blog
+                       {
+                               Name = "hammett's blog",
+                               Author = "hamilton verissimo"
+                       };
+                       blog.Save();
 
-        [Test]
-        public void SimpleOperations2()
-        {
-            ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), 
typeof(Blog));
-            using(new SessionScope())
-            {
-                Recreate();
 
-                Post.DeleteAll();
-                Blog.DeleteAll();
+                       blogs = from b in Blog.Queryable select b;
+                       Assert.IsNotNull(blogs);
+                       Assert.AreEqual(1, blogs.Count());
 
-                var blogs = Blog.Queryable;
-                Assert.IsNotNull(blogs);
-                Assert.AreEqual(0, blogs.Count());
+                       // this line will fail because of blogs.Count above
+                       var retrieved = blogs.First();
+                       Assert.IsNotNull(retrieved);
 
-                Blog blog = new Blog();
-                blog.Name = "hammett's blog";
-                blog.Author = "hamilton verissimo";
-                blog.Create();
+                       Assert.AreEqual(blog.Name, retrieved.Name);
+                       Assert.AreEqual(blog.Author, retrieved.Author);
 
-                Assert.AreEqual(1, (from
-                b in
-                Blog.Queryable select
-                b).
-                Count())
-                ;
+               }
 
-                blogs = Blog.Queryable;
-                Assert.AreEqual(blog.Name, blogs.First().Name);
-                Assert.AreEqual(blog.Author, blogs.First().Author);
 
-                blog.Name = "something else1";
-                blog.Author = "something else2";
-                blog.Update();
+               [Test]
+               public void SimpleOperations2()
+               {
+                       ActiveRecordStarter.Initialize(GetConfigSource(), 
typeof(Post), typeof(Blog));
 
-                blogs = Blog.Queryable;
-                Assert.IsNotNull(blogs);
-                Assert.AreEqual(1, Blog.Queryable.Count());
-                Assert.AreEqual(blog.Name, blogs.First().Name);
-                Assert.AreEqual(blog.Author, blogs.First().Author);
-            }
-        }
+                       Recreate();
 
-        [Test]
-        public void RelationsOneToMany()
-        {
-            ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), 
typeof(Blog));
+                       Post.DeleteAll();
+                       Blog.DeleteAll();
 
-            int blogId;
-            using (new SessionScope())
-            {
-                Recreate();
+                       var blogs = Blog.Queryable;
+                       Assert.IsNotNull(blogs);
+                       Assert.AreEqual(0, blogs.Count());
 
-                Post.DeleteAll();
-                Blog.DeleteAll();
+                       Blog blog = new Blog();
+                       blog.Name = "hammett's blog";
+                       blog.Author = "hamilton verissimo";
+                       blog.Create();
 
-                Blog blog = new Blog();
-                blog.Name = "hammett's blog";
-                blog.Author = "hamilton verissimo";
-                blog.Save();
+                       Assert.AreEqual(1, (from
+                       b in
+                                                                       
Blog.Queryable
+                                                               select
+                                                                       b).
+                       Count())
+                       ;
 
-                Post post1 = new Post(blog, "title1", "contents", "category1");
-                Post post2 = new Post(blog, "title2", "contents", "category2");
+                       blogs = Blog.Queryable;
+                       Assert.AreEqual(blog.Name, blogs.First().Name);
+                       Assert.AreEqual(blog.Author, blogs.First().Author);
 
-                post1.Save();
-                post2.Save();
+                       blog.Name = "something else1";
+                       blog.Author = "something else2";
+                       blog.Update();
 
-                blogId = blog.Id;
-            }
+                       blogs = Blog.Queryable;
+                       Assert.IsNotNull(blogs);
+                       Assert.AreEqual(1, Blog.Queryable.Count());
+                       Assert.AreEqual(blog.Name, blogs.First().Name);
+                       Assert.AreEqual(blog.Author, blogs.First().Author);
 
-            using (new SessionScope())
-            {
-                Blog blog = (from b in Blog.Queryable where b.Id == blogId 
select b).First();
+               }
 
-                Blog blog2 = Blog.Queryable.First(b => b.Id == blogId);
-                Assert.AreSame(blog, blog2);
-                
-                Blog blog3 = Blog.Find(blogId);
-                Assert.AreSame(blog, blog3);
+               [Test]
+               public void RelationsOneToMany()
+               {
+                       ActiveRecordStarter.Initialize(GetConfigSource(), 
typeof(Post), typeof(Blog));
 
-                Assert.IsNotNull(blog);
-                Assert.IsNotNull(blog.Posts, "posts collection is null");
-                Assert.AreEqual(2, blog.Posts.Count);
+                       int blogId;
 
-                foreach (Post post in blog.Posts)
-                {
-                    Assert.AreEqual(blog.Id, post.Blog.Id);
-                }
+                       Recreate();
 
-            }
-        }
+                       Post.DeleteAll();
+                       Blog.DeleteAll();
 
-        [Test]
-        public void UsingLinqFromNonLinqBaseClass()
-        {
-            ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Widget));
+                       Blog blog0 = new Blog();
+                       blog0.Name = "hammett's blog";
+                       blog0.Author = "hamilton verissimo";
+                       blog0.Save();
 
-            using (new SessionScope())
-            {
-                Recreate();
-                Widget.DeleteAll();
+                       Post post1 = new Post(blog0, "title1", "contents", 
"category1");
+                       Post post2 = new Post(blog0, "title2", "contents", 
"category2");
 
-                Widget widget = new Widget { Name = "Hello world" };
-                widget.Save();
-            }
+                       post1.Save();
+                       post2.Save();
 
-            using (new SessionScope())
-            {
-                var widgets = from w in ActiveRecordLinq.AsQueryable<Widget>() 
select w;
-                Assert.IsNotNull(widgets);
-                Assert.AreEqual(1, widgets.Count());
+                       blogId = blog0.Id;
 
-                var widget = (from w in ActiveRecordLinq.AsQueryable<Widget>() 
select w).First();
-                Assert.IsNotNull(widget);
-                Assert.AreEqual("Hello world", widget.Name);
 
-                var widget2 = ActiveRecordLinq.AsQueryable<Widget>().First(w 
=> w.Name == "Hello World");
-                Assert.IsNotNull(widget2);
-                Assert.AreEqual("Hello world", widget2.Name);
+                       using (new SessionScope())
+                       {
+                               Blog blog = (from b in Blog.Queryable where 
b.Id == blogId select b).First();
 
-                Assert.AreSame(widget2, widget);
-            }
-        }
+                               Blog blog2 = Blog.Queryable.First(b => b.Id == 
blogId);
+                               Assert.AreSame(blog, blog2);
 
+                               Blog blog3 = Blog.Find(blogId);
+                               Assert.AreSame(blog, blog3);
 
-        [Test]
-        public void UsingLinqViaSessionScopeVariable()
-        {
-            ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Widget));
+                               Assert.IsNotNull(blog);
+                               Assert.IsNotNull(blog.Posts, "posts collection 
is null");
+                               Assert.AreEqual(2, blog.Posts.Count);
 
-            using (ISessionScope scope = new SessionScope())
-            {
-                Recreate();
-                Widget.DeleteAll();
+                               foreach (Post post in blog.Posts)
+                               {
+                                       Assert.AreEqual(blog.Id, post.Blog.Id);
+                               }
 
-                var widgets = from w in scope.AsQueryable<Widget>() select w;
-                Assert.IsNotNull(widgets);
-                Assert.AreEqual(0, widgets.Count());
+                       }
+               }
 
-                Widget widget = new Widget { Name = "Hello world" };
-                widget.Save();                
+               [Test]
+               public void UsingLinqFromNonLinqBaseClass()
+               {
+                       ActiveRecordStarter.Initialize(GetConfigSource(), 
typeof(Widget));
 
-                widgets = from w in scope.AsQueryable<Widget>() where w.Name 
== "Hello World" select w;
-                Assert.IsNotNull(widgets);
-                Assert.AreEqual(1, widgets.Count());
-            }
-        }
+                       Recreate();
+                       Widget.DeleteAll();
 
-    }
+                       Widget widget0 = new Widget { Name = "Hello world" };
+                       widget0.Save();
+
+                       using (new SessionScope())
+                       {
+                               var widgets = from w in 
ActiveRecordLinq.AsQueryable<Widget>() select w;
+                               Assert.IsNotNull(widgets);
+                               Assert.AreEqual(1, widgets.Count());
+
+                               var widget = (from w in 
ActiveRecordLinq.AsQueryable<Widget>() select w).First();
+                               Assert.IsNotNull(widget);
+                               Assert.AreEqual("Hello world", widget.Name);
+
+                               var widget2 = 
ActiveRecordLinq.AsQueryable<Widget>().First(w => w.Name == "Hello World");
+                               Assert.IsNotNull(widget2);
+                               Assert.AreEqual("Hello world", widget2.Name);
+
+                               Assert.AreSame(widget2, widget);
+                       }
+               }
+
+
+               [Test]
+               public void UsingLinqViaSessionScopeVariable()
+               {
+                       ActiveRecordStarter.Initialize(GetConfigSource(), 
typeof(Widget));
+
+                       using (ISessionScope scope = new SessionScope())
+                       {
+                               Recreate();
+                               Widget.DeleteAll();
+
+                               var widgets = from w in 
scope.AsQueryable<Widget>() select w;
+                               Assert.IsNotNull(widgets);
+                               Assert.AreEqual(0, widgets.Count());
+
+                               Widget widget = new Widget { Name = "Hello 
world" };
+                               widget.Save();
+
+                               widgets = from w in scope.AsQueryable<Widget>() 
where w.Name == "Hello World" select w;
+                               Assert.IsNotNull(widgets);
+                               Assert.AreEqual(1, widgets.Count());
+                       }
+               }
+
+
+
+               [Test]
+               public void Linq_without_session_scope()
+               {
+                       ActiveRecordStarter.Initialize(GetConfigSource(), 
typeof(Widget));
+                       Recreate();
+                       Widget.DeleteAll();
+
+                       var widgets = from w in 
ActiveRecordLinq.AsQueryable<Widget>() select w;
+                       Assert.IsNotNull(widgets);
+                       Assert.AreEqual(0, widgets.Count());
+               }
+
+               [Test]
+               public void Linq_without_session_scope2()
+               {
+                       ActiveRecordStarter.Initialize(GetConfigSource(), 
typeof(Widget));
+                       Recreate();
+                       Widget.DeleteAll();
+
+                       var widget = new Widget { Name = "foo" };
+                       widget.Save();
+
+                       var orderedQueryable = 
ActiveRecordLinqBase<Widget>.Queryable;
+                       var widgets = (from w in orderedQueryable
+                                      where w.Name.StartsWith("f")
+                                      select w).ToList();
+
+                       Assert.IsNotNull(widgets);
+                       Assert.AreEqual("foo", widgets.Single().Name);
+               }
+
+               [Test]
+               public void Projecting()
+               {
+                       ActiveRecordStarter.Initialize(GetConfigSource(), 
typeof(Widget));
+                       Recreate();
+                       Widget.DeleteAll();
+
+                       var widget = new Widget { Name = "foo" };
+                       widget.Save();
+
+                       var orderedQueryable = 
ActiveRecordLinqBase<Widget>.Queryable;
+                       var widgets = (from w in orderedQueryable
+                                                  where w.Name.StartsWith("f")
+                                                  select w.Name).ToList();
+
+                       Assert.IsNotNull(widgets);
+                       Assert.AreEqual("foo", widgets.Single());
+               }
+               [Test]
+               public void Projecting2()
+               {
+                       ActiveRecordStarter.Initialize(GetConfigSource(), 
typeof(Widget));
+                       Recreate();
+                       Widget.DeleteAll();
+
+                       var widget = new Widget { Name = "foo" };
+                       widget.Save();
+
+                       var orderedQueryable = 
ActiveRecordLinqBase<Widget>.Queryable;
+                       var name = (from w in orderedQueryable
+                                                  where w.Name.StartsWith("f")
+                                                  select w.Name).First();
+
+                       Assert.IsNotNull(name);
+                       Assert.AreEqual("foo", name);
+               }
+       }

Directory: /ActiveRecord/trunk/src/Castle.ActiveRecord.Linq.Tests/
==================================================================

File [modified]: ActiveRecordLinqTestCase.cs
Delta lines: +0 -0
===================================================================

-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/castle-project-commits?hl=en.


Reply via email to