Castle Windsor IOC: Passing constructor parameters to child components

后端 未结 3 1782
臣服心动
臣服心动 2020-12-06 07:41

The following code is for demo purposes only.

Lets say i have 2 components (businessService, and dataService), and a UI class.

UI class needs a busi

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-06 08:12

    The behavior you see is by design.

    There are a couple of ways to approach the problem, depending on how dynamic the value you want to pass down is.

    The documentation does a pretty good job detailing it, so I won't reiterate that here.

    Update

    To clarity - Windsor does not pass inline arguments down the resolution pipeline. The reason for that is simple - doing so would break an abstraction. Calling code would have to implicitly know that your BusinessService depends on DataService which depends on connection string.

    If you absolutely have to do it, than do make this explicit. That is do pretty much what you're doing - resolve the DataService with its dependency on connection string explicitly, and explicitly resolve BusinessService passing the DataService as dependency.

    To make things really explicit (and nicer to use as well) I'd suggest using Typed Factory for that instead of directly calling the container

    public interface IFactory
    {
       IDataService ResolveDataService(string connectionString);
       IBussinessService ResolveBussinessService(IDataService dataService);
       // possibly release method for IBussinessService as well
    }
    

提交回复
热议问题