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.

Related Articles:

    Post Footer automatically generated by Add Post Footer Plugin for wordpress.

    This entry was posted in Castle, IOC. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
    • http://meandmycode.com Stephen

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

    • http://www.lostechies.com/members/jcteague/default.aspx jcteague

      @Stephen
      Normally it would be IEnumerable, but I wasn’t sure what I would need to get it wired up in Castle, so I took the easiest enumerable there was.

    • http://jameskovacs.com James Kovacs

      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

    • http://www.lostechies.com/members/jcteague/default.aspx jcteague

      @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?

    • http://kozmic.pl Krzysztof Kozmic

      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

    • http://www.lostechies.com/members/jcteague/default.aspx jcteague

      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.