Installer的几种安装方式

时间:2022-01-23 07:50:52

  当使用依赖注入容器时,你首先要向容器中注册你的组件,Windsor使用installers(该类型实现IWindsorInstaller接口)来封装和隔离注册的逻辑,可以使用Configuration和FromAssembly来完成工作。

  Installers是实现了IWindsorInstaller接口的简单类型,,只有一个Install方法,该方法接收container参数,该参数使用 fluent registration API方式来注册组件

  

public class RepositoriesInstaller : IWindsorInstaller { public void Install(IWindsorContainer container, IConfigurationStore store) { container.Register(AllTypes.FromAssemblyNamed("Acme.Crm.Data") .Where(type => type.Name.EndsWith("Repository")) .WithService.DefaultInterfaces() .Configure(c => c.LifeStyle.PerWebRequest)); } }

备注:使用单独的installer来注册一些相关的服务信息(如repositories、controllers)

installer必须是公共的而且包含一个公共的默认构造函数,Windsor使用InstallerFactory扫描公开的installers

使用installers

var container = new WindsorContainer(); container.Install( new ControllersInstaller(), new RepositoriesInstaller(), // and all your other installers );

当有新的intallers,要记得在这里注册

如果installers过多,添加这些代码有点乏味。Windsor提供FromAssembly静态类和Configuration来进行外部的配置

FromAssembly静态类会扫描指定的程序集中所有实现IWindsorInstaller接口的类,如果添加新的installer,则不用添加额外代码,Windsor会自动检测到类型并实例化

container.Install( FromAssembly.This(), FromAssembly.Named("Acme.Crm.Bootstrap"), FromAssembly.Containing<ServicesInstaller>(), FromAssembly.InDirectory(new AssemblyFilter("Extensions")), FromAssembly.Instance(this.GetPluginAssembly()) );

installer的注册没有特定的顺序,所以你不知道哪个installer先被注册,如果要进行特殊的排序,使用InstallerFactory来实现

public class WindsorBootstrap : InstallerFactory { public override IEnumerable<Type> Select(IEnumerable<Type> installerTypes) { var retval = installerTypes.OrderBy(x => this.GetPriority(x)); return retval; } private int GetPriority(Type type) { var attribute = type.GetCustomAttributes(typeof(InstallerPriorityAttribute), false).FirstOrDefault() as InstallerPriorityAttribute; return attribute != null ? attribute.Priority : InstallerPriorityAttribute.DefaultPriority; } } [AttributeUsage(AttributeTargets.Class)] public sealed class InstallerPriorityAttribute : Attribute { public const int DefaultPriority = 100; public int Priority { get; private set; } public InstallerPriorityAttribute(int priority) { this.Priority = priority; } } container.Install(FromAssembly.This(new WindsorBootstrap()));

给installer添加特性进行排序

1. FromAssembly.This() 调用该方法的程序集
2.
FromAssembly.Named("Acme.Crm.Bootstrap")
Install from assembly with specified assembly name using standard .NET assembly locating mechanism.
  You can also provide path to a .dll or .exe file when you have the assembly in some non-standard location.
如果是.dll,要使用绝对路径
3.
FromAssembly.Containing
程序集中包含特定的类型 Configuration 类

读取xml文件的配置信息

container.Install( Configuration.FromAppConfig(), Configuration.FromXmlFile("settings.xml"), Configuration.FromXml(new AssemblyResource("assembly://Acme.Crm.Data/Configuration/services.xml")) );

settings.xml文件格式如下

<?xml version="1.0" encoding="utf-8" ?> <configuration> <installers> <install type="WindsorInstaller.CustomerInstaller,WindsorInstaller"/> <install type="WindsorInstaller.SecondInstaller,WindsorInstaller"/> </installers> </configuration>

Windsor自动获取xml文件中的installers

也可以写在应用程序配置文件中