Repository不能作为泛型方法中的类型参数

Repository cannot be used as a type parameter in the generic type of method

我最近遇到了一个我以前没有遇到过的关于依赖关系的问题,并查找它并找到了 Ninject。我已按照有关如何使用它的指南进行操作,但已达到我收到的错误和我不理解的错误的地步。我很一般地写下了标题中的错误,但完整的错误如下:

'Error 1 The type 'MyDBFirstAP.Repository.SQLAPRepository' cannot be used as type parameter 'TImplementation' in the generic type or method 'Ninject.Syntax.IBindingToSyntax.To()'. There is no implicit reference conversion from 'MyDBFirstAP.Repository.SQLAPRepository' to 'MyDBFirstAP.Repository.IAPRepository'.'

它出现在这里:

public class NinjectControllerFactory : DefaultControllerFactory{
    private IKernel ninjectKernel;

    public NinjectControllerFactory() {
        ninjectKernel = new StandardKernel();
        AddBindings();
    }
    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) {
        return controllerType == null
            ? null
            : (IController)ninjectKernel.Get(controllerType);
    }
    private void AddBindings()
    {
        ninjectKernel.Bind<IAPRepository>().To<SQLAPRepository>(); // On this line
    }
}

我的控制器的开头是:

 public class ClientsController : Controller
    {
        IAPRepository repository;

        // GET: Clients
        public ClientsController(IAPRepository repository) {
            this.repository = repository;
        }

这里是请求的 SQLRepository 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyDBFirstAP.Models;
using MyDBFirstAP.DI;

namespace MyDBFirstAP.Repository {
    public class SQLAPRepository {
        ApplicationDbContext Database = new ApplicationDbContext();

        #region Client
        public IQueryable<Client> GetAllClients() {
            return Database.Clients;
        }    

        public Client GetClientByID(int id) {
            return Database.Clients.FirstOrDefault(c => c.ClientID == id);
        }

        public IQueryable<Client> GetClientByName(string ClientName) {

            return (from clients in Database.Clients
                    where clients.ClientName.Contains(ClientName)
                    select clients);

        }

        public void AddClient(Client client) {
            Database.Clients.Add(client);
            Database.SaveChanges();
        }

        public void UpdateClient(Client client) {
            var tmpClient = Database.Clients.FirstOrDefault(c => c.ClientID == client.ClientID);
            tmpClient.ClientName = client.ClientName;
            tmpClient.ClientAddress = client.ClientAddress;
            Database.SaveChanges();
        }
        public void DeleteClient(Client client) {
            Database.Clients.Remove(client);
            Database.SaveChanges();
        }
#endregion

        #region Supplier
        public IQueryable<Supplier> GetAllSuppliers() {
            return Database.Suppliers;
        }

        public Supplier GetSupplierByID(int id) {
            return Database.Suppliers.FirstOrDefault(s => s.SupplierID == id);
        }

        public IQueryable<Supplier> GetSupplierByName(string SupplierName) {
            return(from suppliers in Database.Suppliers
                       where suppliers.SupplierName.Contains(SupplierName)
                       select suppliers);
        }

        public void AddSupplier(Supplier supplier) {
            Database.Suppliers.Add(supplier);
            Database.SaveChanges();
        }

        public void UpdateSupplier(Supplier supplier) {
            var tmpSupplier = Database.Suppliers.FirstOrDefault(s => s.SupplierID == supplier.SupplierID);
            tmpSupplier.SupplierName = supplier.SupplierName;
            tmpSupplier.SupplierAddress = supplier.SupplierAddress;
            Database.SaveChanges();
        }

        public void DelteSupplier(Supplier supplier) {
            Database.Suppliers.Remove(supplier);
            Database.SaveChanges();
        }
        #endregion

        #region Claim
        public IQueryable<Claim> GetAllClaims() {
            return Database.Claims;
        }

        public Claim GetClaimByID (int id) {
            return Database.Claims.FirstOrDefault(c => c.ClaimID == id);
        }

        public void AddClaim(Claim claim) {
            Database.Claims.Add(claim);
            Database.SaveChanges();
        }
        public void UpdateClaim(Claim claim) {
            var tmpClaim = Database.Claims.FirstOrDefault(c => c.ClaimID == claim.ClaimID);
            tmpClaim.ClaimTotal = claim.ClaimTotal;
            tmpClaim.ClaimWIP = claim.ClaimWIP;
            tmpClaim.FK_ClientID = claim.FK_ClientID;
            tmpClaim.FK_SupplierID = claim.FK_SupplierID;
            Database.SaveChanges();
        }
        public void DeleteClaim(Claim claim) {
            Database.Claims.Remove(claim);
            Database.SaveChanges();
        }
        #endregion
    }
}

有人可以帮我理解这个错误并帮助我修复它吗?谢谢。

SQLAPRepository 必须实现 IAPRepository。

public class SQLAPRepository : IAPRepository
{
     ....
}

正如 Radin 所说,它必须实现 IAPRepository 接口。当您进行依赖注入时,您允许在运行时使用接口的任何实现。对于生产代码,存在显式配置映射或运行时对可用实现的某种询问。

NancyFX TinyIoC is used, and it does not require explicit type mapping. For other solutions like Unity 中有一个为许多实现完成的显式类型映射 container.RegisterType<IMyInterface,MyImplementation>();