locked
Repository with Unit of Work RRS feed

  • Question

  • User435149036 posted

    Hi All,

    Here i come again. after discussing a lot with the members here i have finalized the following architecure for my DAL.

    public interface iRepository<T> where T:Class

    {

    T GetAll();

    T Get(Expression<Func<T,Bool>> func);

    }

    public ProductRepo <T>:IRepository<T> where T:class

    {

    public T Get(){ }

    public T Get(Expression<Func<T,bool>> func){ }

    }

    and so on. There will be one repository for each eggregate root. i.e Product,Orders,Users etc. And here comes the problem. If you see my design i am using T . So for product it will be Product Entity and for Orders it will be Order entity. What i can understand is that product repo should be responsible for managing product related tables. whereas order repo is orders. But order repo may need to update the product table(e.g. available stock) how do i handle this scenario? Is it Okay to update product table from order repo? 

    Also i will be using Unit Of work on top of Repository to have consistance transaction.

    What i have thought is like 

    public IRepository<Product> Product= new ProductRepo() //This will done using DI.

    public IRepository<Order> Orders= new OrderRepo() 

    When i am using Interface i am restircting business layer to access only those methods that are available in IRepository. What if i have a specific method that is required for Product Repo only. how can i access this method?

    Wednesday, May 14, 2014 3:14 PM

Answers

  • User-760709272 posted

    If you need to update the product table based on an order then you can pass the relevant product, or product id, to the relevant method in the order repository.  If you have operations that need to access both repos then this is the kind of thing you handle in your business objects.  Your code will call "UpdateStock" and that method will coordinate between the two repositories, getting and setting info in both.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 15, 2014 4:47 AM

All replies

  • User-760709272 posted

    How are you accessing your data?  Are you using Entity Framework?

    Wednesday, May 14, 2014 3:25 PM
  • User435149036 posted

    Yes I am using Entity Framework

    Wednesday, May 14, 2014 5:32 PM
  • User-760709272 posted

    The DbContext provided by EF does the job of the Unit of Work pattern, it tracks your changes and submits them atomically.  Also Linq to Entities mean that repository patterns aren't much use either, you might as well treat EF as your data access layer and use it direct from your business layer.  You can find an example of EF with repos and UoW here anyway, if you look at the repo methods they only have one line of code each, you're really just "passing through".

    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

    If your repos have some serious code in them, linking many tables and having many clauses etc then you might get some value from the repo pattern.

    Wednesday, May 14, 2014 5:40 PM
  • User435149036 posted

    Hi Aidy,

    Yes you are correct. EF and DBContext does offer Repository and UoW respectively. But our DB logic is tricker. That is why we have decided to use Repository with UoW for neater architecture. I mean instead of doing all the joins in business we will use them in repository. But my concern is if we reference Product table from Order repository. Aren't we voilating the architecture. Because in my understanding order repo should handle orders and product repo should handle products. but in some scenario there need to communicate with other. How can i handle this situation?

    Thursday, May 15, 2014 2:23 AM
  • User-760709272 posted

    If you need to update the product table based on an order then you can pass the relevant product, or product id, to the relevant method in the order repository.  If you have operations that need to access both repos then this is the kind of thing you handle in your business objects.  Your code will call "UpdateStock" and that method will coordinate between the two repositories, getting and setting info in both.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 15, 2014 4:47 AM