【问题标题】:Ninject Generic Repository in MVCMVC 中的 Ninject 通用存储库
【发布时间】:2019-11-21 02:22:18
【问题描述】:

如您所见,我有一个带有其 imp 的通用存储库接口:

  public interface IEntityRepository<T> where T : class
{

    IQueryable<T> GetAll();
    IQueryable<T> FindBy(Expression<Func<T, bool>> predicate);
    bool Add(T entity);
    bool Delete(T entity);
    bool Edit(T entity);

}

它的含义

 public abstract class Repository<C, T> :
       IEntityRepository<T> where T : class where C : WelfateContext, new()
    {

        private C _entities = new C();
        public C Context
        {

            get { return _entities; }
            set { _entities = value; }
        }

        public virtual IQueryable<T> GetAll()
        {

            IQueryable<T> query = _entities.Set<T>();
            return query;
        }

        public IQueryable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
        {

            IQueryable<T> query = _entities.Set<T>().Where(predicate);
            return query;
        }

        public virtual bool Add(T entity)
        {
            _entities.Set<T>().Add(entity);
            _entities.SaveChanges();
            return true;
        }

        public virtual bool Delete(T entity)
        {
            _entities.Set<T>().Remove(entity);
            _entities.SaveChanges();
            return true;
        }

        public virtual bool Edit(T entity)
        {
            _entities.Entry(entity).State = EntityState.Modified;
            _entities.SaveChanges();
            return true;
        }

    }

如您所见,我在控制器中使用了它:

private IEntityRepository<Employee> employeeService;

    public EmployeeController(IEntityRepository<Employee> _employeeService)
    {

        _employeeService = employeeService;
    }

我收到此错误:

Error activating IEntityRepository{Employee} using binding from IEntityRepository{Employee} to Repository{WelfateContext, Employee}
No constructor was available to create an instance of the implementation type.

Activation path:
 2) Injection of dependency IEntityRepository{Employee} into parameter _employeeService of constructor of type EmployeeController
 1) Request for EmployeeController

Suggestions:
 1) Ensure that the implementation type has a public constructor.
 2) If you have implemented the Singleton pattern, use a binding with InSingletonScope() instead.

这是我的 ninject 绑定

 private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<WelfateContext>().To<WelfateContext>().InRequestScope();
            kernel.Bind<IEntityRepository<Employee>>().To<Repository<WelfateContext, Employee>>();
            kernel.Bind<IEmployeeService>().To<EmployeeService>().InRequestScope();
            kernel.Bind<IEmployeeDomainService>().To<EmployeeDomainService>().InRequestScope();

        }

【问题讨论】:

  • 您的存储库实现中没有构造函数。您还可以使用您的声明来实例化您的上下文。添加一个在上下文中传递的构造函数。

标签: asp.net-mvc generics model-view-controller ninject


【解决方案1】:

将构造器添加到存储库

   private readonly C _entities;
   public Repository(C context)
   {
     _entities = context;
   }

那么你不需要这段代码

 public C Context
        {

            get { return _entities; }
            set { _entities = value; }
        }

【讨论】:

    猜你喜欢
    • 2011-05-21
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 2012-08-11
    • 2011-07-16
    • 2016-09-26
    相关资源
    最近更新 更多