asp.net mvc - 控制器共享对象的最佳方式是什么

发布于 2024-08-01 17:25:26 字数 100 浏览 4 评论 0原文

我正在尝试跨多个控制器共享一些对象。 做这个的最好方式是什么。 我想避免单例,如果可能的话我想注入这些,但使用 ASP.NET MVC 你不会“更新”控制器,所以任何帮助将不胜感激。

i am trying to share some objects across multiple controllers. what is the best way to do this. i want to avoid singletons and i would like to inject these in if possible but with asp.net mvc you are not "newing" the controllers so any help would be appreciated.

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

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

发布评论

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

评论(1

七婞 2024-08-08 17:25:26

让我说一下,当我开始使用 MVC 时,我也遇到了同样的问题。 然后我了解了 IoC(控制反转)容器和依赖注入,这些东西允许你做的事情真是太神奇了。

该项目将 Castle Windsor、NHibernate 和 ASP.NET MVC 集成到一个包中。
http://code.google.com/p/sharp-architecture/

如果如果你想自己做,你可以做的就是选择你最喜欢的 IoC 容器。 有一些适用于 MVC 的绑定,或者您可以自己推出。 如果您想自己实现,则必须实现 IControllerFactory 并设置 MVC 以使用您的工厂。 这是我的。

public class ControllerFactory : IControllerFactory
{
    private readonly IDependencyResolver _controllerResolver;

    private RequestContext RequestContext { get; set; }

    public ControllerFactory(IDependencyResolver controllerResolver)
    {
        _controllerResolver = controllerResolver;
    }

    public void ReleaseController(IController controller)
    {
        _controllerResolver.Release(controller);

        var disposableController = controller as IDisposable;

        if (disposableController != null)
            disposableController.Dispose();
    }


    public IController CreateController(RequestContext requestContext, string controllerName)
    {
        Assert.IsNotNull(requestContext, "requestContext");
        Assert.IsNotNullOrEmpty(controllerName, "controllerName");

        RequestContext = requestContext;

        try
        {
            var controllerInstance = _controllerResolver.Resolve<IController>(controllerName.ToLower() + "controller");
            return controllerInstance;
        }
        catch(Exception ex)
        {
            throw new HttpException(404, ex.Message, ex);
        }
    }

}

Let me say, when I started with MVC, I had the same problem. Then I learned about IoC (Inversion of Control) containers and Dependency Injection, it is so amazing on how much these things allow you to do.

This project integrates Castle Windsor, NHibernate and ASP.NET MVC into one package.
http://code.google.com/p/sharp-architecture/

If you want to do it yourself, what you can do is pick your favorite IoC container. There are some bindings out there for MVC or you can roll your own. If you want to do your own, you have to implement a IControllerFactory and set MVC to use your factory. Here is mine.

public class ControllerFactory : IControllerFactory
{
    private readonly IDependencyResolver _controllerResolver;

    private RequestContext RequestContext { get; set; }

    public ControllerFactory(IDependencyResolver controllerResolver)
    {
        _controllerResolver = controllerResolver;
    }

    public void ReleaseController(IController controller)
    {
        _controllerResolver.Release(controller);

        var disposableController = controller as IDisposable;

        if (disposableController != null)
            disposableController.Dispose();
    }


    public IController CreateController(RequestContext requestContext, string controllerName)
    {
        Assert.IsNotNull(requestContext, "requestContext");
        Assert.IsNotNullOrEmpty(controllerName, "controllerName");

        RequestContext = requestContext;

        try
        {
            var controllerInstance = _controllerResolver.Resolve<IController>(controllerName.ToLower() + "controller");
            return controllerInstance;
        }
        catch(Exception ex)
        {
            throw new HttpException(404, ex.Message, ex);
        }
    }

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