ASP.Net MVC5和StructureMap4 - 简化的方法 [英] ASP.Net MVC5 and StructureMap4 - Simplified Approach

查看:211
本文介绍了ASP.Net MVC5和StructureMap4 - 简化的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管整合 StructureMap.MVC5 以一个ASP.Net MVC5 Web应用程序,实现了它使用3.1版本的SM和不4+。然后试图采取包括在此的NuGet文件,并改变它SM4,但很多code的在那里,整个SM3.1和SM4几个不兼容的电话。

While integrating StructureMap.MVC5 to an ASP.Net MVC5 web application, realized that it uses 3.1 version of SM and not 4+. Then tried taking the files included in this Nuget and changing it for SM4, but a lot of code was there and several incompatible calls across SM3.1 and SM4.

就这样,我结束了写一个简单的IoC如下。寻找其不足之处,什么告知已经相比,这里链接的版本的NuGet效率低下。

With that, I ended up writing a simple IoC as below. Looking for advise on its shortfalls and what inefficiencies this have compared to the Nuget version linked here.

定义默认的注册表

public class DefaultRegistry : Registry
{
    public DefaultRegistry() {
        Scan(
            scan => {
                scan.Assembly("MyAssembly");
                scan.WithDefaultConventions();
            });

        For<IContext<SomeClass>>().Use<MyContext>();
    }
}

创建静态容器

public static class IoC {
    private static IContainer container = new Container(c => c.AddRegistry<DefaultRegistry>());
    public static IContainer Container { get { return container; } }
}

覆盖控制器工厂

public class StructureMapControllerFactory : DefaultControllerFactory
{
    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        return (IController)IoC.Container.GetInstance(controllerType);
    }
}

注册在Global.asax中

protected void Application_Start()
{
    ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
}

这工作,但担心是过度简化它,这可能引入其他的问题。寻找见解,这种方法的问题。

This works, but worried that am over-simplifying it and this might introduce other issues. Looking for insights into problems with this approach.

推荐答案

有两件事情。你需要这个在DefaultRegistry有控制器的一个更强大的登记:

There's two things. You need this in the DefaultRegistry to have a more robust registration of Controllers:

scan.With(new ControllerConvention());

下面是StructureMap 4 ControllerConvention:

Here is the ControllerConvention for StructureMap 4:

public class ControllerConvention : IRegistrationConvention
{
    public void ScanTypes(TypeSet types, Registry registry)
    {
        foreach (var type in types.AllTypes().Where(type => type.CanBeCastTo<Controller>() && !type.IsAbstract))
        {
            registry.For(type).LifecycleIs(new UniquePerRequestLifecycle());
        }
    }
}

二,使用 DependencyResolver 是preferable在创建自己的 DefaultControllerFactory 。 MVC的实现更丰富,你可以看到<一个href=\"https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Mvc/DefaultControllerFactory.cs\"相对=nofollow>此处。你可以这样就会复制自己,但是这不是面向未来,最重要的,没有必要因为你可以只使用 DependencyResolver ,让您的code简单。基本上,使用 StructuremapMvc StructureMapDependencyScope 您安装StructureMap.MVC5得到的类。你可以初始化和 StructuremapMvc 配置移动到Global.asax中,如果你preFER。

Second, using the DependencyResolver is preferable over creating your own DefaultControllerFactory. MVC's implementation is richer as you can see here. You could copy that to your own but that's not future proof and most of all, not needed because you can just use DependencyResolver, keeping your code simpler. Basically, use the classes StructuremapMvcand StructureMapDependencyScope you get from installing StructureMap.MVC5. You could move the initialization and disposing from StructuremapMvc to the Global.asax if you prefer.

public static StructureMapDependencyScope StructureMapDependencyScope { get; set; }

public static void End()
{
    StructureMapDependencyScope.Dispose();
}

public static void Start()
{
    IContainer container = IoC.Initialize();
    StructureMapDependencyScope = new StructureMapDependencyScope(container);
    DependencyResolver.SetResolver(StructureMapDependencyScope);
    DynamicModuleUtility.RegisterModule(typeof(StructureMapScopeModule));
}

这篇关于ASP.Net MVC5和StructureMap4 - 简化的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆