我应该如何为 Unity 和 AutoMapper 实现 MVC Bootstrapper?

发布于 2024-09-25 11:05:34 字数 1046 浏览 4 评论 0原文

为我的 MVC 2 应用程序创建引导程序的最佳方法是什么?我正在使用 Unity 和 AutoMapper,并希望尽可能抽象它们的加载和配置。

一个不错的例子在这里(http://weblogs.asp.net/rashid/archive/2009/02/17/use-bootstrapper-in-your-asp-net-mvc-application- and-reduce-code-smell.aspx ),但是 UnityContainer 实现了 IDisposable,并且在该示例中它永远不会被清理。这(在Bootstrapper中配置Automapper违反了开闭原则? )也是一个不错的例子,但他也没有处理 Unity/Disposable 问题。

这是(http://www.dominicpetifer.co.uk/Blog/42/put-an-ioc-powered-bootstrapper-in-your-asp-net-mvc-application)另一个很好的例子,说明如何做了一个 Bootstrapper,但同样没有解决 Unity/Disposable 问题。

我考虑过将 Bootstrapper 对象保留在静态变量中并使其实现 IDisposable,但这听起来不对。

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.

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 (Configuring Automapper in Bootstrapper violates Open-Closed Principle?) is also a decent example, but he doesn't deal with the Unity/Disposable problem either.

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鱼忆七猫命九 2024-10-02 11:05:34

如果您在 Bootstrapper 类中保留对容器的引用,则可以将其处置在应用程序端。

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();
    }
}

或者您可以从引导程序返回容器,保留对其的引用并将其处置在应用程序端。

public class YourHttpApplication : HttpApplication
{
    private IUnityContainer _container;

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

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

我想这取决于你的喜好。除此之外,问题中列出的任何引导示例都应该是引导应用程序的不错选择。

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();
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文