Reputation: 6659
I would like to prefer the parameter implementation over an implementation registered by default, but it does not work.
private static void Main(string[] args)
{
PassParamThatsAlreadyRegisteredAtResolutionTime();
Console.ReadLine();
}
private static void PassParamThatsAlreadyRegisteredAtResolutionTime()
{
Console.WriteLine("Passing argument that is already registered
does not take precedence over the default implementation");
var container = new WindsorContainer();
container.Register(
Component.For<ISimpletonManager>()
.ImplementedBy<SimpletonManager>()
.LifestyleTransient()
.Properties(PropertyFilter.IgnoreAll));
container.Register(Component.For<ISimpleton>().UsingFactoryMethod(() =>
new Simpleton("Default Implementation"))
.LifestyleTransient());
// The above line could equally be the following, result is the same:
// container.Register(Component.For<ISimpleton>()
// .ImplementedBy<Simpleton>().LifestyleTransient());
var runtimeConstructorParam = new Simpleton("Passed In Implementation");
var runtimeArguments = new Arguments(
new object[] {runtimeConstructorParam});
var shouldBeManagerWithPassedInSimpleton = container
.Resolve<ISimpletonManager>(runtimeArguments);
Console.WriteLine(shouldBeManagerWithPassedInSimpleton.Log);
}
Passing argument that is already registered
does not take precedence over the default implementation
Birth With Child Simpleton: Default Implementation
public class Simpleton : ISimpleton
{
public Simpleton(string id)
{
Id = id;
}
public string Id { get; set; }
}
public class SimpletonManager : ISimpletonManager
{
private ISimpleton _child;
public SimpletonManager(ISimpleton simpleton)
{
Child = simpleton;
}
public ISimpleton Child
{
get { return _child; }
set
{
_child = value;
Log = "Birth With Child Simpleton: " + Child.Id;
}
}
public string Log { get; private set; }
}
[ Using Castle.Core.dll and Castle.Windsor.dll 3.1.0 (2012-08-05) ]
Upvotes: 1
Views: 422
Reputation: 27374
Like with your other question, the type of the dependency is different than the type that Arguemnts
is exposing
Upvotes: 2