带有 Structuremap 的动态配置

发布于 2024-12-18 01:36:42 字数 737 浏览 3 评论 0原文

这就是我试图用 Structuremap 实现的目标。


对于我们的每个请求,我们的客户端使用的数据库连接字符串和 Web 服务 URL 将根据某些业务逻辑而有所不同。目前,我们的 SQL 和 Web 服务客户端实现在其构造函数中接收配置。

我想使用配置文件,却发现不可能根据请求使用它们。

在我们的团队中,我们正在就两种解决方案进行辩论:


1-将配置工厂传递到注册表中,该注册表可以解析要使用的配置 当容器需要实例化某些东西时。

  • 我看到的问题是我们可能必须使用 HttpContext.Items,因为大多数应用程序对象都没有在结构图中实例化,并且似乎很难从工厂内获取当前请求上下文。


2- 为每个不同的配置实例化容器,并根据业务逻辑决定使用哪个容器。

  • 我看到的问题是加载时间、内存消耗,也许还有对象的生命周期。所以,我在这里似乎没有发现任何真正的问题,我只是觉得拥有多个容器是错误的。



1-您还发现其他问题吗?

2-还有更好的主意吗?

3-你会选择哪一个?


谢谢

编辑

而且似乎很难从工厂内部获取当前的请求上下文。

我不是指 HttpContext,而是指请求数据。对于此应用程序,它是一个 wcf 请求对象。

Here's what I am trying to accomplish with Structuremap.

On each we request, database connection strings and web service urls used in our clients will vary based on some business logic. Currently, our sql and web service client implementations receive the configs in their constructors.

I wanted to use profiles, only to discover that it is not possible to use them per request.

In our team, we're having a debate over two solutions:

1- Pass a config factory into the registry that can resolve which configurations to use
when the container needs to instantiate something.

  • Problems I see is that we might have to use HttpContext.Items, as most of the app objects are not instantiated in structuremap and it seems hard to get the current request context from within the factory.

2- Instantiate containers for every different configurations and decide which container to use depending on the business logic.

  • Problems I see is the load time, the memory consumption and maybe the lifecycles of objects. So, I don't seem to find any real problem here, it just feels wrong to me to have multiple containers.

1- Do you see other problems?

2- Any better idea?

3- Which one would you choose?

Thank you

EDIT

and it seems hard to get the current request context from within the factory.

I don't mean HttpContext, I mean the request data. For this app, it is a wcf request object.

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

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

发布评论

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

评论(1

薄暮涼年 2024-12-25 01:36:42

似乎很难从工厂内部获取当前的请求上下文。

不知道为什么看起来是这样。下面的方法不会成功吗?

 ObjectFactory.Configure(config => {
            config.For<HttpContextBase>()
                .Use(() => { return new HttpContextWrapper(HttpContext.Current); });
            config.For<Service>().Use<Service>();
        });
        var service = ObjectFactory.GetInstance<Service>();

 public class ConfigurationFactory
    {
        public ConfigurationFactory(System.Web.HttpContextBase context)
        {

        }
    }


    public class Service
    {
        public Service(ConfigurationFactory Configuration)
        {

        }
    }

it seems hard to get the current request context from within the factory.

Not sure why it seems that way. Wouldnt the following do the trick?

 ObjectFactory.Configure(config => {
            config.For<HttpContextBase>()
                .Use(() => { return new HttpContextWrapper(HttpContext.Current); });
            config.For<Service>().Use<Service>();
        });
        var service = ObjectFactory.GetInstance<Service>();

 public class ConfigurationFactory
    {
        public ConfigurationFactory(System.Web.HttpContextBase context)
        {

        }
    }


    public class Service
    {
        public Service(ConfigurationFactory Configuration)
        {

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