简体   繁体   中英

Implementing custom repository in unit of work pattern

I read a lot about the implementions of the unit of work pattern eg 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 or http://gaui.is/how-to-mock-the-datacontext-linq/

They keep a generic base repository class or interface in the unit of work class which works quite well. In practice my repositories need more functionality as the generic interface repository provides.

So my goal is to use custom repositories if neccessary. But I'm not sure how to implement them. I don't want to define them in the unit of work class as seen below,

public class UnitOfWork : IDisposable
{
    private SchoolContext context = new SchoolContext();
    private CustomRepository<Department> departmentRepository;
    private GenericRepository<Course> courseRepository;
 [...]

because I will not able to use the UnitOfWork class again. I think the sample code above contradicts the UnitofWork pattern.

Does someone have an idea how to implement a custom repository in a clean way in the unit of work pattern when I build something similar to http://gaui.is/how-to-mock-the-datacontext-linq/ ?

best regards

Generally speaking, in unit of work pattern, only database CRUD operations are interfaced in the generic repository. On top of that, service layer is introduced to manipulate the CRUD operations to business operations. can you justify your decision by adding more operations into generic repository? Should you put them in the service layer?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM