【问题标题】:How to create Repository Classes in MVC3 (Entity Framework)?如何在 MVC3(实体框架)中创建存储库类?
【发布时间】:2013-06-29 14:07:44
【问题描述】:

我使用 MVC3 - 实体框架 创建了一个项目。我喜欢和它一起使用存储库模式。我是存储库模式的新手。我是否需要为每个模型类创建一个每个存储库(代表数据库中每个表的类)并且在每个存储库中我是否必须编写所有将插入、更新、删除并获取记录

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 entity-framework repository-pattern


    【解决方案1】:

    不,你没有。您可以为所有类实现一个 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();
        }
    
    }
    

    然后你就有了通用存储库:

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

    【讨论】:

      【解决方案2】:

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

      public class MyModelRepository : GenericRepository<MyModel>
      {
         // extend
      }
      
      var MyModelRepository = new MyModelRepository();
      

      请参阅this,或在 google 上查看“通用存储库”:)。如果您不需要某些模型存储库的扩展功能,那么您甚至可以不创建存储库类,而是执行以下操作:

      var MyModelRepository = new GenericRepository<MyModel>();
      

      【讨论】:

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

      有一个代表每个存储库之间的通用操作的接口。 IE。插入、更新、删除和获取:

       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
       }
      

      然后在每个模型中,您可以定义存储库以适应上下文,例如:

        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/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-15
        • 2016-04-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多