使用 [ImportingConstructor] 使用 MEF 将调用对象导入到构造函数参数中

发布于 2024-09-15 02:32:40 字数 726 浏览 3 评论 0原文

我正在将一些代码从专有系统转换为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧城空念 2024-09-22 02:32:40

如果您可以将循环依赖链中的导入之一更改为惰性导入,那么它应该可以工作。例如:

[Import] 
public Lazy<IAccountServerService> { get; set; } 

If you can change one of the imports in the circular dependency chain to be a lazy import it should work. For example:

[Import] 
public Lazy<IAccountServerService> { get; set; } 
起风了 2024-09-22 02:32:40

所以 MEF 确实支持循环依赖,但它们必须都是属性导入,它们都不能是构造函数导入。因此,从 MEF 的角度来看,以下内容应该有效,当然我不确定这种方法是否受到您拥有的其他一些约束的阻碍。

public class AccountServerService : IAccountServerService {
    [Import]
    public Account Account { get; set; }

    public AccountServerService () { ... }
}

public class Account {
    [Import]
    public IAccountServerService { get; set; }
}

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.

public class AccountServerService : IAccountServerService {
    [Import]
    public Account Account { get; set; }

    public AccountServerService () { ... }
}

public class Account {
    [Import]
    public IAccountServerService { get; set; }
}
浅沫记忆 2024-09-22 02:32:40

我不确定 MEF 中是否可以实现相互递归契约。我会将其分解到下面的内容中,这不需要相互递归的服务合同。

interface IAccountFactory {
  Account CreateAccount(IAccountServerService service);
}

[Export(typeof(IAccountFactory))]
sealed class AccountFactory {
  Account CreateAccount(IAccountServerService service) {
    return new Account(service);
  }
}

class Account {
   Account(IAccountServerService service) {
      ...
   }
}

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.

interface IAccountFactory {
  Account CreateAccount(IAccountServerService service);
}

[Export(typeof(IAccountFactory))]
sealed class AccountFactory {
  Account CreateAccount(IAccountServerService service) {
    return new Account(service);
  }
}

class Account {
   Account(IAccountServerService service) {
      ...
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文