MEF 依赖关系和版本控制

发布于 2024-10-16 18:30:25 字数 1039 浏览 3 评论 0原文

我有一个使用 MEF 加载零件的系统。这些部分中的每一个都依赖于一个核心库。当我构建项目时,我向 .dll 文件添加版本号,如下所示:

  • part1-1.0.0.0.dll
  • part2-1.0.0.0.dll

此外,还有一个执行 MEF 组合的应用程序。它还使用核心库。我发现我可以只部署“部分”dll,并且组合工作正常,因为应用程序已经加载了这些部分所依赖的核心库。所以我的文件系统看起来像这样:

  • /parts/part1-v1.dll
  • /parts/part2-v1.dllcomposer-v1.exe
  • core-v1.exe

遇到的麻烦是如何处理核心的版本控制和部分。假设我对核心和其中一个部分进行了更新。然后,我部署更改。所以现在我的文件系统可能看起来像:

  • /parts/part1-v1.dll
  • /parts/part1-v2.dll
  • /parts/part2-v1.dllcomposer-v1.exe
  • core
  • core-v1.dll
  • -v2.dll

如何我可以确保part1-v1.dll使用core-v1.dll,而part1-v2.dll使用core-v2.dll吗?我需要加载所有版本的部件并使用适当版本的核心。

零件类看起来像这样:

[Export(typeof(IPart))]
public class Part1
{
    public string GetSomethingFromCore()
    {
        return Core.GetSomethingFromCore();
    }
}

[Export(typeof(IPart))]
public class Part2
{
    public string GetSomethingFromCore()
    {
        return Core.GetSomethingFromCore();
    }
}

I have a system that uses MEF to load parts. Each of these parts rely on a core library. When I build the project, I add a version number to the .dll files like this:

  • part1-1.0.0.0.dll
  • part2-1.0.0.0.dll

Also, there is an application that performs MEF composition. It also uses the core library. I've found that I can just deploy the "part" dlls, and composition works fine because the application has already loaded the core library that the parts rely on. So my file system looks something like this:

  • /parts/part1-v1.dll
  • /parts/part2-v1.dll
  • composer-v1.exe
  • core-v1.exe

The trouble I'm having is how to handle versioning of the core and parts. Suppose I make an update to the core, and one of the parts. Then, I deploy the changes. So now my file system might look something like:

  • /parts/part1-v1.dll
  • /parts/part1-v2.dll
  • /parts/part2-v1.dll
  • composer-v1.exe
  • core-v1.dll
  • core-v2.dll

How can I make sure that part1-v1.dll uses core-v1.dll, and part1-v2.dll uses core-v2.dll? I need all versions of the parts to be loaded and using the appropriate version of the core.

The part classes look something like this:

[Export(typeof(IPart))]
public class Part1
{
    public string GetSomethingFromCore()
    {
        return Core.GetSomethingFromCore();
    }
}

[Export(typeof(IPart))]
public class Part2
{
    public string GetSomethingFromCore()
    {
        return Core.GetSomethingFromCore();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

几度春秋 2024-10-23 18:30:25

强命名不能解决您的问题吗?如果程序集是针对强命名依赖项构建的,那么您知道它只会接受直到最后一个字节的完全相同的依赖项。

或者,如果强命名过于严格,您可以将版本号放在类型名称中。例如:

[Export(typeof(IPart))]
public class Part1v1
{
    private readonly ICorev1 core;

    [ImportingConstructor]
    public Part1v1(ICorev1 core)
    {
        this.core = core;
    }
}

[Export(typeof(IPart))]
public class Part1v2
{
    private readonly ICorev2 core;

    [ImportingConstructor]
    public Part1v2(ICorev2 core)
    {
        this.core = core;
    }
}

Doesn't strong naming take care of your issue? If an assembly is build against a strong named dependency, then you know that it will only accept the exact same dependency down to the last byte.

Alternatively, if strong naming is too restrictive, you could put version numbers in the type names. For example:

[Export(typeof(IPart))]
public class Part1v1
{
    private readonly ICorev1 core;

    [ImportingConstructor]
    public Part1v1(ICorev1 core)
    {
        this.core = core;
    }
}

[Export(typeof(IPart))]
public class Part1v2
{
    private readonly ICorev2 core;

    [ImportingConstructor]
    public Part1v2(ICorev2 core)
    {
        this.core = core;
    }
}
○闲身 2024-10-23 18:30:25

您需要为核心程序集的所有部件指定强名称,然后加载引用的程序集时,它们将需要完全匹配。这也意味着您将需要部署核心程序集的多个副本。即代替

  • /parts/part1-v1.dll
  • /parts/part1-v2.dll
  • /parts/part2-v1.dllcomposer-v1.exe
  • core
  • core-v1.dll
  • -v2.dll

您将拥有:

  • /parts/1 -1/part1-v1.dll
  • /parts/1-1/core-v1.dll
  • /parts/1-2/part1-v2.dll
  • /parts/1-2/core-v2.dll
  • /parts/2-1 /part2-v1.dll
  • /parts/2-1/core-v1.dllcomposer-v1.exe
  • core
  • core-v1.dll
  • -v2.dll

我过去这样做的方式只是存储每个部分与它需要的所有依赖项一起放在一个单独的文件夹中。即使它们(当前)与应用程序中的版本相同。这样,当您的应用程序转移到 core-v2 时,所有依赖 core-v1 的部分仍将拥有它。

You need to give your core assembly all of your parts strong names, then they will require an exact match when loading the referenced assemblies. This also means you will need to deploy multiple copies of your core assembly. I.e. instead of

  • /parts/part1-v1.dll
  • /parts/part1-v2.dll
  • /parts/part2-v1.dll
  • composer-v1.exe
  • core-v1.dll
  • core-v2.dll

You will have:

  • /parts/1-1/part1-v1.dll
  • /parts/1-1/core-v1.dll
  • /parts/1-2/part1-v2.dll
  • /parts/1-2/core-v2.dll
  • /parts/2-1/part2-v1.dll
  • /parts/2-1/core-v1.dll
  • composer-v1.exe
  • core-v1.dll
  • core-v2.dll

The way I've done that in the past, is just to store each part in a separate folder along with all of the dependencies it needs. Even if they are (currently) the same version as in the application. So that when you're application moves on to core-v2, all the parts that relied on core-v1 will still have it.

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