Organizing an EF4 data layer?
I am new to Entity Framework 4, and I am wondering, what's the best way to organize my data layer--the code that accesses EF4?
At this point, my data layer is set up like this:
DataStore class: Holds a reference to the EF4 ObjectContext, and contains methods to ope开发者_高级运维n, close, and persist the ObjectContext to storage.
Repository classes: One class for each entity, with methods to create, fetch, and delete entity objects.
Is there a better way to organize this functionality for EF4? I've looked for articles/blogs on the subject, but I'm not finding much. Any suggestions?
I use a generic repository for Entity Framework that makes access very easy. No need to write a separate repository for each entity, just:
MyDataContext ctx = new MyDataContext();
Repository<MyEntity, MyDataContext > myEntityRep = new Repository<MyEntity, MyDataContext>(ctx);
myEntityRep.Add(new MyEntity() {//property settings});
This repository totally abstracts the Entity model, allowing for creating, modifying, and deleting entities.
I prefer the repository classes for each entity type (I know them as DataMapper). One class for all queries easily becomes too monolithic hard to maintain.
精彩评论