Visual Studio : How to manage code shared between

2020-06-12 05:00发布

This has probably been posted before, but I'm not sure what search terms to look for!

Quick explanation.

I have code that is shared between a few projects. This code is still work-in-progress itself. The issue is that whenever I need to update this code for whatever, I don't want to have to do it 3 times, this will become a nightmare.

Is there a way to add it to a project, without copying it into the project folder? i.e. I want the shared class to be linked into my 3 projects as

C:\code repository\sharedclass.cs NOT \eachproject\bin\sharedclass.cs

Do I have to create it as it's own library project? It would be much better if the compiler could compile it as 'external' code.

Cheers.

6条回答
一夜七次
2楼-- · 2020-06-12 05:22

Yes, put the code which need to be shared in a separate class library project, build it and reference the DLL created from this build into your other projects.

查看更多
啃猪蹄的小仙女
3楼-- · 2020-06-12 05:30

It is better to extract common part into a separate project library and add reference of this project to all the solutions/dependent projects.

Otherwise you can Add code/file/item as Link.

查看更多
趁早两清
4楼-- · 2020-06-12 05:30

Yes.

You can add a project from anywhere on your hard drive to a solution. So put the shared code into a class library and add that to your three projects.

查看更多
时光不老,我们不散
5楼-- · 2020-06-12 05:36

As others have said, you can simply right-click on your solution in the solution explorer, select Add > Existing Project and browse to the common projects .csproj file and it will be included in the solution from its original location.

There are two problems with this however which may or may not be an issue, depending on the size of your team:

1 - The common project will included in each solution with a relative path to the solution file (IE: ...\CommonProject\Common.csproj). This means all developers have to have the same working file structure or they will get errors when they try to open the main project.

2 - In the scenario there the common project is referenced by multiple projects (say two - A and B) and a developer working on project A has to make changes to to the common project as part of their task. There is no way for that developer to know if the changes they have made will break project B without them actually checking out project B and compiling it. As more and more projects reference the common project, the risk of this happening increases to the point where it becomes unmanageable.

Again, as other's have said, there is no 'correct' way to do this, however the approach I have taken is as follows:

1 - Use continuous integration such as Cruise Control to manage the building of the projects and put the common project as a stand alone project on the server.

2 - Create a directory under your source control to house built common DLL's. Have this directory checked out on your build machine and whenever the common project builds, it copies the output DLL into the DLL folder and commits these changes to source control.

3 - Use environment variables on all developers machines and the build server to control the location of the common DLL folder and reference the DLL's using that variable rather than the hard-coded path. (IE: rather than C:\Source\MyCommonProjectDLLS\Common.dll use $(MyCommonLocation)\Common.dll with the variable 'MyCommonLocation' set to C:\Source\MyCommonProjectDLLS)

4 - For any project which references the common DLL, set up a CI trigger on the build server for that project to watch the common DLL folder. Whenever changes are committed to it, the build server should then build all consuming projects.

This immediately lets you know if you are committing breaking changes for any other project. The only drawback is that, in this model, consuming projects are forces to take updates to the common DLL as soon as they are made. An alternative is to version the Common DLL from the source control revision when it is built, and place each version in its own sub directory under the common DLL folder. So you would end up with:

Common DLLs
-1.0.0.1234
-1.0.0.1235
-1.0.0.1236

And so on. The advantage of this is that each project can then choose when to take updates to the common DLL by simply referencing the new version of the code. However it cuts both ways as this can mean that some projects are left with older versions of the common code for longer than they should, which can increase the work involved when the time comes to final bring in those changes.

Hope this helps.

查看更多
再贱就再见
6楼-- · 2020-06-12 05:37

I use git submodules to achieve this.

  1. Create a new git repository for each module (project) that you want to share between solutions. I usually also include unit tests for that project in a separate project but in the same git repository.
  2. Add a submodule to the git repository of the solution that will use the shared code. Adding a submodule creates a link to a specific commit of an external repository. When the code in the submodule is updated you will be able to pull the updates to your parent solution, which is essentially the same as updating the reference to the submodule commit. I find that the process is easier to visualise using an app like SourceTree.
  3. Adding the submodule and pulling the latest commit will create a copy of the shared project inside the parent solution folder. Import the project into the parent Visual Studio solution by right-clicking on the solution and selecting "Add existing project".
  4. Add a reference to the shared project in the other projects that will be using it by right-clicking on the project and selecting "Add Reference" and finding the shared project in the "Solution" tab.

Now that the shared project is included in the solution you will be able to push and pull changes to the submodule and these changes will automatically be incorporated into the solution. You will also be able to see the changes in other git repositories that reference the submodule.

查看更多
唯我独甜
7楼-- · 2020-06-12 05:44

Microsoft has been supporting an open source project which comes built into VS now, its called NuGet, you can output your shared project as a nuget file and consume it in your other projects.

It will actually deploy all the files you specify in the package upon build.

This is how .Net supports dependencies now. You will notice that even things like EF come through NuGet packages. You can even host it for free on places like MyGet.org I use this and it works quite well.

http://nuget.org/

查看更多
登录 后发表回答