Perhaps this is just a knowledge limitation but here is something that
I noticed.
It seems that to instantiate a class with the factory facility it is
necessary to provide a concrete type. So if I have the example (to
borrow from
http://bugsquash.blogspot.com/2008/12/castle-windsor-factory-method-support.html)
public interface IUserRepository { }
public class UserRepositoryImplementation : IUserRepository { }
public class UserRepositoryFactory {
public IUserRepository Create() {
return new UserRepositoryImplementation();
}
}
then the following passes:
[Test]
public void Resolve_from_factory() {
var container = new WindsorContainer();
container.AddFacility("factory.support", new FactorySupportFacility
());
container.AddComponent<UserRepositoryFactory>("userRepo.factory");
container.Register(
Component.For<IUserRepository>()
.ImplementedBy<UserRepositoryImplementation>()
.Attribute("factoryId").Eq("userRepo.factory")
.Attribute("factoryCreate").Eq("Create")
);
Assert.IsNotNull(container.Resolve<IUserRepository>());
}
Great! But I had to know ahead of time the specific implementation
the factory method would provide, even though the factory method
itself outputs an interface. So what happens if we're using an
abstract factory?
let's change the code as follows but keep the test the same:
public class UserRepositoryImplementation2 : IUserRepository { }
public class UserRepositoryFactory {
public IUserRepository Create() {
return new UserRepositoryImplementation();
}
}
Still works! So not only is the
".ImplementedBy<UserRepositoryImplementation>()" line unnecessary, it
is downright misleading since we are now resolving a completely
different class.
Any thoughts?