我将温莎城堡与IHandlerSelector
一起用于多租户实施。
我有两种表单FrmInvoice
和一个自定义FrmInvoiceCustomer1
共享相同的IFrmInvoice
界面。我想用我的选择器类切换它们。
public interface IFrmInvoice
{
void Show();
}
container.Kernel.AddHandlerSelector(
new FrmInvoiceSelector(
new Type[] { typeof(IFrmInvoice) }));
使用此代码注册表单:
container.Register(AllTypes.FromThisAssembly()
.Pick()
.If(t => t.Name.StartsWith("Frm"))
.Configure((c => c.LifeStyle.Transient)));
我的主表单带有一个带有此代码的按钮:
private void button1_Click(object sender, EventArgs e)
{
IFrmInvoice form1 = formsFactory.CreateForm<IFrmInvoice>();
form1.Show();
}
现在我问:如何将IFrmInvoice
接口注册到Windsor容器中?这是正确的方法吗?
更新
我想我非常接近。这样它可以工作,但它注册了我的类使用的所有接口!有更好的方法吗?
container.Register(AllTypes.FromAssemblyContaining<IFrmInvoice>()
.BasedOn(typeof(IFrmInvoice)).WithService.AllInterfaces());
答案 0 :(得分:0)
使用Windsor Installer实现,例如:
public class SampleInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Kernel.AddHandlerSelector(new InvoiceHandlerSelector());
}
public class InvoiceHandlerSelector: IHandlerSelector
{
// ...
}
}
然后安装它:
var container = new WindsorContainer().Install(FromAssembly.InDirectory(new AssemblyFilter(...)));
答案 1 :(得分:0)
好的我找到了解决方案:
container.Register(Component.For<IFrmInvoice>().ImplementedBy<IFrmInvoice>());
答案 2 :(得分:0)
好的,现在我明白了......我们正在以这种方式注册:
public class ComponentsInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
var allTypesFromBinDir = AllTypes.FromAssemblyInDirectory(new AssemblyFilter(HttpRuntime.BinDirectory));
container.Register(allTypesFromBinDir
.BasedOn<IComponentService>()
.WithService.FromInterface());
}
}