嵌套类型:
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));
如果这是解决方案,如果类型嵌套得更深,会发生什么?难道这至少打败了依赖注入的一些目的吗?
解决方法 恕我直言,我认为你违反了IOC的一个原则,因为组件不应该知道它的依赖关系的依赖性.在您的情况下,容器不知道SomeBusinessObject依赖于User.话虽这么说,您可以利用autofac的Delegate Factories.您可以手动注册Func< User,SomeBusinessObject>从客户端代码隐藏依赖关系链详细信息:
var builder = new ContainerBuilder();builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>));builder.RegisterGeneric(typeof(EventLogger<>)).As(typeof(IEventLogger<>));builder.RegisterType<SomeBusinessObject>();builder.Register<Func<User,SomeBusinessObject>>(c => { // autofac should be able to resolve these Func<> automatically: var loggerFactory = c.Resolve<Func<User,IEventLogger<SomeLogEventType>>>(); var sboFactory = c.Resolve<Func<IEventLogger<SomeLogEventType>,SomeBusinessObject>>(); // Now we can chain the Funcs: return u => sboFactory(loggerFactory(u));});
现在在您的客户端代码中,您可以:
var currentUser = GetUserFromServiceContext(); var sboFactory = lifetimeScope.Resolve<Func<User,SomeBusinessObject>>();var bo = sboFactory(currentUser);
另外,我认为lamba / Func支持是使autofac成为最佳IOC容器的原因.如果你知道如何编写Func,你可以做一些疯狂的强大的事情.
总结以上是内存溢出为你收集整理的c# – Autofac将参数传递给嵌套类型全部内容,希望文章能够帮你解决c# – Autofac将参数传递给嵌套类型所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)