Windsor Container Factory Facility

This is a real cool facility that I just used in my Hybrid CMS. There is a full in depth walkthrough on BitterCoder’s Blog here but I will describe how I used it in this blog post.

I needed to be able to have the kernel instantiate a specific implementation of IAuthService. This Interface has a couple of method definitions, but most important it had the Authenticate() method. The idea behind this is that I can swap out IAuthService and change the way a user is authenticated. At the moment Active Directory and Database is supported.

So in the whole architecture I have the following classes/interfaces

  • IAuthService
  • DbAuthService : IAuthService (component id: db.authService)
  • AdAuthService : IAuthService (component id: ad.authService)

In the Castle config section for Windsor, they have the following component Id’s listed above. Both of these component declarations have factory=authServiceFactory which activation of the components are deferred to at runtime.

In the AppSettings section I have an Application Setting called authStore=db.authService, which just has whichever component will be used for authentication.

Here is the code for AuthServiceFactory to give you an idea how the AuthService’s are returned from the kernel:


public class AuthServiceFactory : IAuthServiceFactory 


private IKernel _kernel;public AuthServiceFactory(IKernel kernel) { 

_kernel = kernel; 


protected string AuthStoreType 

{ get { return ConfigurationManager.AppSettings["authStoreType"]; } 


public IAuthService Create() 


return (IAuthService)_kernel.Resolve(AuthStoreType, typeof(IAuthService)); 



Pretty cool huh? Now if I want to introduce a new method of Authentication I only need to add the component to the config file, make an implementation and select it in the AppSetting. Very flexible! =)


2 Responses to “Windsor Container Factory Facility”

  1. Ayende Rahien Says:

    Why are you registerring both?
    I do the same, but I only register the one that I want to use.

  2. schambers Says:

    I guess you only really need to register the one I want to use, the only reason I register both of them so then the client using the software or an administrator only needs to change the authStoreType AppSetting in web.config.

    If I don’t register both then the client needs to put in the component configuration.

    I guess this is taking a performance hit as I am registering something that potentially would never be used. Is there a more effecient way of doing this without the client needing to uncomment or add component configuration code? If so, then that would work.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

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