开发者

Castle Windsor auto registration

I currently have the following registration set up

private static void AddFrameworkComponentsTo(IWindsorContainer container)
{
    container.AddComponent<ITypeConverter, TypeConversionFacade>();
    container.AddComponent<Framework.Conversion.ITypeConverter<string, int>, StringConverter>();
    container.AddComponent<Framework.Conversion.ITypeConverter<string, decimal>, StringConverter>();
    container.AddComponent<Framework.Conversion.ITypeConverter<string, DateTime>, StringConverter>();
}

What's the easiest way to avoid having to register each interface to the same component? As you can see, my "StringConverter" class implements several different interfaces, and the list is likely to grow.

Edit I've just realised that the above doesn't even work be开发者_C百科cause complains about the key, so had to change it to below:

container.AddComponent<Framework.Conversion.ITypeConverter<string, int>, StringConverter>("ITypeConverter<string, int>");
container.AddComponent<Framework.Conversion.ITypeConverter<string, decimal>, StringConverter>("ITypeConverter<string, decimal>");
container.AddComponent<Framework.Conversion.ITypeConverter<string, DateTime>, StringConverter>("ITypeConverter<string, DateTime>");

What's the best way to handle this?


How about:

container.Register(Component.For(typeof(StringConverter).GetInterfaces())
                            .ImplementedBy(typeof(StringConverter)));

See the fluent API wiki for more information on fluent registration.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜