EF 4.1 + UnitOfWOrk + Repository + Transactions design patterns best practice. RRS feed

  • Question

  • Hello Guys,


    I have the following implementation ( this is just an example of the general problem)



    public class Db : DbContext, IUnitOfWork
            public DbSet<Project> Projects { get; set; }       
       	public DbSet<ProjectItem> ProjectItems { get; set; }
            void IUnitOfWork.SaveChanges()

    For each of this DbSet i have a Repository class.

    Base on this fundation i have some bussines logic, lets say based on the command pattern.

    One of my commands creates a project template ( adds a project, and several project items in one trasaction. )

    My question is what is the best way to handle this  ?

    1. in the ProjectRepository class add ProjectItem objects (like projecc.ProjectItems.Add( .... ). - this will work, but i see here violation of the "separation of concerns" prinicple - ProjectRepository class deals with the ProjectItem persistance which i do not like becouse i have a separate ProjectItemRepository repository with some specific methods.

    2. use TransactionScope object on the command level - this will work as well, but it seems to violate UnitOfWork pattern where the SaveChanges method shold close the transaction instead of Commit on the TransactionScope level.


    Any suggestions are welcome :)











    • Edited by samurexx Saturday, January 21, 2012 9:00 AM
    Friday, January 20, 2012 4:53 PM

All replies

  • Hi,

    We will try to involve some senior engineer on this issue, and thanks for your patience!

    Good day!


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Monday, January 23, 2012 3:03 AM