Click here to Skip to main content
13,196,426 members (55,589 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

17.2K views
8 bookmarked
Posted 17 Feb 2012

Repositories, Unit Of Work and ASP.NET MVC

, 17 Feb 2012
Rate this:
Please Sign up or sign in to vote.
Repositories, Unit Of Work and ASP.NET MVC

There are a lot of posts discussing repository implementations, unit of work and ASP.NET MVC. This post is an attempt to give you an answer which addresses all three issues.

Repositories

Do NOT create generic repositories. They look fine when you look at them. But as the application grows, you’ll notice that you have to do some workarounds in them which will break open/closed principle.

It’s much better to create repositories that are specific for an entity and it’s aggregate since it’s much easier to show intent. And you’ll also only create methods that you really need, YAGNI.

Generic repositories also remove the whole idea with choosing an OR/M since you can’t use your favorite OR/Ms features.

Unit Of Work

Most OR/Ms available do already implement the UoW pattern. You simply just need to create an interface and make an adapter (Google Adapter pattern) implementation for your OR/M.

Interface:

public interface IUnitOfWork : IDisposable
{
    void SaveChanges();
}

NHibernate sample implementation:

public class NHibernateUnitOfWork : IUnitOfWork
{
    private readonly ITransaction _transaction;

    public NHibernateUnitOfWork(ISession session)
    {
        if (session == null) throw new ArgumentNullException("session");
        _transaction = session.BeginTransaction();
    }

    public void Dispose()
    {
        if (!_transaction.WasCommitted)
            _transaction.Rollback();

        _transaction.Dispose();
    }

    public void SaveChanges()
    {
        _transaction.Commit();
    }
}

ASP.NET MVC

I prefer to use an attribute to handle transactions in MVC. It makes the action methods cleaner:

[HttpPost, Transactional]
public ActionResult Update(YourModel model)
{
    //your logic here
}

And the attribute implementation:

public class TransactionalAttribute : ActionFilterAttribute
{
    private IUnitOfWork _unitOfWork;

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        _unitOfWork = DependencyResolver.Current.GetService<IUnitOfWork>();

        base.OnActionExecuting(filterContext);
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        // let the container dispose/rollback the UoW.
        if (filterContext.Exception == null)
            _unitOfWork.SaveChanges();

        base.OnActionExecuted(filterContext);
    }
}

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

jgauffin
Founder 1TCompany AB
Sweden Sweden
Skip logfiles, try automated error handling!

I'm one of the founders of codeRR, a .NET service which takes care of everything related to exceptions, so that you can focus on writing code.

blog | twitter

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 1 Pin
SwapnadipSaha28-Feb-12 19:49
memberSwapnadipSaha28-Feb-12 19:49 
GeneralRe: My vote of 1 Pin
jgauffin29-Feb-12 22:44
memberjgauffin29-Feb-12 22:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171019.1 | Last Updated 17 Feb 2012
Article Copyright 2012 by jgauffin
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid