我正在使用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,我现在非常接近)
structuremap ×1