Building Real Applications with Telerik MVC & OpenAccess – Building the DAL

Monday, February 15, 2010 by Joshua Holt | Comments 4

In this blog post we will create a simple data access layer that allows our UI to interact with our data.  This will be hand coded for the most part, luckily it doesn’t take much code to get it all together. :)

 

First Things First

The first thing i am going to do is create a generic repository interface that my other repositories will implement.

   1: public interface IRepository<T> where T : IPerstistable
   2:    {
   3:        void Add(T item);
   4:  
   5:        IQueryable<T> GetAll();
   6:  
   7:        T GetItemByID(int id);
   8:  
   9:        void Delete(T item);
  10:  
  11:        int Count();
  12:  
  13:        void Save();
  14:    }

There is nothing too crazy here, I have created my generic interface and constrained my Type parameter saying that it must implement the IPersistable interface.  This interface allows me to easily create a mock for testing!

 

Putting it to Work

Now I need to create a class that implements my interface, and ties in open access.

For this this to work well in a web environment, we want to make sure that we use one Object Scope through out the life of a request.  This being the case, I want to go ahead and inject my open access scope into my repositories, so that i can control when my object scope is created and when it is disposed.  I know we can easily do this using DI, however I want to show how it can be handled without it. 

So we will first create a protected field of IObjectScope, which will allow all class that extend this repository class to access it, and then we will create a constructor that requires a scope object.

   1: protected IObjectScope scope;       
   2:  
   3: public Repository(IObjectScope objectScope)
   4: {
   5:     scope = objectScope;
   6: }

 

Also, when i implement my interface, i am going to mark the methods virtual, to allow derived classes to override the methods if needed.  

   1: public virtual void Add(T item)
   2:         {
   3:             scope.Add(item);
   4:         }
   5:  
   6:         public virtual IQueryable<T> GetAll()
   7:         {
   8:             var query = from items in scope.Extent<T>()
   9:                         select items;
  10:  
  11:             return query;
  12:         }
  13:  
  14:         public virtual T GetItemByID(int id)
  15:         {
  16:             var query = from items in scope.Extent<T>()
  17:                         where items.ID == id
  18:                         select items;
  19:  
  20:             return query.FirstOrDefault<T>();
  21:         }
  22:  
  23:         public virtual void Delete(T item)
  24:         {
  25:             scope.Remove(item);
  26:  
  27:         }
  28:  
  29:         public virtual int Count()
  30:         {
  31:             return scope.Extent<T>().Count();
  32:         }
  33:  
  34:         public virtual void Save()
  35:         {
  36:             scope.Transaction.Commit();
  37:         }

Now we can create specific implentations of our repository class.  I will create one for AssetGroup, Asset, and Alerts because these are my “root objects”. 

 

Once these things are finished, we are ready to start working on our UI, so in the next blog we will take a look at large data concerns when working on with a web UI.  So check back soon! Also, I have created a video that goes a little more in-depth about handling Object scope when using MVC, It should be uploaded shortly to Telerik TV.

4 Comments

  • chelito 03 Mar 2010
    Hi Josh,

    I like the way on how you integrate the asp.net mvc and openaccess, Is it possible to share the source code?  Thanks..
  • Joshua Holt 05 Mar 2010
    Hi Chelito,
    Thank you for reading!  I will get a version together that I can share in the very near future :)
    -Josh
  • Allen 14 Apr 2010
    Any updates on getting the source posted?  Thanks :)
  • Bill 11 Nov 2010
    And code that works with the Q3 2010 release of OpenAccess?  Please, please please! :)

Add comment

  1. Formatting options
       
     
     
     
     
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)