Tom
Tom

Reputation: 16246

Castle Windsor Auto registration with constructor parameters

public abstract class BattleVehicle : IVehicle {
  public BattleVehicle (IList<IGuns> someGuns, ISolider driver) { /*...*/ }
}
public class BigAssTank : BattleVehicle, ITank { /*...*/ }
public class AngryBirdsAPC : BattleVehicle, IAPC { /*...*/ }
//and a lot more of these battle vehicles .... 

Now, say by standard, I want each battle vehicle to register 2 pistols List<Pistols> twoPistols and a Rookie : ISolider as a default driver. How do I do this?

I was expecting something like:

container.Register(Classes
                  .FromAssemblyContaining<IVehicle>()
                  .BasedOn<IVehicle>()
                  .DependsOn(new {someGuns = twoPistols, driver = rookie}) //won't work here....

Of course this won't work. I only found a rather old post here from '09. I am not sure what is the right way to fluently register all the concrete Vehicles now. Please help and/or link me to any advanced auto registration tutorials?

Upvotes: 1

Views: 1256

Answers (1)

Aleš Roub&#237;ček
Aleš Roub&#237;ček

Reputation: 5187

You should use Configure extension method. Usage:

container.Register(
    Classes
        .FromAssemblyContaining<IVehicle>()
        .BasedOn<IVehicle>()
        .Configure(k => 
            k.DependsOn(new {someGuns = twoPistols, driver = rookie})))

Upvotes: 1

Related Questions