Dependency injection WF统一或IoC/DI的另一种方式

Dependency injection WF统一或IoC/DI的另一种方式,dependency-injection,repository,inversion-of-control,unity-container,Dependency Injection,Repository,Inversion Of Control,Unity Container,各位晚上好。 目前,我是IoC/DI的新人。我有一个用C#的Winform项目。请查看一些代码并帮助我解决以下问题: 1/假定的 namespace Framework.Repositories { public interface IRepository<T> : IDisposable where T:class { T Get(int id); IEnumerable<T> GetAll(); IEnu

各位晚上好。 目前,我是IoC/DI的新人。我有一个用C#的Winform项目。请查看一些代码并帮助我解决以下问题:

1/假定的

namespace Framework.Repositories
{
    public interface IRepository<T> : IDisposable where T:class
    {
        T Get(int id);
        IEnumerable<T> GetAll();
        IEnumerable<T> Find(Expression<Func<T, bool>> predicate);
    }
}
namespace Framework.Repositories
{
    public class Repository<T> : IRepository<T> where T : class
    {
        private bool _disposed = false;
        private static bool _isTransactionStarted;
        protected readonly DbContext Context;
        private DbContextTransaction _transaction { get; set; }

        public Repository(DbContext context)
        {
            Context = context;
            _isTransactionStarted = false;

            if (context == null)
                throw new NullReferenceException(nameof(context));

            if (Context.Set<T>() == null)
                throw new NullReferenceException("_context.Set<T>()");
        }

        public T Get(int id) => Context.Set<T>().Find(id);

        public IEnumerable<T> GetAll() => Context.Set<T>().ToList();

        public IEnumerable<T> Find(Expression<Func<T, bool>> predicate) => Context.Set<T>().Where(predicate);

    }
}
namespace PRO.Repositories
{
    public interface IEmployeeRepository:IRepository<Employee>
    {
        Employee UserLogin(string username, string password);
    }
}
namespace PRO.Repositories
{
    public class EmployeeRepository : Repository<Employee>,IEmployeeRepository
    {
        public EmployeeRepository(DbContext context) : base(context)
        {
        }

        public Employee UserLogin(string username, string password)
        {
            try
            {
                return Find(x=>x.Username == username && x.Password == password && x.EmpStatus == BooleanType.Yes ).FirstOrDefault();
            }
            catch (Exception ex)
            {
                throw new RepositoryException(ex);
            }

        }

        public MyDbContext myDbContext => Context as MinhTamHotelDbContext;
    }
}
4/雇员住所

namespace Framework.Repositories
{
    public interface IRepository<T> : IDisposable where T:class
    {
        T Get(int id);
        IEnumerable<T> GetAll();
        IEnumerable<T> Find(Expression<Func<T, bool>> predicate);
    }
}
namespace Framework.Repositories
{
    public class Repository<T> : IRepository<T> where T : class
    {
        private bool _disposed = false;
        private static bool _isTransactionStarted;
        protected readonly DbContext Context;
        private DbContextTransaction _transaction { get; set; }

        public Repository(DbContext context)
        {
            Context = context;
            _isTransactionStarted = false;

            if (context == null)
                throw new NullReferenceException(nameof(context));

            if (Context.Set<T>() == null)
                throw new NullReferenceException("_context.Set<T>()");
        }

        public T Get(int id) => Context.Set<T>().Find(id);

        public IEnumerable<T> GetAll() => Context.Set<T>().ToList();

        public IEnumerable<T> Find(Expression<Func<T, bool>> predicate) => Context.Set<T>().Where(predicate);

    }
}
namespace PRO.Repositories
{
    public interface IEmployeeRepository:IRepository<Employee>
    {
        Employee UserLogin(string username, string password);
    }
}
namespace PRO.Repositories
{
    public class EmployeeRepository : Repository<Employee>,IEmployeeRepository
    {
        public EmployeeRepository(DbContext context) : base(context)
        {
        }

        public Employee UserLogin(string username, string password)
        {
            try
            {
                return Find(x=>x.Username == username && x.Password == password && x.EmpStatus == BooleanType.Yes ).FirstOrDefault();
            }
            catch (Exception ex)
            {
                throw new RepositoryException(ex);
            }

        }

        public MyDbContext myDbContext => Context as MinhTamHotelDbContext;
    }
}
7/雇员服务

namespace PRO.Services
{
    public interface IEmployeeService
    {
        Employee UserLogin(string username, string password);
    }
}
namespace PRO.Services
{
    public class EmployeeService : IEmployeeService
    {
        public IEmployeeRepository EmployeeRepos { get; set; }


        public Employee UserLogin(string username, string password)
        {
            return EmployeeRepos.UserLogin(username, password);
        }
    }
}
8/DbContext

namespace PRO.Repositories
{
    using System.Data.Entity;

    public partial class MyHotelDbContext: DbContext
    {
        public MyHotelDbContext()
            : base("name=MyHotelDbContext")
        {
        }

        public virtual DbSet<Employee> Employees { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
               //DbContext Here
        }
    }
}
有人能帮我解决这个问题吗? 如果Unity不支持多重继承(EmployeeRepository:Repository,IEEmployeeRepository),有其他方法来代替Unity吗


非常感谢

Unity无法解析
DbContext
。错误消息指出,解析参数“existingConnection”时出现问题,因为DbConnection类型没有可访问的构造函数。从查看您的
DbContext
开始,如果您仍然无法解决问题,请使用代码更新您的帖子。亲爱的Chima Osuji,我可以收到您的电子邮件吗?我的电子邮件是:nvdung0616@gmail.com或skype:anhdungct112
Resolution of the dependency failed, type = "PRO.Services.IEmployeeService", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type DbConnection does not have an accessible constructor.
-----------------------------------------------
At the time of the exception, the container was:

  Resolving PRO.Services.EmployeeService,(none) (mapped from PRO.Services.IEmployeeService, (none))
  Resolving value for property EmployeeService.EmployeeRepos
    Resolving PRO.Repositories.EmployeeRepository,(none) (mapped from PRO.Repositories.IEmployeeRepository, (none))
    Resolving parameter "context" of constructor PRO.Repositories.EmployeeRepository(System.Data.Entity.DbContext context)
      Resolving System.Data.Entity.DbContext,(none)
      Resolving parameter "existingConnection" of constructor System.Data.Entity.DbContext(System.Data.Common.DbConnection existingConnection, System.Data.Entity.Infrastructure.DbCompiledModel model, System.Boolean contextOwnsConnection)
        Resolving System.Data.Common.DbConnection,(none)