当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Designing the right ASP.Net mvc repository pattern with EF change tracking?

问题描述:

I'm currently working with ASP.net MVC with repository pattern, the problem arise when we need to audit each entity.

I decided to utilize the change tracking feature which can track down the entity activity, the problem is when we need to involve several repository with it's private context object in one controller which is not supported by entity change tracker.

example :

public SalesOrderController(INumberingRepository NumberRepository, IProductRepository ProductRepository, ISalesOrderRepository SalesOrderRepository)

{

this.productRepository = ProductRepository;

this.salesOrderRepository = SalesOrderRepository;

this.numberRepository = NumberRepository;

}

whereby each repository has it's own context.

is there any reference design so that entity framework change tracker feature can work happily with multiple repository context ?

网友答案:

If you have your context object life cycle set to Per HttpRequest through IoC,then you can perform all of the database calls as one transaction per HttpRequest and per .SaveChanges(). And instead of private context per Repository, inject context into Repositories.

In case of Structuremaps,

   For<DbContext>().LifecycleIs(new HybridLifecycle()).Use(c=> new DbContext());
分享给朋友:
您可能感兴趣的文章:
随机阅读: