[英]How to create Repository Classes in MVC3 (Entity Framework)?
I created a project using MVC3 - Entity Framework . 我使用MVC3-Entity Framework创建了一个项目。 I like to use Repository Pattern together with it. 我喜欢与它一起使用存储库模式。 I am new to repository pattern. 我是存储库模式的新手。 Do I need to create ONE EACH Repository for each Model Class (classes which represent each table in the database) and within each repository do I have to write all the functions which will Insert, Update, Delete and Fetch record ? 我是否需要为每个模型类 (代表数据库中每个表的类)创建一个EACH信息库,并且必须在每个信息库内编写将插入,更新,删除和提取记录的所有功能吗?
You can create common repository which will have common methods, all other repositories will be it's children: 您可以创建具有通用方法的通用存储库,所有其他存储库将是其子代:
public class MyModelRepository : GenericRepository<MyModel>
{
// extend
}
var MyModelRepository = new MyModelRepository();
See this , or google for "Generic Repository" :). 看到这个 ,或谷歌的“通用存储库” :)。 If your don't need extended functionality for some model repository, then you can even not create repository class, instead do something like this: 如果您不需要某些模型存储库的扩展功能,那么您甚至可以不创建存储库类,而是执行以下操作:
var MyModelRepository = new GenericRepository<MyModel>();
Have an interface that represents the common operations between each repository. 有一个表示每个存储库之间通用操作的接口。 Ie Insert, Update, Delete and Fetch: 即插入,更新,删除和提取:
public interface IRepository<T>
{
void Insert(T entity);
void Delete(T entity);
void Update(T entity);
void Fetch(T entity);
}
public class Repository<T> : IRepository<T>
/// your implementation
}
Then in each model you could define the repository to suit the context, for instance: 然后,在每个模型中,您都可以定义存储库以适合上下文,例如:
var repository1 = new Repository<ModelType>(dataContext);
repository1.Insert(obj);
var repository2 = new Repository<DifferentModelType>(dataContext);
repository2.Fetch(objects);
http://www.remondo.net/repository-pattern-example-csharp/ http://www.remondo.net/repository-pattern-example-csharp/
No you don't. 不,你不会。 You can implement a GenericRepository for all your classes and then override it if you need to add functions. 您可以为所有类实现GenericRepository,然后在需要添加函数时覆盖它。 First i am gonna show you the unit of work. 首先,我要向您展示工作单元。 Through this class you can access all the repositories. 通过此类,您可以访问所有存储库。 I have added to this example one generic and one overrided: 我在此示例中添加了一个泛型和一个已覆盖的类:
public class UnitOfWork
{
FBDbContext context = new FBDbContext();
public FBDbContext Context { get { return context; } }
private BlockRepository BlockRepository;
private GenericRepository<Category> CategoryRepository;
#region RepositoryClasses
public IBlockRepository blockRepository
{
get
{
if (this.BlockRepository == null)
this.BlockRepository = new BlockRepository(context);
return BlockRepository;
}
}
public IGenericRepository<Category> categoryRepository
{
get
{
if (this.CategoryRepository == null)
this.CategoryRepository = new GenericRepository<Category>(context);
return CategoryRepository;
}
}
#endregion
public void Save()
{
context.SaveChanges();
}
}
Then you have the generic repository: 然后,您有了通用存储库:
public class GenericRepository<TEntity>
{
internal FBDbContext context;
internal DbSet<TEntity> dbSet;
public GenericRepository(FBDbContext context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}
public virtual TEntity Create()
{
return Activator.CreateInstance<TEntity>();
}
public IQueryable<TEntity> GetAll()
{
return dbSet;
}
//And all the functions you want in all your model classes...
}
and an example when you want to override the generic repository: 还有一个您想覆盖通用存储库的示例:
public class BlockRepository : GenericRepository<Block>
{
public BlockRepository(FBDbContext context) : base(context) { }
public IEnumerable<Block> GetByCategory(Category category)
{
return context.Blocks.Where(r => r.CategoryId == category.Id);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.