Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何在MVC3(实体框架)中创建存储库类?_Asp.net Mvc_Asp.net Mvc 3_Entity Framework_Repository Pattern - Fatal编程技术网

Asp.net mvc 如何在MVC3(实体框架)中创建存储库类?

Asp.net mvc 如何在MVC3(实体框架)中创建存储库类?,asp.net-mvc,asp.net-mvc-3,entity-framework,repository-pattern,Asp.net Mvc,Asp.net Mvc 3,Entity Framework,Repository Pattern,我使用MVC3-entityframework创建了一个项目。我喜欢和它一起使用存储库模式。我不熟悉存储库模式。我是否需要为每个模型类(代表数据库中每个表的类)创建一个存储库?我是否需要在每个存储库中编写插入、更新、删除和获取记录的所有函数?您可以创建具有通用方法的通用存储库,所有其他存储库都将是它的子存储库: public class MyModelRepository : GenericRepository<MyModel> { // extend } var MyMod

我使用MVC3-entityframework创建了一个项目。我喜欢和它一起使用存储库模式。我不熟悉存储库模式。我是否需要为每个模型类(代表数据库中每个表的类)创建一个存储库?我是否需要在每个存储库中编写插入、更新、删除和获取记录的所有函数?

您可以创建具有通用方法的通用存储库,所有其他存储库都将是它的子存储库:

public class MyModelRepository : GenericRepository<MyModel>
{
   // extend
}

var MyModelRepository = new MyModelRepository();
公共类MyModelRepository:GenericRepository
{
//延伸
}
var MyModelRepository=new MyModelRepository();
有关“通用存储库”,请参阅或谷歌:。如果您不需要某些模型存储库的扩展功能,那么您甚至可以不创建存储库类,而是执行以下操作:

var MyModelRepository = new GenericRepository<MyModel>();
var MyModelRepository=newgenericrepository();

有一个接口,表示每个存储库之间的常见操作。即插入、更新、删除和获取:

 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
 }
公共接口IRepository
{
无效插入(T实体);
无效删除(T实体);
无效更新(T实体);
无效获取(T实体);
}
公共类存储库:IRepository
///您的实现
}
然后在每个模型中,您可以定义存储库以适应上下文,例如:

  var repository1 = new Repository<ModelType>(dataContext);
  repository1.Insert(obj);

  var repository2 = new Repository<DifferentModelType>(dataContext);
  repository2.Fetch(objects);
var repository1=新存储库(dataContext);
报告1.插入(obj);
var repository2=新存储库(dataContext);
repository2.获取(对象);

不,你没有。您可以为所有类实现GenericRepository,然后在需要添加函数时重写它。首先我要告诉你工作单位。通过该类,您可以访问所有存储库。我在此示例中添加了一个泛型和一个覆盖:

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();
    }

}
公共类UnitOfWork
{
FBDbContext context=新FBDbContext();
公共FBDbContext上下文{get{return Context;}}
私有区块存储库区块存储库;
私人通用存储类别存储;
#区域存储类
公共IBlockRepository块存储库
{
得到
{
if(this.BlockRepository==null)
this.BlockRepository=新的BlockRepository(上下文);
返回区块存储库;
}
}        
公共IGenericRepository类别repository
{
得到
{
if(this.CategoryRepository==null)
this.CategoryRepository=新的通用存储(上下文);
返回类别存储;
}
}        
#端区
公共作废保存()
{
SaveChanges();
}
}
然后您就有了通用存储库:

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...
}
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);
    }        
}
公共类通用存储库
{
内部背景;
内部数据库集;
公共通用存储库(FBDbContext)
{
this.context=上下文;
this.dbSet=context.Set();
}
公共虚拟空间创建()
{
返回Activator.CreateInstance();
}
公共IQueryable GetAll()
{
返回dbSet;
}
//以及所有模型类中需要的所有函数。。。
}
以及一个要覆盖通用存储库的示例:

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...
}
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);
    }        
}
公共类块存储库:GenericRepository
{
公共块存储库(FBDbContext上下文):基本(上下文){}
公共IEnumerable GetByCategory(类别)
{
返回context.Blocks.Where(r=>r.CategoryId==category.Id);
}        
}

我的模型文件夹中有一些表,如-学生、系、课程、讲师等,还有每个表的模型课(Student.cs、Department.cs等)。1.我是否需要创建诸如-IStudernRepositor、StudentRepository、IDepartmentRepository、DepartmentRepository、ICourseRepository、CourseRepository、IInstructorRepository、IInstructorRepository等类。2。插入、删除、选择和更新每个表中数据的函数如何?我必须在每个存储库中编写这些函数吗。例如函数InserStudent,GetStudentAndDepartment()?@user2215116不,我说过,crud操作和基本函数将在通用存储库中,您不必每次都编写它们。非常感谢karaxuna,我看过您文章中的链接。我认为作者没有使用UnitOfWork。UnitOfWork的作用是什么?我应该在哪里使用它?@user2215116看看这个链接:嗨,如何使用存储库类调用存储过程?在我的上下文类-公共部分类MyEntities DbContext中有一些函数。这些函数是为调用StoredProcess而创建的。如何从我的存储库类访问这些函数?