Los Techies : Blogs about software and anything tech!

Wiring Up Generics In Castle Windsor


I'm working with a new team that has a slightly different technology stack then I'm used to.  On most projects where I am the team lead, StructureMap is my IOC container of choice.  I've always thought that this was just laziness on my part because I can wire up most things very quickly. Now I'm beginning to think that SM has spoiled me :)

I like to take advantage of open generic mapping that most of the popular containers support.  In this particular case, I've got a Generic Validator that takes in a different array of validations for a given type.

 

The constructor for this guy looks like this:

public class ServiceValidationRunner<T> : IServiceValidationRunner<T>
    {
        private IValidation<T>[] _validations;
        private readonly IValidationErrorHandler _errorHandler;

        public ServiceValidationRunner(IValidation<T>[] validations, IValidationErrorHandler errorHandler)
        {
            _validations = validations;
            _errorHandler = errorHandler;
        }
       ...
}

Here is how I'm am setting this up for two different types that have different validations:

container.Register
            (
                //wire up the specific types with their validation rules
                Component.For<IServiceValidationRunner<Object1>>().ImplementedBy<ServiceValidationRunner<Object1>>()
                    .DependsOn(new {validations = new[]{new Validator1(),new Validator2}}),
                Component.For<IServiceValidationRunner<Object2>>().ImplementedBy<ServiceValidationRunner<Object2>>()
                    .DependsOn(new {validations = new[]{new Validator1(), new Validator3()}}),
            ...
);

The code is pretty self explanatory, it's a simple matter of wiring up the dependencies for the specific type the generic class is wired up for.

Kick It on DotNetKicks.com
Posted Aug 20 2009, 04:49 PM by jcteague
Filed under: ,

Comments

Reflective Perspective - Chris Alcock » The Morning Brew #417 wrote Reflective Perspective - Chris Alcock &raquo; The Morning Brew #417
on 08-21-2009 3:31 AM

Pingback from  Reflective Perspective - Chris Alcock  » The Morning Brew #417

Stephen wrote re: Wiring Up Generics In Castle Windsor
on 08-21-2009 9:21 AM

Out of interest, why do you take an array of IValidation<T> vs an IEnumerable<IValidation<T>> ?

jcteague wrote re: Wiring Up Generics In Castle Windsor
on 08-21-2009 10:32 AM

@Stephen

Normally it would be IEnumerable<T>, but I wasn't sure what I would need to get it wired up in Castle, so I took the easiest enumerable there was.

Arjan’s World » LINKBLOG for August 21, 2009 wrote Arjan&#8217;s World &raquo; LINKBLOG for August 21, 2009
on 08-21-2009 10:50 AM

Pingback from  Arjan’s World    » LINKBLOG for August 21, 2009

James Kovacs wrote re: Wiring Up Generics In Castle Windsor
on 08-24-2009 3:45 PM

Like StructureMap, Windsor supports resolution of array parameters. Unfortunately unlike StructureMap, Windsor doesn't turn it on by default. You can enable array resolution via:

container.Kernel.Resolver.AddSubResolver(new ArrayResolver(container.Kernel));

N.B. ArrayResolver is in the Castle.MicroKernel.Resolvers.SpecializedResolvers namespace.

You can then simply register the open generic type in the container and all will be good:

Component.For(typeof(IServiceValidationRunner<>)).ImplementedBy(typeof(ServiceValidationRunner<>))

HTH,

James

jcteague wrote re: Wiring Up Generics In Castle Windsor
on 08-24-2009 4:26 PM

@James

I saw the ArrayResolver and a couple of examples with it, and I started with the open generic.

How would I wire up DIFFERENT arrays using that notation?

on 08-25-2009 5:50 PM

Agile/ALT.NET James Shore provides a Introduction to Agile for QA People This is late but Davy Brion has started a series on Build Your Own Data Access Layer Series . Some posts include Out of the Box CRUD Functionality , Mapping Classes to Tables , and

Krzysztof Kozmic wrote re: Wiring Up Generics In Castle Windsor
on 08-26-2009 2:55 AM

John,

It would be beneficial if you shared your thoughts on friction points in Windsor as compared to SM, or features you find working better in SM...

Probably the best place for that would be castle users/developers group on google.

cheers,

Krzysztof

jcteague wrote re: Wiring Up Generics In Castle Windsor
on 08-26-2009 8:03 AM

Thank Krzysztof.  I'm sure most of the friction is just learning a new tool.  There have been a few things that have come up that the team wasn't sure how to do with Windsor that I knew how to do with SM.  I'll be sure and share those.

Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems