Castle Windsor - Example of using InstallerFactory

三世轮回 提交于 2019-12-23 12:13:11

问题


Does anyone have some sample code of using a castle windsor InstallerFactory for ordering the installation of Installers?

Can't seem to find it in the docs or elsewhere.

Cheers


回答1:


You can only use the InstallerFactory in conjunction with the FromAssembly class.

When using FromAssembly you should not rely on the order in which your installers will be instantiated/installed. It is non-deterministic which means you never know what it's going to be. If you need to install the installers in some specific order, use InstallerFactory.

In addition to that you should inherit from the InstallerFactory class and apply your own rules regarding the instantiation of particular installer types.

All of the above methods have an overload that takes an InstallerFactory instance. Most of the time you won't care about it and things will just work. However if you need to have tighter control over installers from the assembly (influence order in which they are installed, change how they're instantiated or install just some, not all of them) you can inherit from this class and provide your own implementation to accomplish these goals.

Sample class could look like this:

public class CustomInstallerFactory : InstallerFactory
{
    public override IEnumerable<Type> Select(IEnumerable<Type> installerTypes)
    {
        return installerTypes.Reverse(); // just as an example
    }
}

And here is the code for the container initialization:

IWindsorContainer container = new WindsorContainer().Install(FromAssembly.This(new CustomInstallerFactory()));

Hope this helps!



来源:https://stackoverflow.com/questions/9043421/castle-windsor-example-of-using-installerfactory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!