Asp.net mvc 2 在MVC2项目中从温莎城堡转换为StructureMap

Asp.net mvc 2 在MVC2项目中从温莎城堡转换为StructureMap,asp.net-mvc-2,dependency-injection,castle-windsor,structuremap,Asp.net Mvc 2,Dependency Injection,Castle Windsor,Structuremap,我正在学习MVC2中的最佳实践,并且正在从Codeplex中复制“谁能帮助我”项目()。在里面,他们用温莎城堡作为他们的DI容器。我试图做的一个“学习”任务是将这个项目中的子系统转换为使用StructureMap 基本上,在应用程序_Start()中,代码会更新到Windsor容器。然后,它在componentregistrator.cs中使用MEF遍历多个程序集: public static class ComponentRegistrar { public static void Reg

我正在学习MVC2中的最佳实践,并且正在从Codeplex中复制“谁能帮助我”项目()。在里面,他们用温莎城堡作为他们的DI容器。我试图做的一个“学习”任务是将这个项目中的子系统转换为使用StructureMap

基本上,在应用程序_Start()中,代码会更新到Windsor容器。然后,它在componentregistrator.cs中使用MEF遍历多个程序集:

public static class ComponentRegistrar
{
  public static void Register(IContainer container)
    {
        var catalog = new CatalogBuilder()
                          .ForAssembly(typeof(IComponentRegistrarMarker).Assembly)
                          .ForMvcAssembly(Assembly.GetExecutingAssembly())
                          .ForMvcAssembliesInDirectory(HttpRuntime.BinDirectory, "CPOP*.dll") // Won't work in Partial trust
                          .Build();

        var compositionContainer = new CompositionContainer(catalog);

        compositionContainer
            .GetExports<IComponentRegistrar>()
            .Each(e => e.Value.Register(container));
    }
}

希望我没有太多地屠杀WCHM。我想知道如何使用StructureMap实现这一点?我假设我使用Configure(),因为Initialize()会在每次调用时重置容器?或者,这是一种完全不同的方法吗?我是否需要基于MEF的程序集扫描(用于查找所有注册器并运行每个Register()),或者StructureMap的扫描()中是否有类似的内容?

查看StructureMap的注册表()。要控制生命周期,请使用以下方法:

For<ISomething>().Use<Something>().LifecycleIs(new SingletonLifecycle());

回答我自己的问题,我觉得自己很脏,但我做了以下事情:

public class ControllerRegistrar : IComponentRegistrar
    {
        public void Register(IContainer container)
        {
            container.Configure(x =>
            {
                x.Scan(scanner =>
                {
                    scanner.Assembly(Assembly.GetExecutingAssembly());
                    scanner.AddAllTypesOf<IController>().NameBy(type => type.Name.Replace("Controller", ""));
                });
            });

        }
    }
公共类控制器注册器:IComponentRegistrar
{
公共无效登记簿(IContainer容器)
{
container.Configure(x=>
{
x、 扫描(扫描仪=>
{
scanner.Assembly(Assembly.getExecutionGassembly());
scanner.AddAllTypesOf().NameBy(type=>type.Name.Replace(“Controller”,即“”);
});
});
}
}

我不是100%确定这是对的,但它是有效的。主要是从中的“按名称注册类型”部分提取的。

据我所知,我只能调用Initialize()一次。因此,ObjectFactory.Initialize()必须与ComponentRegister.Register()等效?Scan()将替换MEF代码吗?我没有实现IComponentRegister的Register(),而是创建了多个注册表类?最后,Scan()会扫描多个程序集吗?MEF代码将跨多个项目/程序集进行扫描。我决定保留MEF,以便允许我可能希望Register()做的不仅仅是将项目放入StructureMap容器中。这要求我在回答这个问题时使用代码。
ObjectFactory.Initialize(c => c.Scan(s => {
    s.WithDefaultConventions();
    s.LookForRegistries();
}
public class ControllerRegistrar : IComponentRegistrar
    {
        public void Register(IContainer container)
        {
            container.Configure(x =>
            {
                x.Scan(scanner =>
                {
                    scanner.Assembly(Assembly.GetExecutingAssembly());
                    scanner.AddAllTypesOf<IController>().NameBy(type => type.Name.Replace("Controller", ""));
                });
            });

        }
    }