简体   繁体   中英

Creating and maintaining same codebase (class library) with multiple assembly versions as dependency

Given following source code which need to be maintained within a class library project:

using Newtonsoft.Json;

namespace Zephyr.SharedProject.Core
{
    public class TestClass
    {
        public TestClass()
        {
            string json = JsonConvert.SerializeObject(new
            {
                PropertyA = 1
            });
        }
    }
}

What are the options do we have if our class library need to support different versions of Newtonsoft.Json ?

For example, it was known that the product which consumes the class library have following dependencies:

Product version Newtonsoft.Json version
1 10.0.1
2 11.0.1
3 12.0.1

Assuming Newtonsoft.Json does not introduce breaking change and same source code can be used with all version above, how would one create and maintain Visual Studio solution to support scenarios above?

I'm thinking having a single project which holds the source code and creating each version-specific project by adding the source code as link 1 with corresponding dependencies which looks like following:

  • Solution
    • Core Project references Newtonsoft.Json v???
      • TestClass (source code)
    • Project_v1 references Newtonsoft.Json v10.0.1
      • TestClass (added as link)
    • Project_v2 references Newtonsoft.Json v11.0.1
      • TestClass (added as link)
    • Project_v3 references Newtonsoft.Json v12.0.1
      • TestClass (added as link)

1 Right click project, Add > Existing Item > Add As Link

Having project structure above would allow us to maintain a single file and each project can have their own dependency which is fine where we can have unit test for each project as well.

However I'm in dilemma to define the dependency on Core Project as it's ambiguous and would shows compilation error in Visual Studio due to missing reference.

I'm aware binding redirect would solve the problem at consumer-side for version mismatch but I'm interested with the solution from producer (class library) perspective, kindly enlighten me if there's any better approach, cheers!

A shared project can be created in Microsoft Visual Studio which acts a central repository that contains the source codes or files.

Visual-Studio-添加新项目

The project itself doesn't require any references which can then be added as reference for version specific projects.

When open the source code in editor, one can easily switch between the context of referenced projects to make sure everything's good in case there are any conflict due to different dependencies.

Visual-Studio-编辑器

The final project structure would then looks similar to:

Product version Project type Newtonsoft.Json version
All Shared N/A
1 Class library 10.0.1
2 Class library 11.0.1
3 Class library 12.0.1

P/S: This feature has been around for quite some time and I just recently found out about it, hopefully the information provided helps!

Extra: channel 9 video - Sharing Code Across Platforms With Visual Studio 2015

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM