Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify for constructor class fixed values and other variables from container

What I have is a stage, some interfaces, and also registration section. The problem is I need to defined some parameters as fixed and others as variables.

interface IDoSomething {
 void DoWork();
}

interface IDoMath(){
 void DoWork();
}

interface IBehaviorBusiness{
 void Do();
}

class BehaviorBusiness {
 ...
 public BehaviorBusiness(IDoSomething doSomething, IDoMatch doMatch, string connection){};
 ...
}

Is it possible with windsor container to define a parameter connection in declaration, and take IDosomething and IDoMatch from the container?

 container.Register(
   Component.For<IDoSomething>()
   ...
 }

 container.Register(
   Component.For<IDoMatch>()
   ...
 );

That is the concrete problem.

 container.Register(
   Component.For<IBehaviorBusiness>()
   .ImplementedBy<BehaviorBusiness>()
   .DependsOn(Dependency.OnComponent<IDoSomething, [default]>(),
              Dependency.OnComponent<IDoMatch, [default]>(),
              Dependency.OnValue("connection", connectionString))
    .LifeStyle.Transient
   );

Which is correct syntax if it exists?

like image 601
Carlos Cocom Avatar asked Nov 09 '22 21:11

Carlos Cocom


1 Answers

If the connection string comes from your application settings, then use Dependency.OnAppSettingsValue():

container.Register(
    Component.For<IBehaviorBusiness>()
    .ImplementedBy<BehaviorBusiness>()
    .DependsOn(Dependency.OnAppSettingsValue("connection", "connectionString"))
    .LifeStyle.Transient
);

Here, "connection" is the name of the parameter in the class' constructor, and "connectionString" is the key of your connection string value in your application settings (i.e. Web.Config). You don't need to specify the other values, Windsor will resolve them as it usually does.

like image 52
Patrick Quirk Avatar answered Nov 14 '22 21:11

Patrick Quirk