Универсальный репозиторий Ninject в MVC - PullRequest
1 голос
/ 11 июля 2019

У меня есть общий интерфейс репозитория с его импом, как вы можете видеть:

  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.

Вот мой обязательный предмет

 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();

        }

1 Ответ

1 голос
/ 11 июля 2019

Добавьте конструктор в репозиторий

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

, тогда вам не нужен этот код

 public C Context
        {

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