How do I register decorator classes using the container?

Apr 4, 2013 at 4:12 PM
I've just exposed myself to the decorator pattern (which I wasn't aware of) and am already finding great uses for it in our system.

I know it is possible to chain the classes' registrations via injection factory or even via resolved parameters, but this seems extremely clumsy to use, read and maintain.

What I would like is a solution like the one explained here:
http://www.beefycode.com/post/Decorator-Unity-Container-Extension.aspx

The author created a container extension that keeps track of the stack of registrations for the same type and applies them the way it is supposed to in the decorator pattern.

Since the article is a bit old already, I wondered if there is support for this out of the box somewhere in Unity already that I'm missing.

I'm a bit skeptical of taking his code and using it directly, since even he mentions that it must be adjusted to treat open generic types and all that. I would obviously prefer to use something that has been tested extensively and is well supported by the community and all that. I haven't looked at the code a lot but it seems to be quite involved.

I downloaded the CQRS journey code and noticed that there is a decorated class there, and the code registers the chain using normal ResolvedParameter's. From that, I figured there is nothing built-in on Unity itself? That code is apparently very recent.

If there is nothing out there I think I will end up going with his solution, it seems very simple and nice, but I would still like to hear your thoughts on why this is not supported in the current release if so.
Apr 5, 2013 at 5:32 AM
There is nothing natively built in to Unity. Since I have no first hand (or second hand in this case!) knowledge, I can't really comment on the why. Except to say that in all projects there are always competing priorities and features, schedules, resource constraints, etc. that affect the deliverables. I guess that's my can't-comment-motherhood statement of the day. :)

This thread might be of interest to you as well: http://unity.codeplex.com/discussions/406534.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Apr 5, 2013 at 3:21 PM
Edited Apr 5, 2013 at 7:24 PM
Yeah, I figured this had to do with priorities and such, indeed this is a concern with any project. Would it be possible though to make an "official" request for this feature? I personally think the pattern itself is very powerful and would love to see support for it in unity in a cleaner way. I'm not sure about the other containers, but at least Castle Windsor, NInject and Simple Injector support this quite seamlessly. I'm actually eager to see if others would approve this kind of thing.

That thread is very nice, it is pretty much exactly what I was looking for. I would never have found it too, with that title oO. Already taking a deep look at it, maybe I could make my own extension simple enough not to be worried about future problems it might bring.

Thanks as always Randy.