using UnitOfWork and Repository Pattern with Entity Framework

后端 未结 3 1131
情书的邮戳
情书的邮戳 2021-02-04 21:22

I\'m gonna to use repository and UnitOfwork in my data access layer to do this take a look at one contact aggregateroot

 public interface IAggregateRoot
    {
          


        
3条回答
  •  眼角桃花
    2021-02-04 22:16

    Inside the UnitOfWork class you need to implement DBContext or ObjectContext.

    UnitOfWork segregates all transactions regardless of the system. EF is only for DB connection. Even if your system is only using DB still it is better to keep a separate UnitOfWork class for future expansions.

    And inside the unit of work Commit(), you can call the internally implemented DBContext.SaveChanges().

    This DBcontext will be accessible to all repositories declared inside unitofwork. So repositories add or delete from DBcontext and unitOfwork commits it.

    When you have scenarios spanning different storages eg: Cloud Blobs, table storage etc. You could implement them inside UnitofWork just like you implemented a EF context. And some repositories can access Table Storage and some EF context.

    Tip: Implementing ObjectContext instead of DBContext gives you an edge in caching scenarios. And you have more options in extending your framework.

提交回复
热议问题