【问题标题】:Getting Generic Repository + Child repository + UnitOfWork inside my asp.net mvc web application在我的 asp.net mvc Web 应用程序中获取通用存储库 + 子存储库 + UnitOfWork
【发布时间】:2015-01-14 15:58:53
【问题描述】:

我正在开发一个使用实体框架 6 的 asp.net vmc5 Web 应用程序。 现在我正试图让这些工作:-

  1. 定义通用存储库。

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

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

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

我有一个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' 不包含采用 0 个参数的构造函数

【问题讨论】:

  • 你的SkillTypeRepository没有构造函数,但是GenericRepository有,也应该是public class SkillTypeRepository : GenericRepository&lt;SkillType&gt;, ISkillTypeRepository
  • 因为基类有一个构造函数,你需要在派生类中提供一个构造函数并调用:base(params),见这里stackoverflow.com/questions/12051/…
  • 那么 SkillTypeRepository 构造函数的方法定义应该是什么?

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


【解决方案1】:

SkillTypeRepository改写为:

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

    }
 //rest of code etc
}

正如我在评论中提到的,您的SkillTypeRepository 没有构造函数,但GenericRepository 有,因为基类有一个构造函数,您需要在派生类中提供一个构造函数并调用:base(params) 参见@987654321 @了解更多详情。

然后您可以简单地调用base.context 来获取对SkillManagementEntities 的引用。

【讨论】:

  • 所以你的意思是我不必在我的 SkillTypeRepository “private SkillManagementEntities db = new SkillManagementEntities();”中定义以下内容而不是这个,我可以简单地调用 base.context??
  • 是的,原因是在您的GenericRepository 中,您已经定义了SkillManagementEntities,无需在SkillTypeRepository 中再次使用它,因为此类可以访问基类字段/属性,除非它们被标记为私有。
  • 但是如果我保持我的代码不变,这会影响调用 .Save() 方法时的单个事务吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-12
  • 1970-01-01
  • 1970-01-01
  • 2019-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多