1

I would like to prefer the parameter implementation over an implementation registered by default, but it does not work.

Demo of Incorrect Preference

    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);
    }

Console Output

Passing argument that is already registered
does not take precedence over the default implementation
Birth With Child Simpleton: Default Implementation

How to Invert the Preference?

  • I need to be able to ignore the default registered dependency and instead Castle Windsor resolve using the supplied argument as the ISimpleton dependency?
  • Do I need to implement my own IDependencyResolver? How?
  • Or are DynamicParameters useful here?

Supplied Dependency - Simpleton Class

public class Simpleton : ISimpleton
{
    public Simpleton(string id)
    {
        Id = id;
    }

    public string Id { get; set; }
}

Resolved Type - SimpletonManager

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) ]

4

1 に答える 1

2

他の質問と同様に、依存関係のタイプは、Arguemnts公開しているタイプとは異なります

于 2013-01-28T22:53:38.023 に答える