NInject与通用接口 [英] NInject with Generic interface

查看:88
本文介绍了NInject与通用接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义一个接口和一个类:

 公共接口IRepository< T> 
{
}

公共类RoleRepository:IRepository< D​​omain_RoleInfo>
{
}



注射的位置:

 公共RoleService 
{
[注入]
公共RoleService(IRepository< D​​omain_RoleInfo> REP)
{
_roleRep =代表;
}
}



我怎么能执行依赖注入Ninject,怎么说绑定?



我写了一个辅助类下面,它工作正常与非通用interface.but如何重构它支持使用符合上述通用接口?

 公共类RegisterNinjectModule:NinjectModule 
{
公共覆盖无效的load()
{
BindServices();
BindRepositories();
}

私人无效BindServices()
{

FindAndBindInterfaces(RealMVC.Service.Interfaces,RealMVC.Services);
}

私人无效BindRepositories()
{
FindAndBindInterfaces(RealMVC.Repository.Interfaces,RealMVC.Repositories);
}

私人无效FindAndBindInterfaces(字符串interfaceAssemblyName,串implAssemblyName)
{
//获取所有接口
名单,LT;类型> 。接口=的Assembly.Load(interfaceAssemblyName).GetTypes()AsQueryable已(),其中(X => x.IsInterface)。.ToList();
&IQueryable的LT;类型> 。TS =的Assembly.Load(implAssemblyName).GetTypes()AsQueryable已(),其中(X => x.IsClass);

的foreach(在接口类型INTF)
{
型T = ts.Where(X =>!x.GetInterface(intf.Name)= NULL).FirstOrDefault( );
如果(T!= NULL)
{
绑定(INTF)。为了(T).InSingletonScope();
}
}
}


}


解决方案

这应该工作: -

 绑定(typeof运算( IRepository<>))至(typeof运算(库<>)); 



其中: -



IRepository<> 与形式的接口: -

 公共接口IRepository< T>其中T:类
{
// ...
}

库<> 是一类形式: -

 公共类资源库< T> :IRepository< T>其中T:类
{
// ...
}

我希望这有助于: - )


I have defined one interface and one class:

public interface IRepository<T>
{
}

public class RoleRepository:IRepository<Domain_RoleInfo>
{
}

Inject here:

public RoleService
{
    [Inject]
    public RoleService(IRepository<Domain_RoleInfo> rep)
    {
        _roleRep=rep;
    }
}

How can I perform Dependency Injection With Ninject,say how to bind?

I have written a helper class as below, it works fine with non-generic interface.but how to refactor it support generic interface as above?

public class RegisterNinjectModule : NinjectModule
{
    public override void Load()
    {
        BindServices();
        BindRepositories();
    }

    private void BindServices()
    {

        FindAndBindInterfaces("RealMVC.Service.Interfaces", "RealMVC.Services");            
    }

    private void BindRepositories()
    {
        FindAndBindInterfaces("RealMVC.Repository.Interfaces", "RealMVC.Repositories");   
    }

    private void FindAndBindInterfaces(string interfaceAssemblyName, string implAssemblyName)
    {
        //Get all interfaces
        List<Type> interfaces = Assembly.Load(interfaceAssemblyName).GetTypes().AsQueryable().Where(x => x.IsInterface).ToList();
        IQueryable<Type> ts = Assembly.Load(implAssemblyName).GetTypes().AsQueryable().Where(x => x.IsClass);

        foreach (Type intf in interfaces)
        {
            Type t = ts.Where(x => x.GetInterface(intf.Name) != null).FirstOrDefault();
            if (t != null)
            {
                Bind(intf).To(t).InSingletonScope();
            }
        }
    }


}

解决方案

This should work:-

Bind(typeof(IRepository<>)).To(typeof(Repository<>));

where:-

IRepository<> is an interface of the form:-

public interface IRepository<T> where T : class
{
 //...
}

Repository<> is a class of the form:-

public class Repository<T> : IRepository<T> where T : class
{
  //...
}

I hope this helps :-)

这篇关于NInject与通用接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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