locked
What is the difference between repository pattern & Unit of work pattern RRS feed

  • Question

  • User88744855 posted

    it will be great help if some one explain the difference between repository pattern & Unit of work pattern with sample code. thanks

    Friday, November 29, 2013 2:12 AM

Answers

  • User-488622176 posted

    In brief : the unit-of-work tracks the elements that needs be changed, and repository contains the logic to persist those changes to a ... repository (file, dbase, ...)
    MS has a very good article on the UOW pattern here : http://msdn.microsoft.com/en-us/magazine/dd882510.aspx

    Underneath you have 2 interfaces : one for the repository and one for the UOW. See the differences. 

    Repository sample:

    public interface IRepository<T>
        where T : class
    {
    
        bool IsActive { get; }
    
        bool WasCommitted { get; }
    
        /// <summary>
        /// Commits all changes made on the unit of work.
        /// </summary>
        void Commit();
    
        bool WasRolledBack { get; }
    
        /// <summary>
        /// Rolls back all changes made on the unit of work.
        /// </summary>
        void Rollback();
    
        /// <summary>
        /// Returns an instance of an entity with the specified key that is attached to the unit of work without
        /// loading the entity from a repository.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        T Load<T>(int id)
            where T : class;
    
        void Attach<T>(T entity)
            where T : class, IIdentifiable;
    
        void Detach<T>(T entity)
            where T : class;
    
        IRepository<T> GetRepository<T>()
            where T : class;
    
        void Add(T entity);
    
        void Remove(T entity);
    
        /// <summary>
        /// Returns an instance of an entity with the specified key that is attached to the unit of work by loading
        /// the entity from the repository.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        T Get(int id);
    
        IQueryable<T> All();
    }



    Unit of work sample : 

    public interface IUnitOfWork<TEntity> where TEntity : IEntity, class 
        : IDisposable
    {
        void Commit();
        void Discard();
        void Track(TEntity entity);
        void Delete(TEntity entity);
    }
    

    With entity :

    public interface IEntity
    {
        GUID Id { get; }
    }
    

    The difference is well explained here : http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 29, 2013 8:52 AM
  • User71929859 posted

    Unit of work design pattern is responsible for track changes of data retrieved from different respositories. So when it comes to save the changes, it can be used to save all the changes with a one atomic transaction.

    Check the below MSDN blog post for more information

    http://blogs.msdn.com/b/adonet/archive/2009/06/16/using-repository-and-unit-of-work-patterns-with-entity-framework-4-0.aspx

    Both design patterns are used to seperate the data access code form business and presentation layers.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 30, 2013 10:38 PM

All replies

  • User-488622176 posted

    In brief : the unit-of-work tracks the elements that needs be changed, and repository contains the logic to persist those changes to a ... repository (file, dbase, ...)
    MS has a very good article on the UOW pattern here : http://msdn.microsoft.com/en-us/magazine/dd882510.aspx

    Underneath you have 2 interfaces : one for the repository and one for the UOW. See the differences. 

    Repository sample:

    public interface IRepository<T>
        where T : class
    {
    
        bool IsActive { get; }
    
        bool WasCommitted { get; }
    
        /// <summary>
        /// Commits all changes made on the unit of work.
        /// </summary>
        void Commit();
    
        bool WasRolledBack { get; }
    
        /// <summary>
        /// Rolls back all changes made on the unit of work.
        /// </summary>
        void Rollback();
    
        /// <summary>
        /// Returns an instance of an entity with the specified key that is attached to the unit of work without
        /// loading the entity from a repository.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        T Load<T>(int id)
            where T : class;
    
        void Attach<T>(T entity)
            where T : class, IIdentifiable;
    
        void Detach<T>(T entity)
            where T : class;
    
        IRepository<T> GetRepository<T>()
            where T : class;
    
        void Add(T entity);
    
        void Remove(T entity);
    
        /// <summary>
        /// Returns an instance of an entity with the specified key that is attached to the unit of work by loading
        /// the entity from the repository.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        T Get(int id);
    
        IQueryable<T> All();
    }



    Unit of work sample : 

    public interface IUnitOfWork<TEntity> where TEntity : IEntity, class 
        : IDisposable
    {
        void Commit();
        void Discard();
        void Track(TEntity entity);
        void Delete(TEntity entity);
    }
    

    With entity :

    public interface IEntity
    {
        GUID Id { get; }
    }
    

    The difference is well explained here : http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 29, 2013 8:52 AM
  • User71929859 posted

    Unit of work design pattern is responsible for track changes of data retrieved from different respositories. So when it comes to save the changes, it can be used to save all the changes with a one atomic transaction.

    Check the below MSDN blog post for more information

    http://blogs.msdn.com/b/adonet/archive/2009/06/16/using-repository-and-unit-of-work-patterns-with-entity-framework-4-0.aspx

    Both design patterns are used to seperate the data access code form business and presentation layers.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 30, 2013 10:38 PM
  • User88744855 posted

    i believe both the pattern are different from each other. suppose if some one is developing a web site for selling online product then which patter they should follow... repository pattern or Unit of work pattern ??

    it would be nice if u guys tell me when one should go for repository pattern and when one should follow Unit of work pattern ?

    i have seen people developing web site but mix both the pattern.....why ?

    thanka for the discussion.

    Monday, December 2, 2013 2:37 AM
  • User88744855 posted

    i believe both the pattern are different from each other. suppose if some one is developing a web site for selling online product then which patter they should follow... repository pattern or Unit of work pattern ??

    it would be nice if u guys tell me when one should go for repository pattern and when one should follow Unit of work pattern ?

    i have seen people developing web site but mix both the pattern.....why ?

    thanka for the discussion.

    Monday, December 2, 2013 2:37 AM