IOC/Autofac 容器
我目前正在使用 Autofac,但也愿意接受有关其他 IOC 容器的评论。如果可能的话,我更喜欢使用 Autofac 的解决方案。我对 IOC 也有点陌生,所以我可能严重误解了我应该使用 IOC 容器的用途。
基本上,情况如下:
我的应用程序有一个最顶层的 IOC 容器。我有一个子容器/范围树,我希望相同的“服务”(IWhatever)根据树中解析的级别进行不同的解析。此外,如果服务未在树中的某个级别注册,我希望树向上横向,直到找到合适的实现。
此外,在构造给定组件时,我很可能需要访问父容器/范围。在许多情况下,我正在注册的组件将依赖于父范围中的相同或不同的服务。
有什么办法可以用 Autofac 来表达这种依赖关系吗?类似这样的事情:
builder.Register(c=>
{
var parentComponent = ?.Resolve<ISomeService>();
var childComponent = new ConcreteService(parentComponent, args...);
return childComponent;
}).As<ISomeService>();
出于以下几个原因,我无法获得与上述伪代码类似的任何内容:
A)似乎范围树中的所有级别都共享一组通用的注册。我似乎找不到一种方法来使给定的注册限制在一定的“范围”。
B)我似乎找不到一种方法来获取给定范围的父范围。我可以解析容器中的 ILifetimeScope,然后将其封装到提供其父范围的具体 LifetimeScope 实例,但我猜测它可能是要以这种方式使用的。这安全吗?
C) 我不确定如何告诉 Autofac 哪个容器拥有已解析的对象。对于许多组件,我希望组件由其构建的范围“拥有”。标记的上下文可以帮助我吗?我是否必须用唯一的标签来标记树的每一层?这会很困难,因为树深度是在运行时确定的。
很抱歉问了一个非常冗长的问题。总结:
1)有什么办法可以使用 Autofac 来做我想做的事情吗?
2)还有其他容器更适合这种依赖结构吗?
3) IOC 是否完全是一个错误的工具?
I am currently using Autofac, but am open to commentary regarding other IOC containers as well. I would prefer a solution using Autofac if possible. I am also somewhat new to IOC so I may be grossly misunderstanding what I should be using an IOC container for.
Basically, the situation is as follows:
I have a topmost IOC container for my app. I have a tree of child containers/scopes where I would like the same "service" (IWhatever) to resolve differently depending on which level in the tree it is resolved. Furthermore if a service is not registered at some level in the tree I would like the tree to be transversed upward until a suitable implementation is found.
Furthermore, when constructing a given component, it is quite possible that I will need access to the parent container/scope. In many cases the component that I'm registering will have a dependency on the same or a different service in a parent scope.
Is there any way to express this dependency with Autofac? Something like:
builder.Register(c=>
{
var parentComponent = ?.Resolve<ISomeService>();
var childComponent = new ConcreteService(parentComponent, args...);
return childComponent;
}).As<ISomeService>();
I can't get anything similar to the above pseudocode to work for several reasons:
A) It seems that all levels in the scope tree share a common set of registrations. I can't seem to find a way to make a given registration confined a certain "scope".
B) I can't seem to find a way to get a hold of the parent scope of a given scope. I CAN resolve ILifetimeScope in the container and then case it to a concrete LifetimeScope instance which provides its parent scope, but I'm guessing it is probably note meant to be used this way. Is this safe?
C) I'm not sure how to tell Autofac which container owns the resolved object. For many components I would like component to be "owned" by the scope in which it is constructed. Could tagged contexts help me here? Would I have to tag every level of the tree with a unique tag? This would be difficult because the tree depth is determined at runtime.
Sorry for the extremely lengthy question. In summary:
1) Is there any way to do what I want to do using Autofac?
2) Is there another container more suited to this kind of dependency structure?
3) Is IOC the wrong tool for this altogether?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Autofac 1.4 很容易支持这一点,并且可能是目前最好的选择。您只需在子容器中注册子容器组件即可。
您正在使用的 Autofac 2 尚未提供简单的等效项,尽管正在对其进行研究。
Autofac 1.4 supports this easily and is probably the best choice here right now. You can simply register the child container components in the child container.
Autofac 2, which you're using, doesn't have a simple equivalent yet, although it is being investigated.