简体   繁体   English

存储库不能用作方法的通用类型中的类型参数

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

I recently came across a problem which I haven't met before about dependencies and looked it up and found Ninject. 最近,我遇到了一个以前从未遇到过的关于依赖项的问题,并进行了查找,找到了Ninject。 I have followed a guide on how to use it and have reached a point where I receive and error which I do not understand. 我遵循了有关如何使用它的指南,并且达到了我所收到的错误和我不理解的错误。 I very generally wrote down the error in the title but the full error is as follows: 我通常会在标题中写下错误,但完整错误如下:

'Error 1 The type 'MyDBFirstAP.Repository.SQLAPRepository' cannot be used as type parameter 'TImplementation' in the generic type or method 'Ninject.Syntax.IBindingToSyntax.To()'. 错误1类型MyDBFirstAP.Repository.SQLAPRepository不能用作通用类型或方法Ninject.Syntax.IBindingToSyntax.To()中的类型参数“ TImplementation”。 There is no implicit reference conversion from 'MyDBFirstAP.Repository.SQLAPRepository' to 'MyDBFirstAP.Repository.IAPRepository'.' 没有从“ MyDBFirstAP.Repository.SQLAPRepository”到“ MyDBFirstAP.Repository.IAPRepository”的隐式引用转换。

It occurs here: 它发生在这里:

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
    }
}

The beginning of my controller is as: 我的控制器的开头是:

 public class ClientsController : Controller
    {
        IAPRepository repository;

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

Here is the requested SQLRepository code: 这是请求的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
    }
}

Can someone please help me understand this error and also help my fix it please. 有人可以帮助我了解此错误,也请帮助我修复该错误。 Thank you. 谢谢。

SQLAPRepository must implement IAPRepository. SQLAPRepository必须实现IAPRepository。

public class SQLAPRepository : IAPRepository
{
     ....
}

as Radin said it must implement the IAPRepository interface. 正如Radin所说,它必须实现IAPRepository接口。 When you do dependency injection you're allowing any implementation of an interface to be used at runtime. 当您进行依赖注入时,您将允许在运行时使用接口的任何实现。 For production code there is either an explicit configuration mapping or some kind of interrogation of available implementations at runtime. 对于生产代码,在运行时存在明确的配置映射或某种形式的对可用实现的询问。

In NancyFX TinyIoC is used, and it does not require explicit type mapping. NancyFX中使用TinyIoC,它不需要显式的类型映射。 For other solutions like Unity there is an explicit type mapping done for many implementations container.RegisterType<IMyInterface,MyImplementation>(); 对于像Unity这样的其他解决方案,为许多实现container.RegisterType<IMyInterface,MyImplementation>();完成了显式的类型映射container.RegisterType<IMyInterface,MyImplementation>();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通用方法-类型不能用作类型参数 - Generic method - Type cannot be used as Type Parameter 该类型不能用作通用类型或方法中的类型参数 - The type cannot be used as type parameter in the generic type or method 类型&#39;&#39;不能在通用类型或方法中用作类型参数&#39;T&#39; - The type '' cannot be used as type parameter 'T' in the generic type or method 类型“ XXX”不能用作通用类型或方法中的类型参数“ T” - The type 'XXX' cannot be used as type parameter 'T' in the generic type or method 该类型不能用作泛型类型或方法UserStore中的类型参数TRole - The type cannot be used as type parameter TRole in the generic type or method UserStore 类型“ T”不能用作通用类型或方法中的类型参数“ T” - The type 'T' cannot be used as type parameter 'T' in the generic type or method 不能在泛型类型或方法中用作类型参数“TElement” - Cannot be used as type parameter 'TElement' in the generic type or method 当类型已知时,类型&#39;T&#39;不能用作泛型类型或方法错误中的类型参数 - The type 'T' cannot be used as type parameter in the generic type or method error when type is known Xamarin / Realm-该类型不能用作通用类型或方法中的类型参数“ T” - Xamarin/Realm - The type cannot be used as type parameter 'T' in the generic type or method 类型&#39;System.Windows.Forms.GroupBox&#39;不能在泛型类型或方法中用作类型参数&#39;T&#39; - The type 'System.Windows.Forms.GroupBox' cannot be used as type parameter 'T' in the generic type or method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM