开发者

Lost in multitenant Windsor Castle configuration

开发者 https://www.devze.com 2023-03-13 05:28 出处:网络
I use Windsor Castle with IHandlerSelector for multitenant implementation. I have two forms FrmInvoice and a custom FrmInvoiceCustomer1 share same IFrmInvoice interface. I want to switch them with my

I use Windsor Castle with IHandlerSelector for multitenant implementation.

I have two forms FrmInvoice and a custom FrmInvoiceCustomer1 share same IFrmInvoice interface. I want to switch them with my selector class.

public interface IFrmInvoice
{
   void Show();
}

container.Kernel.AddHandlerSelector(
            new FrmInvoiceSelector(
                new Type[] { typeof(IFrmInvoice) }));

Forms are registered with this code:

 container.Register(AllTypes.FromThisAssembly()
                            .Pick()
                            .If(t => t开发者_开发百科.Name.StartsWith("Frm"))
                            .Configure((c => c.LifeStyle.Transient)));

I've my main form with a button with this code:

private void button1_Click(object sender, EventArgs e)
{
    IFrmInvoice form1 = formsFactory.CreateForm<IFrmInvoice>();
    form1.Show();
}

Now I ask: How I can register IFrmInvoice interface into the Windsor container? Is this the right way to do this?

update

I think I'm very close. In this way it works but it register all interfaces used by my classes! There's a better way?

 container.Register(AllTypes.FromAssemblyContaining<IFrmInvoice>()
              .BasedOn(typeof(IFrmInvoice)).WithService.AllInterfaces());


Use a Windsor Installer implementation, for example:

public class SampleInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Kernel.AddHandlerSelector(new InvoiceHandlerSelector());
    }

    public class InvoiceHandlerSelector: IHandlerSelector
    {
        // ...
    }
}

Then install it:

var container = new WindsorContainer().Install(FromAssembly.InDirectory(new AssemblyFilter(...)));


ok I've found a solution:

container.Register(Component.For<IFrmInvoice>().ImplementedBy<IFrmInvoice>());


Ok, now I see.. we are registering in this way:

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

精彩评论

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

关注公众号