存储库不能用作泛型类型方法

时间:2015-05-08 11:51:42

标签: c# dependency-injection ninject

我最近遇到了一个问题,我之前没有遇到过关于依赖关系的问题,并查找了Ninject。我已经遵循了如何使用它的指南,并达到了我收到的错误和我不理解的错误。我一般在标题中写下错误,但完整错误如下:

  

'错误1类型' MyDBFirstAP.Repository.SQLAPRepository'不可能是   用作类型参数' TImplementation'在泛型类型或方法中   ' Ninject.Syntax.IBindingToSyntax.To()&#39 ;.有   没有隐式引用转换   ' MyDBFirstAP.Repository.SQLAPRepository'至   ' 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
    }
}

有人可以帮我理解这个错误,并帮助我解决它。谢谢。

2 个答案:

答案 0 :(得分:6)

SQLAPRepository必须实现IAPRepository。

public class SQLAPRepository : IAPRepository
{
     ....
}

答案 1 :(得分:0)

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

NancyFX中使用TinyIoC,它不需要显式类型映射。对于像Unity这样的其他解决方案,有许多实现container.RegisterType<IMyInterface,MyImplementation>();

的显式类型映射