[英]NInject with Generic interface
我已經定義了一個接口和一個類:
public interface IRepository<T>
{
}
public class RoleRepository:IRepository<Domain_RoleInfo>
{
}
在這里注入:
public RoleService
{
[Inject]
public RoleService(IRepository<Domain_RoleInfo> rep)
{
_roleRep=rep;
}
}
如何使用Ninject執行依賴注入,說明如何綁定?
我已經編寫了一個如下所示的輔助類,它可以與非泛型接口一起工作。但是如何重構它支持如上所述的通用接口?
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();
}
}
}
}
這應該工作: -
Bind(typeof(IRepository<>)).To(typeof(Repository<>));
哪里:-
IRepository <>是以下形式的接口: -
public interface IRepository<T> where T : class
{
//...
}
存儲庫<>是一種形式的類: -
public class Repository<T> : IRepository<T> where T : class
{
//...
}
我希望這有幫助 :-)
這應該有助於實現您的要求。
首先讓我們定義兩個類( InterfaceTypeDefinition
和BindingDefinition
)。
InterfaceTypeDefinition
包含有關具體類型及其接口的信息。 方法IsOpenGeneric
在TypeExtensions
類中定義。
public class InterfaceTypeDefinition
{
public InterfaceTypeDefinition(Type type)
{
Implementation = type;
Interfaces = type.GetInterfaces();
}
/// <summary>
/// The concrete implementation.
/// </summary>
public Type Implementation { get; private set; }
/// <summary>
/// The interfaces implemented by the implementation.
/// </summary>
public IEnumerable<Type> Interfaces { get; private set; }
/// <summary>
/// Returns a value indicating whether the implementation
/// implements the specified open generic type.
/// </summary>
public bool ImplementsOpenGenericTypeOf(Type openGenericType)
{
return Interfaces.Any(i => i.IsOpenGeneric(openGenericType));
}
/// <summary>
/// Returns the service type for the concrete implementation.
/// </summary>
public Type GetService(Type openGenericType)
{
return Interfaces.First(i => i.IsOpenGeneric(openGenericType))
.GetGenericArguments()
.Select(arguments => openGenericType.MakeGenericType(arguments))
.First();
}
}
BindingDefinition
包含有關服務和具體實現之間的綁定的信息。
public class BindingDefinition
{
public BindingDefinition(
InterfaceTypeDefinition definition, Type openGenericType)
{
Implementation = definition.Implementation;
Service = definition.GetService(openGenericType);
}
public Type Implementation { get; private set; }
public Type Service { get; private set; }
}
其次,讓我們實現一個檢索必要信息的擴展方法。
public static class TypeExtensions
{
public static IEnumerable<BindingDefinition> GetBindingDefinitionOf(
this IEnumerable<Type> types, Type openGenericType)
{
return types.Select(type => new InterfaceTypeDefinition(type))
.Where(d => d.ImplementsOpenGenericTypeOf(openGenericType))
.Select(d => new BindingDefinition(d, openGenericType));
}
public static bool IsOpenGeneric(this Type type, Type openGenericType)
{
return type.IsGenericType
&& type.GetGenericTypeDefinition().IsAssignableFrom(openGenericType);
}
}
現在可以使用這些類初始化模塊中的綁定。
public class RepositoryModule : NinjectModule
{
public override void Load()
{
var definitions = Assembly.GetExecutingAssembly().GetTypes()
.GetBindingDefinitionOf(typeof(IRepository<>));
foreach (var definition in definitions)
{
Bind(definition.Service).To(definition.Implementation);
}
}
}
如果您導入Ninject約定擴展,其GenericBindingGenerator
應該能夠幫助您。 它增加了對通用接口的支持。
只是關於FindAndBindInterfaces方法的一個問題:在foreach內部,你對intf變量沒有“閉包”問題嗎? 我仍然不確定我是否理解閉包問題是如何工作的。
無論如何,為了安全起見,我認為你應該將你的foreach改成:
foreach (Type intf in interfaces)
{
var tmp = intf;
Type t = ts.Where(x => x.GetInterface(tmp.Name) != null).FirstOrDefault();
if (t != null)
{
Bind(intf).To(t).InSingletonScope();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.