Home > Development > Castle Windsor Manual XML Registration – Decorator Pattern

Castle Windsor Manual XML Registration – Decorator Pattern


Back with more of the same as the previous article but in the XML Configuration File. Man I hate the XML Angle Bracket tax. Notice how i refer to the other registered type by id using the ${} syntax.

using System;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;

namespace CastleWindsorDemo
{

    public interface ICustomerRepository
    {
        Customer Load(int id);
    }

    public class CustomerRepository : ICustomerRepository
    {
        public Customer Load(int id)
        {
            throw new NotImplementedException();
        }
    }

    public class CachedCustomerRepository : ICustomerRepository
    {
        private readonly ICustomerRepository wrapped_repository;

        public CachedCustomerRepository(ICustomerRepository wrapped_repository)
        {
            this.wrapped_repository = wrapped_repository;
        }

        public Customer Load(int id)
        {
            //Check cache here otherwise call wrapped item

            return wrapped_repository.Load(id);
        }
    }

    public class Customer
    {
    }

    class Program
    {
        static void Main()
        {
            WindsorContainer container = new WindsorContainer(new XmlInterpreter());
            ICustomerRepository repository = container.Resolve<ICustomerRepository>();

        }
    }
}

and XML is

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section
        name="castle"
        type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
  </configSections>
  <castle>
    <components>
      <component id="customer-repository.cache"
                 type="CastleWindsorDemo.CachedCustomerRepository,CastleWindsorDemo"
                 service="CastleWindsorDemo.ICustomerRepository,CastleWindsorDemo"
                 >
        <parameters>
          <wrapped_repository>
            ${customer-repository.default}
          </wrapped_repository>
        </parameters>
      </component>
      <component id="customer-repository.default"
                 type="CastleWindsorDemo.CustomerRepository,CastleWindsorDemo"
                 service="CastleWindsorDemo.ICustomerRepository,CastleWindsorDemo"
                 >
      </component>
    </components>
  </castle>
</configuration>

Blair…

Advertisements
Categories: Development
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: