Home > General > Castle Windsor 3 – Classes and Types

Castle Windsor 3 – Classes and Types

In my previous post on strongly typed sessions in ASP.NET, I had to manually look through the current assembly and retrieve all interfaces that end in “Session”. To register these interfaces with my container, I had to loop through each type found and register them one-by-one:

IEnumerable<Type> sessions =
        .Where(x => x.IsInterface && x.Name.EndsWith("Session"));

foreach (Type session in sessions)
    Type sessionTemp = session;

            .UsingFactoryMethod(x =>
                var factory = x.Resolve<DictionaryAdapterFactory>();
                return factory.GetAdapter<object>(sessionTemp, new SessionDictionary(HttpContext.Current.Session));

Unfortunately, Castle Windsor 2.x did not have support for finding and registering interfaces with no implementions for use with a factory method or typed factories. I also couldn’t take advantage of the rich registration API of the AllTypes class since AllTypes only returned concrete classes.

Enter Castle Windsor 3. In version 3, Castle Windsor introduced two new registration classes, Classes and Types. The Classes class is identical to AllTypes. The new Types class returns all types, including interfaces, abstract classes, and concrete classes.

We can now update the registration code above to take advance of the new Types class:

        .Where(t => t.IsInterface && t.Name.EndsWith("Session"))
        .Configure(c => c.UsingFactoryMethod(f =>
            var factory = f.Resolve<DictionaryAdapterFactory>();
            return factory.GetAdapter<object>(c.Implementation, new SessionDictionary(HttpContext.Current.Session));
  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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: