简体   繁体   English

在我的asp.net MVC Web应用程序中获取通用存储库+子存储库+ UnitOfWork

[英]Getting Generic Repository + Child repository + UnitOfWork inside my asp.net mvc web application

I am working on an asp.net vmc5 web application that uses Entity framework 6. Now I am trying to get these working :- 我正在使用Entity Framework 6的asp.net vmc5 Web应用程序上工作。现在,我正在尝试使这些工作正常:-

  1. Define a generic repository. 定义通用存储库。

  2. For each DBSet type to create a dedicated repository which will be derived from the generic repository. 为每种DBSet类型创建一个专用存储库,该存储库将从通用存储库派生。

  3. Create an interface for each of the dedicated repository. 为每个专用存储库创建一个接口。

  4. Use UnitOfwork class so that calling multiple repository classes will result in a single transaction generated. 使用UnitOfwork类,以便调用多个存储库类将导致生成单个事务。

I have a DbSet of type SkillType . 我有一个DbSet类型的SkillType So I created the following interface:- 所以我创建了以下界面:

namespace SkillManagementp.DAL
{
public interface ISkillTypeRepository {
}

Then the following generic Repository:- 然后是以下通用存储库:

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...

The following SkillTypeRepository:- 以下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();
        }
    }
}

And finally I created the following UnitOfWork class:- 最后,我创建了以下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);
        }
    }
}

But I am getting these errors:- 但我得到这些错误:

  • I can not define two derived classes for my SkillManagmentRepositry class. 我不能为SkillManagmentRepositry类定义两个派生类。

  • Also I am getting this error inside the SkillTypeRepository:- SkillManagement.DAL.GenericRepository' does not contain a constructor that takes 0 arguments 我也在SkillTypeRepository内部收到此错误:-SkillManagement.DAL.GenericRepository'不包含带有0个参数的构造函数

Rewrite SkillTypeRepository to this: SkillTypeRepository重写为此:

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

    }
 //rest of code etc
}

As mentioned in my comment, your SkillTypeRepository does not have a constructor, but the GenericRepository does, as the base class has a constructor you need to provide one in the derived class and call :base(params) see here for more details. 如我的评论中所述,您的SkillTypeRepository没有构造函数,但GenericRepository却有,因为基类具有构造函数,您需要在派生类中提供一个构造函数并调用:base(params) 以了解更多详细信息。

You can then simply call base.context to obtain a reference to the SkillManagementEntities . 然后,您可以简单地调用base.context以获得对SkillManagementEntities的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在ASP.NET MVC 5中使用具有通用存储库模式UnitOfWork的继承接口的方法 - Using Method of Inherited Interface with Generic Repository Pattern, UnitOfWork in ASP.NET MVC 5 将项目插入到UnitofWork存储库-不保存对象-ASP.Net MVC - Inserting items to UnitofWork Repository - objects not saving - ASP.Net MVC 标准化存储库,UnitOfWork,IOC容器Asp.Net MVC - Standardize Repository, UnitOfWork, IOC Container Asp.Net MVC ASP.NET MVC通用存储库 - ASP.NET MVC generic repository ASP.NET 5 MVC 6通用存储库模式 - ASP.NET 5 MVC 6 Generic Repository Pattern 在 MVC5 ASP.Net 应用程序中使用 Repository 和 UnitOfWork 模式时,如何修改数据库初始值设定项 Seed 方法? - How do modify database initializer Seed method when using Repository and UnitOfWork patterns in MVC5 ASP.Net application? 应用程序设计:NH会话管理,通用存储库,ASP.NET MVC - Application design: NH-session management, generic repository, ASP.NET MVC ASP.NET MVC通用存储库的实现和用法 - ASP.NET MVC generic repository implementation and usage 为我的asp.net mvc Web应用程序提供一个Repository类。 如何确保我正确处理所有东西 - Provide a Repository class for my asp.net mvc web application. How to make sure i am disposing eveything correctly 实现通用存储库和UnitOf Work - Implementing Generic Repository and UnitOfWork
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM