Home > Development > Castle Windsor Auto Registration Part I

Castle Windsor Auto Registration Part I


This is the first of many posts on Castle Windsor Auto Registration.  Below is some code showing auto registration. Okay what is auto registration. Well all the examples I have show before require you to register types to interfaces in a one by one fashion. If you have a design were many types will be registered against a interface or sub interface the you can wire them all up automatically in one definition to the container.

using System;
using System.Reflection;
using Castle.MicroKernel.Registration;
using Castle.Windsor;

namespace CastleWindsorDemos
{

    public class Customer
    {
        public string Name{ get; set;}
    }

    public class Student
    {
        public string Name{ get; set;}
    }

    public class Teacher
    {
        public string Name{ get; set;}
    }

    public interface IHandler<T>
    {
        void Handle(T item);
    }

    public class TeacherHandler : IHandler<Teacher>
    {
        public void Handle(Teacher item)
        {
            Console.WriteLine(item.Name);
        }
    }

    public class StudentHandler : IHandler<Student>
    {
        public void Handle(Student item)
        {
            Console.WriteLine(item.Name);
        }
    }

    public class CustomerHandler : IHandler<Customer>
    {
        public void Handle(Customer item)
        {
            Console.WriteLine(item.Name);
        }
    }

    public class Program
    {
        static void Main()
        {

            WindsorContainer container = new WindsorContainer();

            container.Register(
                    AllTypes.Of(typeof(IHandler<>))
                    .FromAssembly(Assembly.GetExecutingAssembly())
                    .WithService.FirstInterface()
                    .Configure( x => x.LifeStyle.Transient)
                );

            IHandler<Teacher> thandler = container.Resolve<IHandler<Teacher>>();
            thandler.Handle(new Teacher{ Name = "Mr Teacher"});

            IHandler<Student> shandler = container.Resolve<IHandler<Student>>();
            shandler.Handle(new Student { Name = "Mr Student" });

            IHandler<Customer> chandler = container.Resolve<IHandler<Customer>>();
            chandler.Handle(new Customer { Name = "Mr Customer" });

            Console.ReadKey();
        }
    }
}

As you can see from the example we have a type system based on IHandler<T>. We have there sub types defined for Customer, Student and Teacher.  The AllTypes.Of(<IHandler<>) part basicall says let me get all closed generic types based on that generic interface type. The FromAssembly restricts what assemblies will be used to find types in.  The WithService.FirstInterface part register the type i.e. StudentHandler as a service against its first interface which would be IHandler<Student>.

There are more things you can do with this like Convention over configuration which i will cover later on with some more detailed examples.

Blair…

Advertisements
Categories: Development Tags: , , ,
  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: