The EntityManager class is the entry point into accessing the rich functionality given to you by Rapid Entity Framework, this class is inheritable, and the reason's for this is design flexibilities over your propriety code. Repository design pattern helps you separates data access concerns from business logic's. It abstracts the complexity of querying, deleting, updating, creating data from your entire code, thereby giving you a single Aggregate API that access your data in a uniformed way. There is more to repository design pattern, than what i will be explaining (Dont think i am cheating) . Now let us look at how we are to inherit from the EntityManager class.
  1. First example is Linq Repository

    internal class LinqRepository :IDisposable
    {
        private EntityManager entityManager;

        internal LinqRepository()
        {
            entityManager = new EntityManager();
            entityManager.OpenDatabaseSession();
        }

        internal IQueryable<Customer> Customers
        {
            get { return entityManager.AsQueryable<Customer>(); }
        }

        public void Dispose()
        {
            entityManager.Dispose();
        }
    }

Code review
Note that the LinqRepository class has a containment relationship with our good old EntityManager class. The containment relationship is another way of abstracting the complexities of the EntityManager class. Now, let us see how our Linq Repository class will be used to query using Linq grammars.

            using ( LinqRepository repository = new LinqRepository() )
            {
                var customers = (from c in repository.Customers
                                 where c.Address.City.Contains("a")
                                 select new
                                 {
                                     c.CompanyName
                                 }).ToList();
            }

See the generated SQL
 SELECT  [t2].[CompanyName] AS [R0] 
FROM  [Customers] [t2] , 
   (       SELECT  
                [t0].[CustomerID] AS [CustomerID] 
           FROM  [Customers] [t0]  WHERE  [t0].[City]  LIKE  @Cityt0_0_47a30 
  ) t1  
WHERE  [t2].[CustomerID]  =  [t1].[CustomerID]

2. RQL Repository
RQL the Frameworks endorsed language can also be used for repository pattern. Our next example will show you how RQL will be used in its own repository context.

    internal class RQLRepository<T> : From , IDisposable where T : Customer
    {
        internal T Current;
        private EntityManager entityManager;

        internal RQLRepository() :base( typeof( T ) )
        {
            entityManager = new EntityManager();
            entityManager.OpenDatabaseSession();            
        }

        internal AS Customers
        {
            get { return As(Current = entityManager.AsAlias<T>()); }
        }

        public void Dispose()
        {
            entityManager.Dispose();
        }
    }

The code above is how to ensure Repository designs for RQL. This a a simple example, i am sure in reality, developers would explore other possibilities around this option. Now, let us look at how to use the above code to query the data store using RQL.

            using ( RQLRepository<Customer> repo = new RQLRepository<Customer>() )
            {
                var customers = repo.Customers.Where( repo.Current.Address.City ).Contains( "a" )
                                          .SelectNew(   () =>
                                                            new
                                                            {
                                                                repo.Current.CompanyName
                                                            }
                                                    ).Execute();
            }

See the generated SQL
 SELECT  [t2].[CompanyName] AS [R0] 
FROM  [Customers] [t2] , 
   (       SELECT  
                [t0].[CustomerID] AS [CustomerID] 
           FROM  [Customers] [t0]  WHERE  [t0].[City]  LIKE  @Cityt0_0_47a30 
  ) t1  
WHERE  [t2].[CustomerID]  =  [t1].[CustomerID]

As you must have noticed, the Linq and RQL generates the same SQL query, this is because Rapid Entity Framework has an internal query generator API which all queries call to write an sql query. Whether it is LINQ , Criteria API , XML Query and RQL , they are all sitting on a single query generator, this approach ensures that queries generated are uniformed and less error prone, and also makes Rapid developers to respond quickly to fixes.

Now the last Inheriting from the EntityManager**

Since inheritance gives you the same methods of the base class, i would live that to you to figure out. Infact, its either inheritance or containment relationship.





Last edited Feb 7, 2010 at 6:28 PM by ahmedsalako, version 14

Comments

No comments yet.