温莎城堡IOC:构造函数传递参数,以子组件 [英] Castle Windsor IOC: Passing constructor parameters to child components

查看:196
本文介绍了温莎城堡IOC:构造函数传递参数,以子组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面code是仅用于演示目的。

可以说我有2个部分组成(businessService的,和DataService的),和一个UI类。

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

UI类需要一个业务服务的businessService需要一个DataService的,而DataService的依靠的connectionString。

UI class needs a business service, businessService needs a dataService, and dataService relies on a connectionString.

表单我需要解决的业务服务的UI类,所以我写了下面code:

Form the UI class i need to resolve the business service, so i am writing the below code:

var service = container.Resolve<BusinessService>(new { dependancy = "con string 123" }));

注意到,扶养是的connectionString构造函数的参数。

但上面的code不工作,说DataService的期待扶养这是不在线北京。

But the above code is not working, saying that dataService expecting dependancy which was not satisified.

无法创建组件的DataService   因为它有依赖关系是   满意。 DataService是否等待   以下依赖性:

Can't create component 'dataService' as it has dependencies to be satisfied. dataService is waiting for the following dependencies:

键(组件与特定的键)    - 这是没有注册的扶养

Keys (components with specific keys) - dependancy which was not registered.

所以,作为一种解决方法我这样做:

So as a workaround i am doing this:

var service = container.Resolve<BusinessService>(new { dataService = container.Resolve<IDataService>(new { dependancy = "123" }) });

但是,从设计,编码风格,许多观点,这是不这样做的一个很好的方式。

But from design, coding style and many perspectives this is not a good way of doing it.

所以,请你能不能劝为什么它不工作,在简单的方式,或者你有更好的解决办法,请分享一下。

So please if you can advise why its not working in the simple way or you have a better workaround please share.

推荐答案

您所看到的现象是设计。

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.

做了pretty的文档做好细节,所以我不会重申,在这里。

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

更新

要清晰 - 温莎没有通过内嵌参数下的解析管道。其中的原因很简单 - 这样做会打破一个抽象的概念。调用code将不得不含蓄知道你的的BusinessService 依赖于的DataService 依赖于连接字符串。

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.

如果你绝对要做到这一点,不是做更加明确。这就是做pretty的你在做多的东西 - 解析的DataService ,其在连接字符串的依赖性明确,并明确解决的BusinessService 通过了的DataService 的依赖。

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
}

这篇关于温莎城堡IOC:构造函数传递参数,以子组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆