C# 使用ninject的DI、IoC绑定(将接口绑定到IEnumerable<;T>;方法)

C# 使用ninject的DI、IoC绑定(将接口绑定到IEnumerable<;T>;方法),c#,binding,ninject,C#,Binding,Ninject,我有一个界面: public interface IProductRepository { IEnumerable<Product> Products { get; } } 我试图在AddBindings()中将它们绑定在一起{ 我有一个想法: public class productDAL: databaseDAL { public productDAL() { } public IEnumerable<CountyDataset>

我有一个界面:

public interface IProductRepository {
    IEnumerable<Product> Products { get; }
}
我试图在AddBindings()中将它们绑定在一起{

我有一个想法:

public class productDAL: databaseDAL {
    public productDAL() {

    }
    public IEnumerable<CountyDataset> Products {
        get { ... }
    }
}
公共类productDAL:databaseDAL{
公共产品(){
}
公共可数产品{
获取{…}
}
}
这就是我错过的

现在我有了一个产品库(还有朋友哈哈):

公共类lcProducts:IPProductRepository{
private productDAL context=new productDAL();
公共可数产品{
获取{return context.Products;}
}
}
这是我更正的DI容器:

private void AddBindings() {
        ...
        kernel.Bind<IProductRepository>().To<lcProducts>();
    }
private void AddBindings(){
...
kernel.Bind().To();
}
您不能--类型不匹配。您必须绑定到
IPProductRepository
的实例。看起来您的
db
对象实现了该接口,请尝试一下:

kernel.Bind<IProductRepository>().To<productDAL>();
.
.
.
var db = kernel.Get<IProductRepository>();
IEnumerable<Product> help = db.getProducts();
此时,依赖项解析程序如下所示:

Mock<IProductRepostiory> mock = new Mock<IProductRespository>();
mock.Setup(m => m.Products).Returns(new List<Product> {
new Product { Name= ... }

kernel.Bind<IProductRepository>().ToConstant(mock.Object);
kernel.Bind<SomeConsumerOfProductRepository>().ToSelf();
kernel.Bind<Controller>().To<ProductController>();
.
.
.
var consumer = kernel.Get<Controller>();
//Use consumer or do whatever you have to do.... 
kernel.Bind().ToSelf();
kernel.Bind().To();
.
.
.
var consumer=kernel.Get();
//使用消费者或做任何你必须做的事。。。。

这将允许Ninject自动检测到
ProductController
依赖于您绑定到实现的
IPProductRepository
,并为您解决它。此外,如果应该会有需要。

消费者将成为我的ProductController公共类ProductController:Controller{private IPProductRepository repository;public ProductController(IPProductRepository productRepository){this.repository=productRepository;}public ViewResult List(){return View(repository.Products);}}}如果我这样做:kernel.Bind().To();我收到此严重性代码说明项目文件行抑制状态错误CS0311类型“DataLibrary.Dal.productDAL”不能用作泛型类型或方法“IBindingToSyntax.To()中的类型参数“TImplementation”)“。没有从“DataLibrary.Dal.productDAL”到“DataLibrary.Interfaces.IProductRepository”的隐式引用转换。ClerkHelper c:\users\hanka\onedrive\documents\visualstudio 2017\Projects\ClerkHelper\ClerkHelper\Slab\njectdependency解析这是因为
productDAL
类没有实现
IProductRepository
interface。我知道我在尝试这样做:Mock Mock=new Mock();Mock.Setup(m=>m.Products)。Returns(new List{new Product{Name=…}kernel.Bind().ToConstant(Mock.Object);这会起作用。很难回答,因为我觉得这里缺少大量上下文。
public class ProductController : Controller
{
    private IProductRepository repository;

    public ProductController(IProductRepository productRepository) {
        this.repository = productRepository;
    }

    public ViewResult List() {
        return View(repository.Products);
    }
}
public class productDAL: databaseDAL {
    public productDAL() {

    }
    public IEnumerable<CountyDataset> Products {
        get { ... }
    }
}
public class lcProducts : IProductRepository {
    private productDAL context = new productDAL();

    public IEnumerable<CountyDataset> Products {
        get { return context.Products; }
    }
}
private void AddBindings() {
        ...
        kernel.Bind<IProductRepository>().To<lcProducts>();
    }
kernel.Bind<IProductRepository>().To<productDAL>();
.
.
.
var db = kernel.Get<IProductRepository>();
IEnumerable<Product> help = db.getProducts();
public class ProductController : Controller
{
    private IProductRepository repository;

    public ProductController(IProductRepository productRepository) {
        this.repository = productRepository;
    }

    public ViewResult List() {
        return View(repository.Products);
    }
}
kernel.Bind<SomeConsumerOfProductRepository>().ToSelf();
kernel.Bind<Controller>().To<ProductController>();
.
.
.
var consumer = kernel.Get<Controller>();
//Use consumer or do whatever you have to do....