How we can use Unit of Work pattern with (Entity Framework 4.1) code first approach? As I know, EF 4.1 has internally implemented the Unit of Work and repository patterns. If we use self tracking and Data Transfer Objects pattern do we also need to implement a Unit Of Work pattern?
-
Nobody can answer your question. We don't know your application or architecture and pattern should be used based on actual needs not because it exists. You can start without it and if you will feel that you really need it you can refactor your code later.– Ladislav MrnkaJul 27, 2011 at 19:12
-
Actually, We used DTO to data transfer from Service layer to UI Layer and inside we use POCO in infrastructure layer. we used automapper to do the conversion.DB Context is in Http Current context, my problem is do we really need UoW pattern to track state changes because EF 4.1 will do it ?– marvelTrackerJul 28, 2011 at 4:56
3 Answers
In response to your first question, you can find an example of the unit of work pattern implemented with EF Code First here: http://www.asp.net/entity-framework/tutorials/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application
If you use web services and stateless things, you need to track user entity state manually because DBContext should be disposable (the recommended usage). In other words, DBContext should be wrapped within a Using Block. The Unit of work pattern helps to reduce the dependency between Entity Framework and your infrastructure layer. This is ideal for stateless implementations like WCF services over DTO.
Check this out, too. This is a decent example of the UoW and Repository patterns in an application.
-
1I agree to some extent. However, reviews were horrible.Check this review series from Ayende. ayende.com/blog/34817/… Jul 28, 2011 at 14:40
-
@marvelTracker, yes I have read the reviews, however, I still feel it is a good example.– DDiVitaJul 29, 2011 at 12:27