失去多租户温莎城堡配置

时间:2011-06-16 12:13:12

标签: c# winforms inversion-of-control castle-windsor multi-tenant

我将温莎城堡与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());

3 个答案:

答案 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());
    }
}