使用 IoC 的 ASP.NET MVC 配置类

发布于 2024-10-06 23:11:15 字数 276 浏览 0 评论 0原文

在 MVC 应用程序中,我们需要创建整个应用程序所需的配置设置类。这是一个跨领域的关注点,因为它在控制器中需要,有时在域逻辑深处,以及像 HtmlHelper 扩展这样的地方。事实上,很多不同的地方都需要它,这让我感到困惑。

该类将包装从 web.config 中提取的设置以及数据库中的表。数据库设置查询将被缓存,因此我不担心每个请求都会受到影响。

在过去的几年里,我可能创建了一些静态类型的类或单例,但我不想失去现在拥有的可测试性。实例化此类然后能够通过应用程序中的几乎任何位置访问它的最佳方法是什么?

In an MVC app, we the have need to create a configuration settings class that is needed throughout the app. It is a cross-cutting concern in that it is need in controllers, sometimes deep in the domain logic, as well as place like HtmlHelper extensions. The fact that it's needed is so many different places is what is tripping me up.

The class will wrap settings that are pulled from the web.config, as well as a table in a DB. The DB settings query will be cached so I'm not worried about that getting hit up for every request.

In years past I may have created some static type of class or singleton, but I don't want to lose the testability I have now. What would be the best way to instantiate this class and then to be able to access it through pretty much anywhere in the app?

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

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

发布评论

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

评论(3

莫言歌 2024-10-13 23:11:15

我会继续使用单例。但是包装接口的单例也使其可测试。

public class Configuration
{
    private IConfiguration _config;

    public static IConfiguration Instance { get { return _config; }}

    public static void Assign(IConfiguration config)
    {
       _config = config;
    }
}

只需在 global.asax 或任何单元测试中使用 Assign 即可。

如果您想以正确的方式执行此操作,则应直接在对象的构造函数中提供配置设置。

而不是

public class MyService
{
    public MyService()
    {
        var confString = Configuration.Instance.GetConnectionString()
    }
}

You 会这样做:

public class MyService
{
    public MyService(string confString)
    {}
}

最后,我不会在 HTML 帮助程序中具有任何配置依赖项。通过这样做,您将业务逻辑添加到您的视图中,这打破了关注点分离

I would continue to use a singleton. But a singleton which is wrapping an interface, which also makes it testable.

public class Configuration
{
    private IConfiguration _config;

    public static IConfiguration Instance { get { return _config; }}

    public static void Assign(IConfiguration config)
    {
       _config = config;
    }
}

Simply use Assign in global.asax or any of your unit tests.

If you want to do it the correct way, you should provide the configuration settings directly in the constructors of your objects.

Instead of

public class MyService
{
    public MyService()
    {
        var confString = Configuration.Instance.GetConnectionString()
    }
}

You would do:

public class MyService
{
    public MyService(string confString)
    {}
}

Finally, I would not have any configuration dependencies in HTML helpers. By doing so yuo are adding business logic to your views which breaks separation of concerns

和影子一齐双人舞 2024-10-13 23:11:15

我认为 codeplex 项目 mvccontrib 提供了一些可以使用的钩子
据我所知,至少有 3 个 IOC 提供商,除了 Windsor、structurmap、spring.net...
但我自己没有用过
你可以在这里了解更多信息
http://mvccontrib.codeplex.com/

也许你可以查看这个项目的源代码,看看在哪里你可以从那里去...

HTH

I think the codeplex project mvccontrib provided some hooks to use
at least 3 IOC providers as far as I not windsor, structurmap, spring.net...
but I did not used it myself
you can find out more here
http://mvccontrib.codeplex.com/

and maybe you can look into the sourcecode of this project and see where you can go from there...

HTH

吻泪 2024-10-13 23:11:15

我会重构我的应用程序,不要在任何地方使用配置。我仅在控制器中使用配置。我的视图没有任何逻辑,我的域模型只有业务逻辑,没有应用程序逻辑。

I would refactor my app not to use configuration everywhere. I use configuration in controllers only. My views do not have any logic, my domain model does just have business logic, not application logic.

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