DecorateWith Syntax

97 views
Skip to first unread message

StarTrekRedneck

unread,
Jul 1, 2014, 3:52:37 PM7/1/14
to structure...@googlegroups.com
I find myself wanting a decorator pattern syntax similar to...

x.For<IInterface>().Use<Implementation>()
                   .DecorateWith<Decorator1>()
                   .DecorateWith<Decorator2>()
;

In the past (v2.6) I used an extension method by Matt Honeycutt that gave me this (from his blog)...

cfg.For<IProductRepository>().Use<InMemoryProductRepository>()
                             .Decorate().With<ProductSecurityDecorator>()
                             .AndThen<ProductRepoLogger>()
                             .AndThen<RudeProductRepoLogger>();

Am I missing something in StructureMap v3, or can we add this kind of decorator syntax?

Jeff Doolittle

unread,
Jul 2, 2014, 11:16:06 AM7/2/14
to structure...@googlegroups.com
Looking at the acceptance tests on github shows the syntax I think you're looking for:


Does that point you in the right direction?

--Jeff

StarTrekRedneck

unread,
Jul 2, 2014, 3:12:56 PM7/2/14
to structure...@googlegroups.com
Yes, thanks Jeff!

x.For<IInterface>().Use<Implementation>();
x.For<IInterface>().DecorateAllWith<Decorator1>();
x.For<IInterface>().DecorateAllWith<Decorator2>();

The last decorator will be the outermost wrapper (as one would expect), and the .Use<Implementation> does not have to be declared first-- it can go anywhere (it's last in the acceptance tests).

Additionally, the Scan() feature can still register the default implementation. So, without establishing custom conventions, I wouldn't have to register an implementation for IWidget if the implementation was named Widget, and decorators would still be appropriately applied.

Jeff Doolittle

unread,
Jul 3, 2014, 12:05:36 PM7/3/14
to structure...@googlegroups.com
Awesome, glad that helped.

--Jeff
Reply all
Reply to author
Forward
0 new messages