I feel like I should be able to register all my components into the application composition root container no matter what the current state of the environment is, even in the case environment lacks parts of configuration required to proper working of some of its components.
Currently, I sometimes run into code that for example uses Castle Windsor Dependency.OnValue
functionality that allows custom code from the container to supply constructor values. When such custom code fails, application start fails in the composition phase, even before class initialization kicks in.
I feel like every failure in the composition phase that is not of IoC framework concern (like "circular dependency detected") is in fact a symptom of an improper separation concerns issue. So I am tempted to refactor such usages of Dependency.OnValue
into a configuration component that still may fail, but later.
On the other hand, fail fast rule would suggest it's nice that system won't even start to function without some important environmental prerequisites being fulfilled.
What would you suggest?
Edit: code example:
public static IWindsorContainer Compose()
{
var container = new WindsorContainer();
container.Register(
// lots of components there
Component
.For<IService>()
.ImplementedBy<DefaultService>()
.DependsOn(Dependency.OnValue<string>(Configuration.GetString("SomeKey")))
// ^ this can fail
// lots of components there
);
return container;
}
Now, unit test like this:
[TestMethod]
public void CompositionDoesntContainCycles()
{
try
{
var container = CompositionRoot.Compose();
}
catch (SomeKindOfCycleExceptionThrownByWindsor)
{
Assert.Fail();
}
}
will fail because I need to mock configuration as Compose() does two, not one, thing.