StructureMap and the decorator pattern

爱⌒轻易说出口 提交于 2019-12-21 03:33:15

问题


I'm using StructureMap, v. 2.5.3 and am having trouble with chaining together implementations on an interface to implement the Decorator pattern.

I'm used to Windsor, where it is possible to name variations on interface implementations and send the named impl. into another (default) impl.

This is the default, non decorated version, which works fine:

ObjectFactory.Initialize(registry =>
{
  registry.ForRequestedType<ICommentService()
    .TheDefault.Is.OfConcreteType<CommentService>();
... }

This is the ctor on the decorator, that I want to call:

public CommentAuditService( ICommentService commentService, 
                            IAuditService auditService )

This works, but does not give me access to the decorated object:

registry.ForRequestedType<ICommentService>()
  .TheDefault.Is.OfConcreteType<CommentService>()
  .EnrichWith(x => new CommentAuditService());

This takes me int an inf. loop:

registry.ForRequestedType<ICommentService>()
  .TheDefault.Is.OfConcreteType<CommentService>()
  .EnrichWith(x => new CommentAuditService( new CommentService(), 
                                            new AuditService()));

So far this is what seems to me should work:

registry.ForRequestedType<ICommentService.()
  .TheDefault.Is.OfConcreteType<CommentAuditService>()
  .WithCtorArg("commentService")
  .EqualTo(new CommentService());

However it send it into an endless loop of creating new instances of CommentAuditService

Does anyone have an quick answer? (other than switching to Castle.Windsor, which I'm very close to at the moment)


回答1:


You were very close. Try:

registry.ForRequestedType<ICommentService>()
    .TheDefaultIsConcreteType<CommentService>()
    .EnrichWith(original => new CommentAuditService(original, 
                                         new AuditService()));

If AuditService might itself have dependencies, you would do:

registry.ForRequestedType<ICommentService>()
    .TheDefaultIsConcreteType<CommentService>()
    .EnrichWith((ioc, original) => new CommentAuditService(original, 
                                   ioc.GetInstance<AuditService>()));

Or, if you change the last part to:

ioc.GetInstance<IAuditService>()

you can register the concrete type of your audit service separately.



来源:https://stackoverflow.com/questions/1443464/structuremap-and-the-decorator-pattern

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!