get Generic CRUD operation in EF
Is there any way or design pattern can I use to get Generic CRUD operations?
Because I’m working on n-tire applica开发者_StackOverflow社区tion using EF in the data layer and I don’t want to use CRUD Functions in Every Entities.
Your help would be appreciated
You can use the Repository pattern, where you implement the repository as an interface and then a base class. For instance:
IRepository where T : class
void Save(T entity )
T FindById( T id )
....
EntityFrameworkRepositoryBase : IRepository
void Save( T entity )
{
// do EF specfic stuff
}....
Then for a given entity you can create(or inject) a concrete repository:
PersonRepository : EntityFrameworkRepositoryBase
From there, simply call the PersonRepository to Save or Find Persons.
精彩评论