11

I am new to LINQ to SQL and attempting to create a generic Data Access Object (DAO) for the basic Create, Read, Update, and Destroy (CRUD) methods so that I can reuse the code. I was successful in creating a generic method that will delete any entity by using the code below but, I was wondering if anyone knows how to create a generic method that will select any entity by a common Id field that exists on all tables.

    /// <summary>
    /// Generic method that deletes an entity of any type using LINQ
    /// </summary>
    /// <param name="entity"></param>
    /// <returns>bool indicating whether or not operation was successful</returns>
    public bool deleteEntity(Object entity)
    {
        try
        {
            DomainClassesDataContext db = new DomainClassesDataContext();
            db.GetTable(entity.GetType()).Attach(entity);
            db.GetTable(entity.GetType()).DeleteOnSubmit(entity);
            db.SubmitChanges();
            return true;
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.StackTrace);
            return false;
        }
    }

I am pretty sure that the same patter will work for update and insert and would like to have a generic method on the GenericDAO that will retrieve me any entity (i.e. Customer, Invoice, WorkOrder, etc...) based on the entities Id. Thanks in advance for the replies.

1 Answer 1

18

I think you are looking for Repository Pattern, the following is a simple implementation of it:

First you need to create an interface IRepository like this:

public interface IRepository<T> where T : class
{
    void Add(T entity);
    void Delete(T entity);
    void Update(T entity);
    IEnumerable<T> All();
    ...
}

Then:

public class Repository<T> : IRepository<T>
    where T : class, IEntity
{
    DataContext _db;
    public Repository()
    {
        _db = new DataContext("Database string connection");
        _db.DeferredLoadingEnabled = false;
    }
    public void Add(T entity)
    {
        if (!Exists(entity))
            GetTable.InsertOnSubmit(entity);
        else
            Update(entity);
        SaveAll();
    }
    public void Delete(T entity)
    {
        GetTable.DeleteOnSubmit(entity);
        SaveAll();
    }
    public void Update(T entity)
    {
        GetTable.Attach(entity, true);
        SaveAll();
    }
    System.Data.Linq.Table<T> GetTable
    {
        get { return _db.GetTable<T>(); }
    }
    public IEnumerable<T> All()
    {
        return GetTable;
    }
}

Then :

public class CustomerRepository : Repository<Customer>
{
    public ProductRepository()
        : base()
    {
    }
}

Then you can have something like:

Customer newCustomer = new Customer { FistName = "Foo", LastName = "Boo" };
_customerRepository.Add(newCustomer);

Where Customer is an entity mapped to your database which is defined in the .dbml. This is just a start, see the following for more details:

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you very much. This works perfectly. I have only used Hibernate with Java in the past and the framework was already in place. Now, I am the sole developer working on a project from scratch so this is the first time I have had to write my own data access layer outside of a team. I wish I had enough rep to vote this solution up. I am new to the community and will be sure to vote this up when my rep is higher.
@AaronMajor Glade to help and welcome to stackoverflow, BTW: you can mark the answer as accepted if you found it helpful see this: stackoverflow.com/faq#howtoask
@MahmoudGamal great answer. Is it mandatory to use an interface can't we just define a single class(Repository) and from controller's Action method Add or Update or Delete a record. Thanks
@User - No it is not a mandatory, but the interface works as a generic repository, it has the advantages of the interface instead of the class directly, like if a particular repository requires additional operations, you can extend the generic repository interface. Generally, it depends on your design and requirements, also read more about classes and interfaces and when you should choose over the other.
@MahmoudGamal Thanks for your answer and your help is appreciated. I do have one more doubt as per this link programmers.stackexchange.com/questions/180851/… Entity Framework already implements a repository pattern then if this is the case can't I use my own repository so that I can reduce the coding. Requires your kind advise and thanks for your time and patience.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.