ARTICLE

Generic Data Access Layer: Part 1

Posted by Mahadesh Mahalingappa Articles | ADO.NET in C# September 08, 2011
In this article we wwill be discussing how we can create a Generic Data Acess Layer which we could use in developing our Business Applications.
Reader Level:

In this article we will be discussing how we can create a Generic Data Access Layer which we could use in developing our Business Applications.

Lets start by creating a Data Model.

Create a ADO.NET Entity Data Model as shown below :

GDAccLyr1.gif

Once the Data Model is added to my project, I can then move to the next step, Creating a Repository.

Creating a Repository Class

public class Repository<T> where T : EntityObject
    {
        IObjectSet<T> _objectSet;

        /// <summary>
        /// This method is used to pass the Context object as a parameter to the Respository
        /// </summary>
        /// <param name="objectContext"></param>
        public Repository(ObjectContext objectContext)
        {
                _objectSet = objectContext.CreateObjectSet<T>();
        }

        /// <summary>
        /// This method we manually pass the Context Object
        ///
        /// </summary>
        public Repository()
        {
            DataEntities context = new DataEntities ();
            _objectSet =  context.CreateObjectSet<T>();
        }

        public IQueryable<T> AsQueryable()
        {
            return _objectSet;
        }

        public IEnumerable<T> GetAll()
        {
            return _objectSet.ToList();
        }

        public IEnumerable<T> Find(Expression<Func<T, bool>> where)
        {
            return _objectSet.Where(where);
        }

        public T Single(Expression<Func<T, bool>> where)
        {
            return _objectSet.Single(where);
        }

        public T First(Expression<Func<T, bool>> where)
        {
            return _objectSet.First(where);
        }

        public void Delete(T entity)
        {
            _objectSet.DeleteObject(entity);
        }

        public void Add(T entity)
        {
            _objectSet.AddObject(entity);
        }

        public void Attach(T entity)
        {
            _objectSet.Attach(entity);
        }
    }


I can now use this Repository Class in a Client Project and easily access or modify the Entities using the Above methods.

The client code would look like below :

Repository<Company> company = new Repository<Company>();

            foreach (Company comp in company.GetAll())
            {
                Console.WriteLine(comp.Name);
            }
            Console.ReadLine();


Note that here I am not using the context object at all to get the entities as I have already taken care of that in the below code in Repository Class .

/// <summary>
///
This method we manually pass the Context Object
///
/// </summary>
public Repository()
{
    DataEntities context = new DataEntities ();
    _objectSet =  context.CreateObjectSet<T>();
}


Thanks . In my next post we will look into how we can improve on this Data access Layer which we have just created.
 

Login to add your contents and source code to this article
post comment
     
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter