0
votes

I'm creating a web API and I want to be able to version it. The way I want to do it is this:

  1. Client application passes in custom HTTP header with each request containing version of API they're calling against.
  2. A component (called IVersionRetriever or something) exists to pull the version number from the request. Castle Windsor is responsible for providing instances of IVersionRetriever.
  3. Where I want the implementation of a service interface to vary between versions, Castle Windsor is configured to check IVersionRetriever.ApiVersion and return an implementation according to the value obtained.

All this seems simple enough, but I can't work out from Windsor's registration API how I would say to Windsor 'right, for IFoo, if IVersionRetriever.ApiVersion is 1, then I want a SuperFoo, but if the version is 2, then I want a SuperDuperFoo.

How would I configure this?

All components have a PerWebRequest lifestyle, if that's relevant.

2

2 Answers

1
votes

Just so you know how to do it in the future:

container.Register(Component.For<IUnitOfWork>().UsingFactoryMethod(() => IVersionRetriever.ApiVersion == 1 ? new SuperFoo() : new SuperDuperFoo() ).LifeStyle.PerWebRequest);

but i agree with @Cristiano answer.

2
votes

What about using a TypedFactory to resolve the desired IFoo according to ApiVersion? In that case dependency won't be on IFoo, but on IFooFactory instead