Search code examples
c#domain-driven-designrepository-patternunit-of-work

Is Unit Of Work and Repository Patterns very useful for big projects?


I'm starting a new web project using ASP.NET Webforms + EF4. I'm trying to apply a repository pattern with a unit of work pattern following this tutorial : http://www.dotnetage.com/publishing/home/2011/07/05/6883/the-repository-pattern-with-ef-code-first-dependeny-injection-in-asp-net-mvc3.html

I think I got the idea but my question is that, when I create a new object in the model, do I also have to define that object in IDALContext of the Unit Of Work? Isn't that a handbreak for rapid development? Also if you work with multiple developers and if you don't want other developers to see your DAL, how can you manage this? Because in this pattern as I understand, when you create a new object in the model you also have to define it in the IDALContext for this tutorial. Sorry I'm so confused by this.


Solution

  • Martin Fowler describes the repository's role as: "A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection". What Entity Framework 4.1 exposes is a repository in that sense. Also EF has a unity of work built in. So my advice is to ignore the blog article you mentioned in your question.

    Code like this is not just only useless or worthless but dangerous because there is no benefit added to your code but a dependency!

    public interface IUnitOfWork:IDisposable
    {
        int SaveChanges();
    }
    
    public interface IDALContext : IUnitOfWork
    {
        ICategoryRepository Categories { get; }
        IProductRepository Products { get; }
    }
    

    To answer your question having some abstraction that mediates between the domain and data mapping layers, acting like an in-memory domain object collection is a must for "big" projects. And having a UnitOfWork mechanism under the hood can help decouple your business logic from access to a some data access abstraction.

    TL;TR; Repository and UnitOfWork can help you but don't apply it like in the given blog post.