我一直在用CaSTLe执行我的DI来构建新的.NET解决方案。
现在是我想要控制安装程序运行顺序的阶段。我建立了单独的类,这些类实现了IWindsorInstaller来处理我的核心类型,例如IRepository,IMapper和IService等。
我看到它的建议是在此类中实现我自己的InstallerFactory(猜测我只是覆盖Select)。
然后在我的电话中使用这个新工厂:
FromAssembly.InDirectory(new AssemblyFilter("bin location"));
我的问题-覆盖save方法时-强制安装程序顺序的最佳方法是什么?
最佳答案
我知道它已经解决了,但是我找不到关于如何实际实现InstallerFactory的任何示例,因此如果有人在搜索它,这是一个解决方案。
使用方法:
[InstallerPriority(0)]
public class ImportantInstallerToRunFirst : IWindsorInstaller
{
public void Install(IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
{
// do registrations
}
}
只需将具有优先级的
InstallerPriority
属性添加到“对安装顺序敏感”的类即可。安装程序将按升序排序。没有优先级的安装程序将默认为100。如何实现:
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;
}
}
启动应用程序时,global.asax等:
container.Install(FromAssembly.This(new WindsorBootstrap()));
关于c#-4.0 - 如何强制执行安装程序的执行顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6358206/