.net 如何使用Ninject约定库绑定到非';没有接口吗?
我试图在与我的应用程序位于同一目录的程序集中扫描一组实现特定基类的组件。我需要作为一种插件式的架构来完成这项工作,因为我的应用程序使用这些类型来填充其他组件 支持扫描本地目录中的程序集,所以我决定试一试 问题是库提供的绑定生成器(.net 如何使用Ninject约定库绑定到非';没有接口吗?,.net,dependency-injection,ninject,.net,Dependency Injection,Ninject,我试图在与我的应用程序位于同一目录的程序集中扫描一组实现特定基类的组件。我需要作为一种插件式的架构来完成这项工作,因为我的应用程序使用这些类型来填充其他组件 支持扫描本地目录中的程序集,所以我决定试一试 问题是库提供的绑定生成器(DefaultBindingGenerator和RegexBindingGenerator)将只将组件绑定到它们实现的接口。它们不会绑定到非接口基类型 如何使用此库按约定绑定到基类,而不是接口 我使用的是-2.2.0.5 我当前基于约定的绑定代码如下所示: Kernel
DefaultBindingGenerator
和RegexBindingGenerator
)将只将组件绑定到它们实现的接口。它们不会绑定到非接口基类型
如何使用此库按约定绑定到基类,而不是接口
我使用的是-2.2.0.5
我当前基于约定的绑定代码如下所示:
Kernel.Scan(x =>
{
x.FromAssembliesMatching("*.dll");
x.WhereTypeInheritsFrom<BaseType>();
// I've tried both DefaultBindingGenerator and RegexBindingGenerator
x.BindWith<DefaultBindingGenerator>();
x.InTransientScope();
});
Kernel.Scan(x =>
{
x.FromAssembliesMatching("*.dll");
x.WhereTypeInheritsFrom<BaseType>();
x.BindWith<BaseTypeBindingGenerator<BaseType>>();
x.InTransientScope();
});
// ...
var myTypes = Kernel.GetAll<BaseType>();
int count = myTypes.Count(); // Didn't return zero!
Kernel.Scan(x=>
{
x、 FromAssembliesMatching(“*.dll”);
x、 其中类型继承自();
//我尝试了DefaultBindingGenerator和RegexBindingGenerator
x、 BindWith();
x、 内窥镜();
});
当我尝试解析组件时,没有返回任何内容:
var myTypes = Kernel.GetAll<BaseType>();
int count = myTypes.Count(); // Always returns zero
var myTypes=Kernel.GetAll();
int count=myTypes.count();//总是返回零
在中,您可以使用
在我拥有的代码库(目前在NuGet-2.2.0.5上的代码库)中,我使用定制的IBindingGenerator
解决了这个问题。我看了一下,写起来相当简单
公共类BaseTypeBindingGenerator:IBindingGenerator
{
私有静态只读类型baseType=typeof(TBase);
public void进程(类型,Func scopeCallback,
IKernel内核)
{
if(type.IsInterface | | | type.isastract | | | type.BaseType!=BaseType)
{
返回;
}
Bind(baseType).To(type).InScope(scopeCallback);
}
}
我是这样用的:
Kernel.Scan(x =>
{
x.FromAssembliesMatching("*.dll");
x.WhereTypeInheritsFrom<BaseType>();
// I've tried both DefaultBindingGenerator and RegexBindingGenerator
x.BindWith<DefaultBindingGenerator>();
x.InTransientScope();
});
Kernel.Scan(x =>
{
x.FromAssembliesMatching("*.dll");
x.WhereTypeInheritsFrom<BaseType>();
x.BindWith<BaseTypeBindingGenerator<BaseType>>();
x.InTransientScope();
});
// ...
var myTypes = Kernel.GetAll<BaseType>();
int count = myTypes.Count(); // Didn't return zero!
Kernel.Scan(x=>
{
x、 FromAssembliesMatching(“*.dll”);
x、 其中类型继承自();
x、 BindWith();
x、 内窥镜();
});
// ...
var myTypes=Kernel.GetAll();
int count=myTypes.count();//没有返回零!
只是对Merlyn答案的更新,我使用的是ninject extensions conventions 3.2,签名有点变化
public class BaseTypeBindingGenerator<TBase> : IBindingGenerator
{
private static readonly Type baseType = typeof (TBase);
public IEnumerable<IBindingWhenInNamedWithOrOnSyntax<object>> CreateBindings(Type type, IBindingRoot bindingRoot)
{
if (type.IsInterface || type.IsAbstract || type.BaseType != baseType)
{
return Enumerable.Empty<IBindingWhenInNamedWithOrOnSyntax<object>>();
}
return new [] { bindingRoot.Bind(baseType).To(type) };
}
}
公共类BaseTypeBindingGenerator:IBindingGenerator
{
私有静态只读类型baseType=typeof(TBase);
公共IEnumerable CreateBindings(类型类型,IBindingRoot bindingRoot)
{
if(type.IsInterface | | | type.isastract | | | type.BaseType!=BaseType)
{
返回可枚举的.Empty();
}
返回新的[]{bindingRoot.Bind(baseType).To(type)};
}
}