我正在使用StructureMap,v.2.5.3,并且在将接口上的实现链接在一起以实现Decorator模式时遇到了麻烦.
我已经习惯了Windsor,可以在接口实现上命名变体并发送命名的impl.进入另一个(默认)impl.
这是默认的非装饰版本,工作正常:
ObjectFactory.Initialize(registry =>
{
registry.ForRequestedType<ICommentService()
.TheDefault.Is.OfConcreteType<CommentService>();
... }
Run Code Online (Sandbox Code Playgroud)
这是装饰器上的ctor,我想调用:
public CommentAuditService( ICommentService commentService,
IAuditService auditService )
Run Code Online (Sandbox Code Playgroud)
这有效,但不允许我访问装饰对象:
registry.ForRequestedType<ICommentService>()
.TheDefault.Is.OfConcreteType<CommentService>()
.EnrichWith(x => new CommentAuditService());
Run Code Online (Sandbox Code Playgroud)
这需要我一个inf.环:
registry.ForRequestedType<ICommentService>()
.TheDefault.Is.OfConcreteType<CommentService>()
.EnrichWith(x => new CommentAuditService( new CommentService(),
new AuditService()));
Run Code Online (Sandbox Code Playgroud)
到目前为止,这似乎是我应该工作的:
registry.ForRequestedType<ICommentService.()
.TheDefault.Is.OfConcreteType<CommentAuditService>()
.WithCtorArg("commentService")
.EqualTo(new CommentService());
Run Code Online (Sandbox Code Playgroud)
然而,它将它发送到创建CommentAuditService的新实例的无限循环中
有人有快速回答吗?(除了切换到Castle.Windsor,我现在非常接近)
Jos*_*gan 21
你非常接近.尝试:
registry.ForRequestedType<ICommentService>()
.TheDefaultIsConcreteType<CommentService>()
.EnrichWith(original => new CommentAuditService(original,
new AuditService()));
Run Code Online (Sandbox Code Playgroud)
如果AuditService本身可能具有依赖关系,那么您将执行以下操作:
registry.ForRequestedType<ICommentService>()
.TheDefaultIsConcreteType<CommentService>()
.EnrichWith((ioc, original) => new CommentAuditService(original,
ioc.GetInstance<AuditService>()));
Run Code Online (Sandbox Code Playgroud)
或者,如果您将最后一部分更改为:
ioc.GetInstance<IAuditService>()
Run Code Online (Sandbox Code Playgroud)
您可以单独注册审计服务的具体类型.
| 归档时间: |
|
| 查看次数: |
1849 次 |
| 最近记录: |