使用 [ImportingConstructor] 使用 MEF 将调用对象导入到构造函数参数中
我正在将一些代码从专有系统转换为 MEF,该系统的功能与 MEF 相同,并且我有一个关于如何解决我最近遇到的以下问题的问题。
我有一个典型的实体对象,看起来像这样:
public class Account {
[Import]
public IAccountServerService { get; set; }
}
以及一个需要导入到上述实体对象中的服务对象:
public class AccountServerService : IAccountServerService {
[ImportingConstructor]
public AccountServerService (Account account) { ... }
}
为了将其转化为文字,我需要将 account
参数传递到 AccountServerService
构造函数实例是调用 Account
对象的对象。因此它的行为如下:
public class Account {
public IAccountServerService { get { return new AccountServerService (this); } }
}
请告诉我这种情况是否可能,或者我是否必须在这种情况下重构我的服务接口。
I am in the process of converting some of my code to MEF from a proprietary system that sort of does the same thing as MEF, and I have a question about how I would accomplish the following problem that I recently ran into.
I have a typical entity object that looks something like this:
public class Account {
[Import]
public IAccountServerService { get; set; }
}
And a service object that needs to be imported in to the above entity object:
public class AccountServerService : IAccountServerService {
[ImportingConstructor]
public AccountServerService (Account account) { ... }
}
To put this into words I need the account
parameter passed into the AccountServerService
constructor instance to be the object of the calling Account
object. So that it act like this:
public class Account {
public IAccountServerService { get { return new AccountServerService (this); } }
}
Please let me know if this scenario is possible or if I have to refactor my service interface in this instance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您可以将循环依赖链中的导入之一更改为惰性导入,那么它应该可以工作。例如:
If you can change one of the imports in the circular dependency chain to be a lazy import it should work. For example:
所以 MEF 确实支持循环依赖,但它们必须都是属性导入,它们都不能是构造函数导入。因此,从 MEF 的角度来看,以下内容应该有效,当然我不确定这种方法是否受到您拥有的其他一些约束的阻碍。
So MEF does support circular dependencies but they must both be property imports, neither of them can be constructor imports. So the following should work from a MEF standpoint, of course I'm not sure if this approach is blocked by some other constraint you have.
我不确定 MEF 中是否可以实现相互递归契约。我会将其分解到下面的内容中,这不需要相互递归的服务合同。
I'm not sure if mutually recursive contracts are or are not possible in MEF. I would factor it out a bit into the following which doesn't require mutually recursive service contracts.