Autofac 将参数传递给嵌套类型
我在 WCF 服务中使用 Autofac 作为 IoC。我有一种情况,我想将对象传递给嵌套类型(即不直接解析的类型,但在解析另一个类型时)。据我了解,将此对象作为构造函数参数传递是 Autofac 中的首选方式。这是这种情况的一个例子。
嵌套类型:
public class EventLogger<T> : IEventLogger<T>
{
public EventLogger(IRepository<T> repository, User currentUser) { ... }
}
我实际尝试解析的类型:
public class SomeBusinessObject
{
public SomeBusinessObject(IEventLogger<SomeLogEventType> logger, ...) { ... }
}
注册:
var builder = new ContainerBuilder();
builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>));
builder.RegisterGeneric(typeof(EventLogger<>)).As(typeof(IEventLogger<>));
builder.RegisterType<SomeBusinessObject>();
WCF 服务操作内部的解析:
var currentUser = GetUserFromServiceContext();
var bo = lifetimeScope.Resolve<SomeBusinessObject>();
我应该如何以及在哪里将当前用户传递给我的记录器?我是否应该假设 WCF 操作必须知道解析 SomeBusinessObject 需要首先解析 IEventLogger 并在解析 SomeBusinessObject 时传递已解析的实例?像这样的东西(如果这不起作用,请原谅我,这只是一个想法):
var currentUser = GetUserFromServiceContext();
var logger = lifetimeScope.Resolve<IEventLogger<SomeLogEventType>>(new NamedParameter("currentUser", currentUser));
var bo = lifetimeScope.Resolve<SomeBusinessObject>(new NamedParameter("logger", logger));
如果这是解决方案,如果类型嵌套得更深会发生什么?这是否至少违背了依赖注入的一些目的?
I am using Autofac as my IoC in my WCF service. I have a situation where I want to pass an object to a nested type (ie a type that is not resolved directly, but when resolving another type). As far as I understood, passing this object as a constructor parameter is the preferred way in Autofac. Here is an example of such a situation.
The nested type:
public class EventLogger<T> : IEventLogger<T>
{
public EventLogger(IRepository<T> repository, User currentUser) { ... }
}
The type I am actually trying to resolve:
public class SomeBusinessObject
{
public SomeBusinessObject(IEventLogger<SomeLogEventType> logger, ...) { ... }
}
The registration:
var builder = new ContainerBuilder();
builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>));
builder.RegisterGeneric(typeof(EventLogger<>)).As(typeof(IEventLogger<>));
builder.RegisterType<SomeBusinessObject>();
The resolving inside my WCF service operation:
var currentUser = GetUserFromServiceContext();
var bo = lifetimeScope.Resolve<SomeBusinessObject>();
How and where should I pass the current user to my logger? Should I assume that the WCF operation has to know that resolving SomeBusinessObject requires to resolve IEventLogger first and pass a resolved instance when resolving SomeBusinessObject? Something like this (pardon me if this does not work, it is just an idea):
var currentUser = GetUserFromServiceContext();
var logger = lifetimeScope.Resolve<IEventLogger<SomeLogEventType>>(new NamedParameter("currentUser", currentUser));
var bo = lifetimeScope.Resolve<SomeBusinessObject>(new NamedParameter("logger", logger));
If this is the solution, what happens if the type is nested deeper? Doesn't that defeat at least some of the purpose of dependency injection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
恕我直言,我认为您违反了 IOC 的原则之一,因为组件不需要了解其依赖项的依赖项。在您的情况下,容器不知道
SomeBusinessObject
依赖于User
。话虽这么说,您也许可以利用 Autofac 的委托工厂。您可以手动注册
Func
来隐藏客户端代码中的依赖链详细信息:现在在您的客户端代码中,您可以执行以下操作:
顺便说一句,我认为amba/Func 支持这使得 Autofac 成为最好的 IOC 容器。如果您知道如何编写 Func,您可以做一些疯狂而强大的事情。
IMHO, I think you're violating one of the principles of IOC in that a component should not need to know about the dependencies of it's dependencies. In your case, the container doesn't know that
SomeBusinessObject
has a dependency onUser
.That being said, you may be able to leverage Autofac's Delegate Factories. You could manually register a
Func<User, SomeBusinessObject>
to hide the dependency chain details from the client code:Now in your client code, you can do:
As an aside, I think the lamba/Func support is what makes Autofac the best IOC container. You can do some crazy powerful things if you know how to compose Funcs.