通常的方法是註冊一個名稱每個實現,並使用該名稱加以解決。這是我過去的做法。
要在其安裝程序註冊的插件:
container.Register(
Component.For<MyPlugin>.Named(MyPluginA.ID).ImplementedBy<MyPluginA>());
ID可能是類的名稱,或任何唯一的字符串ID。爲了解決這個問題,你可以讓Windsor爲你實施一個可以接受ID的工廠。定義接口:
public interface IPluginFactory
{
MyPlugin CreatePluginById(String id);
}
定義組件選擇,可以選擇作爲一個構造函數的第一個參數提供的插件的ID:
public class PluginFactorySelector : DefaultTypedFactoryComponentSelector
{
protected override string GetComponentName(MethodInfo method, object[] arguments)
{
return (method.Name.EndsWith("ById") && arguments.Length >= 1 && arguments[0] is string)
? (string) arguments[0]
: base.GetComponentName(method, arguments);
}
}
最後,在你的應用程序的安裝掛鉤這一切.. 。
container.Register(
Component.For<PluginFactorySelector, ITypedFactoryComponentSelector>().LifestyleSingleton(),
Component.For<IPluginFactory>().AsFactory(c => c.SelectedWith<PluginFactorySelector>()));