Service layer and repositories performance question
i have multiple repositories in my project for different entities and each has it's own datacontext (which comes from a base class for my repositories).
In my service layer, there are many times that the business logic requires me to add/开发者_如何学编程edit objects in multiple repositories (hence inturn using different DataContext).
So when i end up doing .SaveContext on multiple repositories. Which seems like a bad thing to do as ideally i should be able to hit Save on one context and one ado.net call to the DB should perform all the tasks.
What am i doing wrong here?
Thanks
You need to look at using the Unit of Work pattern with your repositories, as it allows your multiple repositories to take part in a single logical transaction.
there are plenty of blog posts around the net about how to do this, including;
- Using Repository and Unit of Work patterns with Entity Framework 4.0
- ASP.NET MVC, DataContext and The Unit of Work Pattern
- The Unit Of Work Pattern And Persistence Ignorance
Also here is a good tutorial walking through using MVC and EF with a Unit of Work + Repository pattern.
As here is a nice diagram explaining how it works;
精彩评论