Tag Archive for repository

Repository Pattern, Lazy Style

One of the most annoying things when working with MVC is repeating the same code over and over again at each layer. We can try to abstract our classes, but it still only gets us so far, a lot of the code must still be manually typed or templated. I found myself particularly tired of creating repository objects for each of my database tables. Sure, I was inheriting from a generic base class, but a lot of the implementation I couldn’t get away from.

Using Linq2Sql was one of the reasons for my headaches. Types aren’t so easy to get at, and queries cannot be created dynamically at runtime–or could they? Dynamic Linq to the rescue. Microsoft has a seperate library available for download which actually allows you to run dynamic queries at runtime. Once added to your project, the dynamic functions are added through class extensions to the standard Linq classes.

I decided that I would start by creating a generic, templated, repository layer which could be extended if necessary, but would by default contain all of the necessary functions to interact with the model layer.

using System.Linq.Dynamic;

public abstract class Repository<R> where R : class
{
    public virtual DataContext Context { get; protected set; }

    public Repository() { }

    public Repository(DataContext context)
    {
        Context = context;
    }

    public virtual void Insert(R record)
    {
        Context.GetTable<R>().InsertOnSubmit(record);
    }

    public virtual void Delete(R record)
    {
        Context.GetTable<R>().DeleteOnSubmit(record);
    }

    public virtual void Update(R record)
    {
        return;
    }

    public virtual IEnumerable<R> GetAll()
    {
        var records = from r in Context.GetTable<R>()
                        select r;

        return records;
    }

    public virtual void Save()
    {
        Context.SubmitChanges();
    }

    public virtual void Revert(R record)
    {
        Context.Refresh(RefreshMode.OverwriteCurrentValues, record);
    }
}

Then create a class which inherits from your template, and code in all of the functions which are application specific.

public class ApplicationRepository<R> : Repository<R> where R: class
{
    public ApplicationDataContext AppContext
    {
        get
        {
            return AppContext as ApplicationDataContext;
        }
    }

    public ApplicationRepository()
    {
        AppContext = new ApplicationDataContext();
    }

    public virtual R Get(Guid Id)
    {
        var record = (from  r in AppContext.GetTable<R>().Where("Id = @0", Id)
                        select r).Single();

        return record;
    }

    public virtual IEnumerable<R> GetAll(string clause, params object[] args)
    {
        var records = from  r in AppContext.GetTable<R>().Where(clause, args)
                        select r;

        return records;
    }

    public override void Insert(R record)
    {
        var d = record as dynamic;
        d.Id = Guid.NewGuid();
        d.CreatedOn = DateTime.Now;
        d.CreatedBy = "System";

        base.Insert(record);
    }

    public override void Update(R record)
    {
        var d = record as dynamic;
        d.ModifiedOn = DateTime.Now;
        d.ModifiedBy = "System";

        base.Update(record);
    }
}

In my case, I added a narrowing ApplicationDataContext and some additional functionality. One of the key functions is the GetAll(string, param object[]) function. This allows me to essential run any queries against my database using that Dynamic Linq library. All of the functionality that I need from a repository layer will most likely already exist in this implementation. Anything else that is needed can be added later by inheriting from the ApplicationRepository class. This is going to save me a great deal of time.