Adding an answer to demonstrate @Kryzsttof's preferences for overriding services. Instead of the factory method:
container.Register(Component.For<ICostCalculator>() .UsingFactoryMethod(k => new ServiceTaxApplicableCostCalculator( k.Resolve<ServiceTaxCalculator>(), k.Resolve<DefaultCostCalculator>()) ) );
Instead, you define the dependencies through DependsOn :
container.Register(Component.For<ICostCalculator>() .ImplementedBy<ServiceTaxApplicableCostCalculator>() .DependsOn(Dependency.OnComponent("with", typeof(ServiceTaxCalculator))) .DependsOn(Dependency.OnComponent("without", typeof(DefaultCostCalculator))));
The only advantage that is obvious to me is that if another service is added to the ServiceTaxApplicableCostCalculator constructor, then if the service is redefined, it will work without any changes (automatically allowing the new service), while the factory method will require another call to Resolve . Other than that, this is certainly more idiomatic than using the factory method to explicitly create an object.
Further information is available in the documentation .
source share