简体   繁体   中英

ASP.NET MVC 4 Entity Framework Database First Repository

I've been reading Pro ASP.NET 4 and the demo application they use the Entity Framework code first. In my project I've already got the database. In the book they only deal with one table, then create an ITableNameRepository for it, then pass that into the controller's constructor. Is it even right in the first place to make a repository with a model of your entire database? Should I just pass an instance to each controller's constructor of IMyDataModel ?

There's a clue in the name of the interface. They called it ITableNameRepository rather than IDatabaseNameRepository

In my application I pass in an IUnitOfWork and that has the various repositories as properties, so the controller's all have a similar constructor signature that doesn't need to change when a controller needs to access a new repository.

The DbContext class is itself a combination of the Unit Of Work and Repository patterns with DbSet s acting as repositories and SaveChanges() implementing the unit of work.

The DbContext or IUnitOfWork does mean the controller can access the entire database - but via several repositories not one big one. I use a repository for each table but I have seen many people advocate "aggregate roots"

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