简体   繁体   中英

Using Castle Windsor fluent API to register components implementing multiple interfaces

I am building a POC to demonstrate if we can use Castle Windsor along with the Model-View-Presenter patter in a WinForms application.

The design I have is as follows:

public interface IPresenter{...}  
public interface IPresenterOne : IPresenter {...}  
public interface IPresenterN : IPressenter {...}  

public interface IView{...}  
public interface IViewOne : IView {...}  
public interface IViewN : IView {...}

I have created installers for the Presenters and the Views like the following:

    public class PresentersInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(Classes.FromAssemblyContaining(typeof(PresenterOne))
                .BasedOn<IPresenter>()
                .LifestyleSingleton());
        }
    }


    public class FormsInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(Classes.FromAssemblyNamed("MyViews")
                .BasedOn<IView>()
                .LifestyleSingleton());
        }
    }

Now what I would like is to be able to do the same but each view and each presenter to be registered as both the IView and IPresenter respectably AND their specific interface ie the PresenterOne class to be registered as both an IPresenter AND an IPresenterOne .

How can I accomplish this by using the Castle's fluent API without having to register each component one by one?

I am using Visual Studio 2017 and Castle Windsor 4.0.

Thank you.

After much googling, documentation reading and trying out every single option in the API, I was finally able to find the answer:

public class FormsInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(Classes.FromAssemblyNamed("MyViews")
            .BasedOn<IView>()
            .WithServiceAllInterfaces()
            .LifestyleSingleton());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM