Specify fixed values ​​and other variables from the container for the constructor class

I have a scene, some interfaces, and also a registration section. The problem is that I need to define 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 the windsor container to define the parameter connection in the declaration and take IDosomething and IDoMatch from the container?

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

This is a specific problem.

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

What is the correct syntax if it exists?

+5
source share
1 answer

If the connection string comes from your application settings, 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 constructor of the class, and "connectionString" is the key of your connection string value in the settings of your application (ie Web.Config). You do not need to specify other values; Windsor will resolve them as usual.

+1
source

Source: https://habr.com/ru/post/1209133/


All Articles