我应该如何实现一个MVC引导程序的统一和AutoMapper? [英] How should I implement an MVC Bootstrapper for Unity and AutoMapper?

查看:170
本文介绍了我应该如何实现一个MVC引导程序的统一和AutoMapper?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是为我的MVC 2的应用程序引导程序的最佳方式?我使用的是团结和AutoMapper并希望抽象的加载和它们的配置尽可能多的。

What is the best way to create a bootstrapper for my MVC 2 app? I'm using Unity and AutoMapper and want to abstract the loading and configuration of them as much as possible.

一个体面的例子是在这里(<一href=\"http://weblogs.asp.net/rashid/archive/2009/02/17/use-bootstrapper-in-your-asp-net-mvc-application-and-reduce-$c$c-smell.aspx\">http://weblogs.asp.net/rashid/archive/2009/02/17/use-bootstrapper-in-your-asp-net-mvc-application-and-reduce-$c$c-smell.aspx
),但UnityContainer实现IDisposable并且在该实施例,从未清理。这(<一个href=\"http://stackoverflow.com/questions/1807298/configuring-automapper-in-bootstrapper-violates-open-closed-principle/1810728#1810728\">http://stackoverflow.com/questions/1807298/configuring-automapper-in-bootstrapper-violates-open-closed-principle/1810728#1810728)也是一个体面的例子,但他并没有与Unity /一次性问题,或者处理。

A decent example is here (http://weblogs.asp.net/rashid/archive/2009/02/17/use-bootstrapper-in-your-asp-net-mvc-application-and-reduce-code-smell.aspx ), but UnityContainer implements IDisposable and in that example it is never cleaned up. This (http://stackoverflow.com/questions/1807298/configuring-automapper-in-bootstrapper-violates-open-closed-principle/1810728#1810728) is also a decent example, but he doesn't deal with the Unity/Disposable problem either.

下面是(<一个href=\"http://www.dominicpettifer.co.uk/Blog/42/put-an-ioc-powered-bootstrapper-in-your-asp-net-mvc-application\">http://www.dominicpettifer.co.uk/Blog/42/put-an-ioc-powered-bootstrapper-in-your-asp-net-mvc-application)如何做一个引导程序,但同样没有解决统一/一次性问题的另一个很好的例子。

Here's (http://www.dominicpettifer.co.uk/Blog/42/put-an-ioc-powered-bootstrapper-in-your-asp-net-mvc-application) another great example of how to do a Bootstrapper, but again doesn't address the Unity/Disposable issue.

我想过在一个静态变量围绕保持我的引导程序对象,并使其实现IDisposable,但是,这并不健全的权利。

I thought about keeping my Bootstrapper object around in a static variable and make it implement IDisposable, but that doesn't sound right.

推荐答案

如果你守在引导程序类的容器的引用,你可以配置它的应用程序结束。

If you keep a reference to the container in the Bootstrapper class you can dispose it on application end.

public static class Bootstrapper
{
    private static IUnityContainer _container;

    public static void Run()
    {
        _container = new UnityContainer();

        // Configure container here...
    }

    public static void Dispose()
    {
        _container.Dispose();
    }
}

public class YourHttpApplication : HttpApplication
{
    protected void Application_Start()
    {
        Bootstrapper.Run();
    }

    protected void Application_End()
    {
        Bootstrapper.Dispose();
    }
}

或者,你可以从你的引导程序返回容器,保持对它的引用,并处理它的应用程序结束。

Or you could return the container from your bootstrapper, keep a reference to it and dispose it on application end.

public class YourHttpApplication : HttpApplication
{
    private IUnityContainer _container;

    protected void Application_Start()
    {
        _container = Bootstrapper.Run();
    }

    protected void Application_End()
    {
        _container.Dispose();
    }
}

取决于你的preference我猜。除此以外,任何在这个问题中列出的引导例子应该是一个很好的选择提供引导您的应用程序。

Depends on your preference I guess. Apart from that, any of the bootstrapping examples listed in the question should be a good pick for bootstrapping your application.

这篇关于我应该如何实现一个MVC引导程序的统一和AutoMapper?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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