在我的asp.net mvc Web应用程序中获取Generic Repository + Child repository + UnitOfWork

时间:2015-01-14 15:58:53

标签: c# entity-framework entity-framework-6 unit-of-work

我正在使用一个使用Entity framework 6的asp.net vmc5 Web应用程序。 现在我想让这些工作: -

  1. 定义通用存储库。

  2. 为每个DBSet类型创建一个专用存储库,该存储库将从通用存储库中派生。

  3. 为每个专用存储库创建一个接口。

  4. 使用UnitOfwork类,以便调用多个存储库类将导致生成单个事务。

  5. 我的DbSet类型为SkillType。 所以我创建了以下界面: -

    namespace SkillManagementp.DAL
    {
    public interface ISkillTypeRepository {
    }
    

    然后是以下通用存储库: -

    namespace SkillManagement.DAL
    {
        public class GenericRepository<TEntity> where TEntity : class
        {
            internal SkillManagementEntities context;
            internal DbSet<TEntity> dbSet;
    
            public GenericRepository(SkillManagementEntities context)
            {
                this.context = context;
                this.dbSet = context.Set<TEntity>();
            }//code goes here...
    

    以下SkillTypeRepository: -

    namespace SkillManagement.DAL
    {
        public class SkillTypeRepository :  GenericRepository<SkillType> : ISkillTypeRepository 
        {
            private SkillManagementEntities db = new SkillManagementEntities();
    
            public void Dispose()
            {
                db.Dispose();
    
            }
            public void Save()
            {
                db.SaveChanges();
            }
        }
    }
    

    最后我创建了以下UnitOfWork类: -

    namespace SkillManagement.DAL
    {
        public class UnitOfWork : IDisposable
        {
            private SkillManagementEntities db = new SkillManagementEntities();
            private SkillTypeRepository skillTypeRepository;
    
    
            public SkillTypeRepository SkillTypeRepository
            {
                get
                {
    
                    if (this.skillTypeRepository == null)
                    {
                        this.skillTypeRepository = new SkillTypeRepository();
                    }
                    return skillTypeRepository;
                }
            }
    
    
    
            public void Save()
            {
                db.SaveChanges();
            }
    
            private bool disposed = false;
    
            protected virtual void Dispose(bool disposing)
            {
                if (!this.disposed)
                {
                    if (disposing)
                    {
                        db.Dispose();
                    }
                }
                this.disposed = true;
            }
    
            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
        }
    }
    

    但是我收到了这些错误: -

    • 我无法为我的SkillManagmentRepositry类定义两个派生类。

    • 此外,我在SkillTypeRepository中收到此错误: - SkillManagement.DAL.GenericRepository&#39;不包含带0参数的构造函数

1 个答案:

答案 0 :(得分:1)

SkillTypeRepository重写为:

public class SkillTypeRepository :  GenericRepository<SkillType>, ISkillTypeRepository 
{            
    public SkillTypeRepository() : base(new SkillManagementEntities())
    {

    }
 //rest of code etc
}

正如我的评论中所提到的,你的SkillTypeRepository没有构造函数,但GenericRepository没有,因为基类有一个构造函数,你需要在派生类中提供一个构造函数并调用{{ 1}}有关详细信息,请参阅here

然后,您只需致电:base(params)即可获得对base.context

的引用