2015-05-08 68 views
1

我最近發現了一個我之前沒有遇到的有關依賴關係的問題,並且查找它並找到Ninject。我遵循了一個關於如何使用它的指南,並且已經達到了我收到的以及我不明白的錯誤。我非常普遍寫下了錯誤的標題,但完整的錯誤如下:存儲庫不能用作通用類型方法中的類型參數

「錯誤1型‘MyDBFirstAP.Repository.SQLAPRepository在泛型類型’不能 用作類型參數‘TImplementation’或方法 'Ninject.Syntax.IBindingToSyntax.To()'。有 沒有從 '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 
    } 
} 

能有人幫我明白這個錯誤,並幫助我解決它的問題即謝謝。

+0

您可以發佈SQLAPRepository代碼嗎? –

+0

當然,我已經發布了它。 –

回答

5

SQLAPRepository必須實現IAPRepository。

public class SQLAPRepository : IAPRepository 
{ 
    .... 
} 
+1

耶穌。這麼簡單,不敢相信我沒有看到。謝謝 –

+0

還有一件事,你可以請嘗試向我解釋錯誤嗎? –

+0

出現錯誤意味着運行時不知道如何將具體實現轉換爲接口實現。在像'int'到'double'的基元之間存在隱式轉換,這意味着我可以在不編寫專用代碼的情況下將'int'轉換爲'double'。如果你想隱式轉換,你必須重載運算符https://msdn.microsoft.com/en-us/library/85w54y0a.aspx或讓你的類成爲基類的後代或實現相關接口。 – mckeejm

0

由於Radin說它必須實現IAPRepository接口。當你進行依賴注入時,你允許在運行時使用任何接口的實現。對於生產代碼,可以在運行時顯式配置映射或某種可用實現的詢問。

NancyFX TinyIoC被使用,並且它不需要顯式類型映射。對於其他解決方案,例如Unity,有許多實現的顯式類型映射container.RegisterType<IMyInterface,MyImplementation>();

+1

謝謝:)我明白。對此,我真的非常感激 –

相關問題