Home > Development > Castle Windsor Manual Registration – Decorator Pattern

Castle Windsor Manual Registration – Decorator Pattern


Hi All I am going to show you how to add decorators to objects in Castle Windsor with manual registration. I am not going to explain the pattern as you can find it here but will show how to do it in Castle Windsor.

When you do register make sure you register all the decorators from last to first then the real object last otherwise it wont work.

Well heres the code

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();


            container.Register(
                    Component.For<ICustomerRepository>()
                             .ImplementedBy<CachedCustomerRepository>()
                             .Named("customer-repository.cached")
                             .ServiceOverrides(
                                ServiceOverride.ForKey("wrapped_repository")
                                               .Eq("customer-repository.default")
                             )
                             .LifeStyle
                             .Transient
                );

            container.Register(
                    Component.For<ICustomerRepository>()
                             .ImplementedBy<CustomerRepository>()
                             .Named("customer-repository.default")
                             .LifeStyle
                             .Transient
                );

            

            ICustomerRepository repository = container.Resolve<ICustomerRepository>();

        }
    }
}

Blair

Advertisements
  1. Thanos Papathanasiou
    November 17, 2010 at 11:25 am

    I’m kinda new to castle windsor and the fluent registration. Would that work even with a generic repository? Could you give an example?

    • November 17, 2010 at 11:30 am

      You should never use a generic repository. Be explicit by defining each entities OWN repository

    • November 17, 2010 at 11:32 am

      If you define each entities own repository the can all evolve independently. You can use a generic repository but delegate calls to inside each entities own repository.

  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: