Ada*_*art 8 dependency-injection castle-windsor inversion-of-control c#-4.0
我一直在构建一个新的.NET解决方案,而Castle正在执行我的DI.
它现在处于我想控制我的安装程序运行顺序的阶段.我已经构建了单独的类来实现IWindsorInstaller来处理我的核心类型 - 例如IRepository,IMapper和IService等等.
我看到它建议我在这个类中实现我自己的InstallerFactory(猜测我只是覆盖Select).
然后在我的电话中使用这个新工厂:
FromAssembly.InDirectory(new AssemblyFilter("bin location"));
Run Code Online (Sandbox Code Playgroud)
我的问题 - 覆盖保存方法时 - 强制安装程序顺序的最佳方法是什么.
Jon*_*ved 21
我知道它已经解决但我找不到任何关于如何实际实现InstallerFactory的例子,所以这里是一个解决方案,如果有人在谷歌上搜索它.
如何使用:
[InstallerPriority(0)]
public class ImportantInstallerToRunFirst : IWindsorInstaller
{
public void Install(IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
{
// do registrations
}
}
Run Code Online (Sandbox Code Playgroud)
只需将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;
}
}
Run Code Online (Sandbox Code Playgroud)
启动应用程序时,global.asax等:
container.Install(FromAssembly.This(new WindsorBootstrap()));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1653 次 |
| 最近记录: |