Ninject命名为与IGenericRepository绑定 [英] Ninject Named binding with IGenericRepository

查看:170
本文介绍了Ninject命名为与IGenericRepository绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个ASP.NET MVC 4 Web Api,具有C#,.NET Framework 4.0,实体框架代码第6.0和Ninject。



我有两个

这是我的 NinjectConfigurator / code> class(partial):

  private void AddBindings(IKernel container)
{
container.Bind< IUnitOfWork>()。
到< TRZICDbContext>().InRequestScope()。Named(TRZIC);
container.Bind< IUnitOfWork>()。
到< INICDbContext>().InRequestScope()。Named(INIC);

container.Bind< IGenericRepository< CONFIGURATIONS>>()。
到< GenericRepository< CONFIGURATIONS>>();
container.Bind< IGenericRepository< INCREMENTAL_TABLE>>()。
到< GenericRepository< INCREMENTAL_TABLE>>();

//更多实现...
}

CONFIGURATIONS 是一个 TRZIC 表, INCREMENTAL_TABLE 是一个 INIC 表。



我正在使用 IGenericRepository ,这里是我有问题:

  public class GenericRepository< TEntity> :IGenericRepository< TEntity>其中TEntity:class 
{
protected DbSet< TEntity> DbSet;

private readonly DbContext dbContext;

public GenericRepository(IUnitOfWork unitOfWork)
{
dbContext =(DbContext)unitOfWork;
DbSet = dbContext.Set< TEntity>();
}

//隐藏的实现..
}

我不知道如何使用 [Named(TRZIC)] 这里 public GenericRepository(IUnitOfWork unitOfWork)或者可能需要在其他地方使用。



这里 IUnitOfWork 实现取决于TEntity。 p>

任何建议?

解决方案

另一个解决方案:

  private void AddBindings(IKernel container)
{
container.Bind< IUnitOfWork>()。到< TRZICDbContext>()。 InRequestScope();

container.Bind< IGenericRepository< CONFIGURATIONS>>()。
到< GenericRepository< CONFIGURATIONS>>();
container.Bind< IGenericRepository< INCREMENTAL_TABLE>>()。
到< GenericRepository< INCREMENTAL_TABLE>>()。WithConstructorArgument(unitOfWork,new INICDbContext());

//更多代码..
}

我有使用 WithConstructorArgument 表示我想使用 INICDbContext



我不知道这是否正确。


I'm developing an ASP.NET MVC 4 Web Api, with C#, .NET Framework 4.0, Entity Framework Code First 6.0 and Ninject.

I have two different DbContext custom implementations to connect with two different databases.

This is my NinjectConfigurator class (partial):

private void AddBindings(IKernel container)
{
    container.Bind<IUnitOfWork>().
       To<TRZICDbContext>().InRequestScope().Named("TRZIC");
    container.Bind<IUnitOfWork>().
       To<INICDbContext>().InRequestScope().Named("INIC");

    container.Bind<IGenericRepository<CONFIGURATIONS>>().
       To<GenericRepository<CONFIGURATIONS>>();
    container.Bind<IGenericRepository<INCREMENTAL_TABLE>>().
    To<GenericRepository<INCREMENTAL_TABLE>>();

    // More implementation...
}

CONFIGURATIONS is a TRZIC table and INCREMENTAL_TABLE is an INIC table.

I'm using a IGenericRepository and here it's where I have the problems:

public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
{
    protected DbSet<TEntity> DbSet;

    private readonly DbContext dbContext;

    public GenericRepository(IUnitOfWork unitOfWork)
    {
        dbContext = (DbContext)unitOfWork;
        DbSet = dbContext.Set<TEntity>();
    }

    // Hidden implementation..
}

I don't know how to use the [Named("TRZIC")] here public GenericRepository(IUnitOfWork unitOfWork) or maybe I need to use it elsewhere.

Here the IUnitOfWork implementation depends on TEntity.

Any advice?

解决方案

Another solution:

private void AddBindings(IKernel container)
{
    container.Bind<IUnitOfWork>().To<TRZICDbContext>().InRequestScope();

    container.Bind<IGenericRepository<CONFIGURATIONS>>().
        To<GenericRepository<CONFIGURATIONS>>();
    container.Bind<IGenericRepository<INCREMENTAL_TABLE>>().
        To<GenericRepository<INCREMENTAL_TABLE>>().WithConstructorArgument("unitOfWork", new INICDbContext());

    // More code..
}

I have used WithConstructorArgument to indicate that I want to use INICDbContext.

I don't know if this is correct or not.

这篇关于Ninject命名为与IGenericRepository绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆