Wednesday, May 09, 2007
More parts in the container tutorial series are on the way, I've just been a little busy these last few days.

However I had a discussion with someone about decorators this week - they eluded to them being too hard to "create" and maintain, and that opportunities to get them wrong abound (manually writing the code to call all the members of the inner service) - I was a little astounded that they didn't know how to do this using ReSharper.

So heres a quick 1 minute tutorial for those that don't know, first an interface.

public interface IBondService

{

    void Smoke50Cigarettes();

    void RegisterEnemy(string name);

    void GetOfBed();

    bool GunLoaded();       

}


Now let's write the start of our decorator.

public class BondService : IBondService

{

    private readonly IBondService _innerService;

 

    public BondService(IBondService innerService)

    {

        _innerService = innerService;

    }

}


And then generate the rest... by going to: ReSharper ->Code->Generate...->Delegating Members

Or just type Alt-insert (if your using the default key bindings) and select "Delegating Members".

At that point select the only option, the inner service.

delegate_members_1.png

And then select all the members to delegate for.

delegate_members_2.png

And then we get the results... joy

public class BondService : IBondService

{

    private readonly IBondService _innerService;

 

    public BondService(IBondService innerService)

    {

        _innerService = innerService;

    }

 

    public void Smoke50Cigarettes()

    {

        _innerService.Smoke50Cigarettes();

    }

 

    public void RegisterEnemy(string name)

    {

        _innerService.RegisterEnemy(name);

    }

 

    public void GetOfBed()

    {

        _innerService.GetOfBed();

    }

 

    public bool GunLoaded()

    {

        return _innerService.GunLoaded();

    }

}


posted @ Wednesday, May 09, 2007 3:11:45 AM (New Zealand Standard Time, UTC+12:00)    Comments [4] | Trackback |
Wednesday, May 09, 2007 4:50:33 AM (New Zealand Standard Time, UTC+12:00)
Great tip. I knew there had to be an easier way. I had to do this manually the other day and wished the feature existed. Turns out that it does!. How did we ever get work done in VS without ReSharper?
Wednesday, May 09, 2007 5:12:26 AM (New Zealand Standard Time, UTC+12:00)
Yeah I don't know what I'd do without ReSharper - VS.Net doesn't seem complete without it... you can certainly notice it's absence when playing with Orcas.
Wednesday, May 23, 2007 10:28:48 PM (New Zealand Standard Time, UTC+12:00)
Hey Alex,

Thanks - I hadn't come across an explanation of this pattern before. Could you shed some light on when you'd use it? I.e. what sort of situations beg for it, and perhaps other situations where you might be tempted to use it, but it'd be a bad idea?

I saw somewhere else on the net (perhaps wikipedia?) that suggests it would be a good way to add scroll bars to a non-scrolling window class. I don't think I'll ever find myself in that scenario, but you never know.
Thursday, May 24, 2007 9:18:24 PM (New Zealand Standard Time, UTC+12:00)
Hi Tim,

The scrollbar example seems stupid to me... there are other mechanisms in most UI frameworks for achieving the same thing...

Well let's see... in the past I've done things such as:

* Implementing caching over a simple store (so methods that write store a copy in cache, and methods that read check the cache first to see if the results have been fetched in the past. At that point it's very easy to enabled/disable caching, and there is a good separation of concerns.
* Implementing validation of parameters for a preexisting class, so you create a decorator which validates arguments, then proceeds to call the underlying implementation if everything is OK (or throws an exception if not).
* Synchronization mechanisms (decorate a store, when somebody saves something, the decorator intercepts, and generally raises an event which the UI can respond to - say to keep a tree view in sync... quite elegant when working with a WCF service).
* Implementing an additional interface on a preexisting class, technically that could be considered another kind of pattern though.

Decorators are a good fit with IoC - be sure to have a look at my IoC tutorials:

http://blog.bittercoder.com/CategoryView,category,IoC.aspx
Comments are closed.
Search
FeedCount

Tags...
Who am I?
Alex Henderson
Alex Henderson
Auckland, New Zealand
Managing Director at Dev|Defined Limited

"Self Confessed Coding Junky for 15 years"
View Alex Henderson's profile on LinkedIn
 
Mobile: +64-21-402-969
Email: bittercoder 'at' gmail 'dot' com
MSN: bittercoder_nz@hotmail
Skype: alex.devdefined
Navigation