The Decorator Pattern with Castle Windsor

1 Flares Twitter 0 Facebook 0 StumbleUpon 0 Google+ 1 LinkedIn 0 1 Flares ×

The decorator pattern is a useful patter for adding additional behaviour or functionality to an existing object. Castle windsor enables injection of a decorator in a fairly low friction way.

A Simple Decorator Implementation

For my example I’m going to have a basic repository pattern, whereby my repository is injected by castle windsor. My decorator will provide caching to the repository, and will also be injected by castle windsor:

This is a really simple decorator example, the magic is in the windsor bit.

Doing the Castle Windsor Bit

Getting windsor to inject this successfully is really quite simple, wherever your main windsor bootstrap procedure is (or in your installer), you just need to register your components like this:

As you can see you need to register both components, but inform windsor to use your decorator as the default component to inject. Windsor will then take care of injecting the internal class (i.e. MyRepository) into the decorator.

After that its all plain sailing!

,

The Essential App Marketing Kit
Subscribe To My Newsletter To Get an Entire Chapter From The Book for FREE
Never display this again
1 Flares Twitter 0 Facebook 0 StumbleUpon 0 Google+ 1 LinkedIn 0 1 Flares ×