Home > Development > Castle Windsor Auto Registration Part II

Castle Windsor Auto Registration Part II


The previous post looked at the basics. Lets look at convention over configuration.

using System;
using System.Collections.Generic;
using System.Linq;
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 IRepository<T>
    {
        void Add(T item);
    }

    public class RepositoryBase<T> : IRepository<T>
    {
        public void Add(T item)
        {
            Console.WriteLine("Adding item...");
        }
    }

    public interface ICustomerRepository : IRepository<Customer>
    {
        IEnumerable<Customer> SelectTopTenCustomers();
    }

    public class CustomerRepository : RepositoryBase<Customer>, ICustomerRepository
    {
        public IEnumerable<Customer> SelectTopTenCustomers()
        {
            throw new NotImplementedException();
        }
    }

    public static class WindsorExtensions
    {
        public static BasedOnDescriptor FirstInterfaceOnType(this ServiceDescriptor serviceDescriptor)
        {
            return serviceDescriptor.Select((type, baseType) =>
            {
                var interfaces = type.GetInterfaces().Except(type.BaseType.GetInterfaces());
                if (interfaces.Count() == 0)
                    return null;
                return new[] { interfaces.First() };
            });
        }
    }

    public class Program
    {
        static void Main()
        {

            WindsorContainer container = new WindsorContainer();
            container.Register(
                    AllTypes.FromAssembly(Assembly.GetExecutingAssembly())
                            .Where(x => x.Name.Contains("Repository"))
                            .Unless(x => x.Name.Contains("RepositoryBase"))
                            .Configure( x => x.LifeStyle.Transient)
                            .WithService.FirstInterfaceOnType()
                );

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

            Console.ReadKey();

        }
    }
}

This code automatically wires up any classes that you create with the containing Repository in the class name. This is handy as you can create all classes of a certain type and create a naming convention and new types are automagically wired up.

We also filter out the class called repository base as it is a base class. The class is registered against the real first interface and this is achieved by using the extension method FirstInterfaceOnType from class WindsorExtensions.

More windsor fun to come soon.

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: