Unity 和 Web 服务
我有这个很棒的想法...但恐怕这实际上是一个坏主意...
我们使用 Unity 进行依赖注入。
我使用部分类从我的 Web 服务创建接口,以进行模拟和 Web 服务......
我想做的是将我的 Web 服务统一起来并通过依赖项注入获取它们......
你觉得怎么样?某处是否有太多开销?内存泄漏?这是一个坏主意吗?
I had this awesome idea... but I am afraid maybe it is actually a bad idea....
we use unity for dependency injection.
I make interfaces from my web services using partial classes for the purpose of mocking and web services....
What I want to do is put my web services into unity and get them via dependency injection...
What do you think? Is there too much overhead somewhere? Memory leaks? Is this a bad idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请记住:不要使用 DI 容器来查找服务 - 它是 服务定位器反模式。相反,使用构造函数注入将您的依赖项放入您的消费者中。
您还可以使用 Web 服务来执行此操作 - 至少使用 WCF 可以 使用构造函数注入来实现服务。
这意味着您可以将所有必要的依赖项注入到您的 Web 服务中,并让 Unity 为您解决所有问题。这才是正确的做法。这根本不是一个坏主意。
为了防止内存泄漏,您必须记住在使用后释放每个已解析的实例。然而,这是微不足道的(此处包含的链接之一显示了如何做到这一点)。
Remember: don't use a DI Container to look up services - it's the Service Locator anti-pattern. Instead, use Constructor Injection to get your dependencies into your consumers.
You can also do this with web services - at least with WCF it's possible to use Constructor Injection for the service implementation.
This means that you can inject all necessary dependencies into your web service and let Unity resolve them all for you. That would be the correct approach. That's not a bad idea at all.
To prevent memory leaks you must remember to Release each resolved instance after use. That is, however, trivial (one of the included links here shows how to do that).
我认为通过 Unity 获取 Web 代理类值得一试。我可以预见您的模拟测试将变得更容易配置和执行。事实上,我想得越多,这个想法似乎就越可靠,值得付出努力。
I think it's worth a shot to get your web proxy classes via unity. I can envision your mock tests becoming a lot easier to configure and execute. Actually, the more I think about it, the idea seems pretty solid and worth the effort.