我如何可以绑定InitializerForXXX
使用Ninject Conventions因此,对于一个IInitializer<T>
请求解决非通用实现,其名称开始(非通用实现)IInitializer<XXX>
(通用接口)与InitializerFor和一端与typeof(T).Name
像:如何绑定使用Ninject约定扩展泛型类型与继承
initializerFactory.CreateFor<Blue>(); //resolves InitializerOfBlue
initializerFactory.CreateFor<ShadeOfBlue>(); //resolves InitializerOfShadeOfBlue
在没有非抽象类直接实现IInitializer<T>
,并且一些实现方式中从其他实现中继承:
InitializerForBlue
InitializerForBlue
继承
InitializerForShadeOfBlue
继承抽象Initializer<Blue>
- 抽象
Initializer<T>
直接实现IInitializer<T>
我希望我可以用一个给定的IInitializer<T>
约定,我可以使用一个.EndsWith(typeof(T).Name)
,因为有几百个初始化程序在ShadeOfxxx静脉中。如果我必须映射它们,我最好找到一种在运行时用反射来解决的方法。
考虑以下几点:
更新:使用自定义绑定发生器绑定(见我的回答如下实施)
void Bootstrap(IBindingRoot kernel)
{
kernel.Bind<IInitializerFactory>()
.To<InitializerFactory>()
.InSingletonScope();
kernel.Bind(scanner =>
scanner.FromThisAssembly().SelectAllClasses()
.WhichAreNotGeneric()
.InheritedFrom(typeof(IComplexContent))
.BindAllInterfaces());
kernel.Bind(scanner =>
scanner.FromThisAssembly().SelectAllClasses()
.WhichAreNotGeneric()
.InheritedFrom(typeof(IInitializer<>))
.BindWith<FirstTypeParameterNameMatchesEndOfBoundClassNameGenerator>());
}
主要方法
void Main(IEnumerable<string> values)
{
// setup bindings
var kernel = new StandardKernel();
Bootstrap(kernel);
IInitializerFactory initializerFactory =
kernel.Get<IInitializerFactory>();
IInitializer<ShadeOfBlueComplexContent> initializer =
initializerFactory.CreateFor<ShadeOfBlueComplexContent>();
initializer.Initialize(values);
}
初始化厂
个interface IInitializerFactory
{
IInitializer<T> CreateFor<T>() where T : class, IComplexContent, new();
}
class InitializerFactory : IInitializerFactory
{
public IInitializer<T> CreateFor<T>() where T : class, IComplexContent, new()
{
return MagicallyGetInitializer<T>();
}
//behind the curtain, whirring noises are heard as 't' is resolved...
private static IInitializer<T> MagicallyGetInitializer<T>()
where T : class, IComplexContent, new()
{
IInitializer<T> i = null;
return i;
}
}
初始化
interface IInitializer<out T> where T : IComplexContent
{
T Initialize(IEnumerable<string> values);
}
abstract class Initializer<T> : IInitializer<T> where T : IComplexContent
{
public abstract T Initialize(IEnumerable<string> values);
}
class InitializerOfBlue : Initializer<Blue>
{
private readonly Blue _content;
public InitializerOfBlue(Blue content) {_content = content;}
public override Blue Initialize(IEnumerable<string> values)
{
_content.BlueSpecificProperty = values.ElementAt(0);
//... populate other blue-specific properties like this
return _content;
}
}
class InitializerOfShadeOfBlue : InitializerOfBlue
{
public InitializerOfShadeOfBlue(ShadeOfBlue content) : base(content){}
}
内容模型
interface IComplexContent
{
string OneBasicProperty { get; set; }
// other properties are specific to implementation
string UniqueOperation();
}
abstract class BaseComplexContent : IComplexContent
{
public string OneBasicProperty { get; set; }
public abstract string UniqueOperation();
}
class Blue : BaseComplexContent
{
// initializer sets this
public string PropertyForAllKindsOfBlue { get; set; }
// initializer doesn't interact with this
public override string UniqueOperation() {return "I'm plain.";}
}
class ShadeOfBlue : Blue
{
// initializer doesn't interact with this
public override string UniqueOperation() {return "I'm fabulous!";}
}
好了,我要在那一刀,然后再发布更新。为此,从AbstractInterfaceBindingGenerator继承还是实现IBindingGenerator更好?我注意到现有的做或者/或者。 – Jeff 2013-03-07 01:18:34
@Lumirris代码(和wiki IIRC)有答案(一般比较喜欢'AbstractInterfaceBindingGenerator',除非你碰到一个障碍,你不能通过实现基本接口或者通过拉请求来避开) – 2013-03-07 09:11:43