简体   繁体   English

Visual Studio:如何管理项目之间共享的代码

[英]Visual Studio : How to manage code shared between projects

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.问题是,每当我需要更新此代码时,我都不想这样做 3 次,这将成为一场噩梦。

Is there a way to add it to a project, without copying it into the project folder?有没有办法将它添加到项目中,而无需将其复制到项目文件夹中? ie I want the shared class to be linked into my 3 projects as即我希望共享类链接到我的 3 个项目中

C:\\code repository\\sharedclass.cs NOT \\eachproject\\bin\\sharedclass.cs C:\\code repository\\sharedclass.cs 不是 \\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.干杯。

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 project's .csproj file, and it will be included in the solution from its original location.正如其他人所说,您只需在解决方案资源管理器中右键单击您的解决方案,选择添加 > 现有项目,然后浏览到公共项目的 .csproj 文件,它将从其原始位置包含在解决方案中。

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 be included in each solution with a relative path to the solution file (ie: ...\\CommonProject\\Common.csproj).公共项目将包含在每个解决方案中,并带有解决方案文件的相对路径(即:...\\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 that 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 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.在公共项目被多个项目(例如两个 - A 和 B)引用并且在项目 A 上工作的开发人员必须对公共项目进行更改作为其任务的一部分的情况下,该开发人员无法知道如果他们所做的更改会破坏项目 B,而无需他们实际检查项目 B 并对其进行编译。 As more and more projects reference the common project, the risk of this happening increases to the point where it becomes unmanageable.随着越来越多的项目引用公共项目,发生这种情况的风险增加到无法管理的程度。

Again, as others 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 standalone project on the server.使用 Cruise Control 等持续集成来管理项目的构建,并将公共项目作为独立项目放在服务器上。

  2. Create a directory under your source control to house built common DLLs.在您的源代码控制下创建一个目录来存放构建的通用 DLL。 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.在您的构建机器上签出此目录,并且无论何时构建公共项目,它都会将输出 DLL 复制到 DLL 文件夹中,并将这些更改提交给源代码管理。

  3. Use environment variables on all developers' machines and the build server to control the location of the common DLL folder and reference the DLLs using that variable rather than the hard-coded path.在所有开发人员的机器和构建服务器上使用环境变量来控制公共 DLL 文件夹的位置,并使用该变量而不是硬编码路径来引用 DLL。 (ie: rather than C:\\Source\\MyCommonProjectDLLS\\Common.dll, use $(MyCommonLocation)\\Common.dll with the variable 'MyCommonLocation' set to C:\\Source\\MyCommonProjectDLLS) (即:而不是 C:\\Source\\MyCommonProjectDLLS\\Common.dll,使用 $(MyCommonLocation)\\Common.dll 并将变量“MyCommonLocation”设置为 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.对于引用公共 DLL 的任何项目,请在构建服务器上为该项目设置 CI 触发器以查看公共 DLL 文件夹。 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 forced to take updates to the common DLL as soon as they are made.唯一的缺点是,在此模型中,消费项目在生成后就被迫更新公共 DLL。 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.另一种方法是在构建时从源控制修订版中对通用 DLL 进行版本控制,并将每个版本放在通用 DLL 文件夹下的自己的子目录中。 So you would end up with:所以你最终会得到:

Common DLLs常见的 DLL
-1.0.0.1234 -1.0.0.1234
-1.0.0.1235 -1.0.0.1235
-1.0.0.1236 -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.这样做的好处是,每个项目都可以通过简单地引用新版本的代码来选择何时对公共 DLL 进行更新。 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 finally bring in those changes.然而,它是双向的,因为这可能意味着某些项目保留旧版本的公共代码的时间比它们应有的时间长,这可能会增加最终引入这些更改时所涉及的工作。

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.因此,将共享代码放入类库并将其添加到您的三个项目中。

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. Microsoft 一直在支持一个现在内置于 VS 中的开源项目,称为 NuGet,您可以将共享项目输出为 nuget 文件并在其他项目中使用它。

It will actually deploy all the files you specify in the package upon build.它实际上会在构建时部署您在包中指定的所有文件。

This is how .Net supports dependencies now.这就是 .Net 现在支持依赖项的方式。 You will notice that even things like EF come through NuGet packages.您会注意到,即使是像 EF 这样的东西也是通过 NuGet 包实现的。 You can even host it for free on places like MyGet.org I use this and it works quite well.您甚至可以在 MyGet.org 之类的地方免费托管它,我使用它并且效果很好。

http://nuget.org/ http://nuget.org/

I use git submodules to achieve this.我使用git 子模块来实现这一点。

  1. Create a new git repository for each module (project) that you want to share between solutions.为要在解决方案之间共享的每个模块(项目)创建一个新的 git 存储库。 I usually also include unit tests for that project in a separate project but in the same git repository.我通常还在一个单独的项目中包含该项目的单元测试,但在同一个 git 存储库中。
  2. Add a submodule to the git repository of the solution that will use the shared code.子模块添加到将使用共享代码的解决方案的 git 存储库。 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 .我发现使用像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".通过右键单击解决方案并选择“添加现有项目”,将项目导入到父 Visual Studio 解决方案中。
  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.您还可以看到引用子模块的其他 git 存储库中的更改。

是的,将需要共享的代码放在一个单独的类库项目中,构建它并将由此构建创建的 DLL 引用到您的其他项目中。

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 .否则,您可以将代码/文件/项目添加为链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有Visual Studio的情况下管理大型项目? - How to manage large projects without Visual Studio? 如何在visual studio中的不同类型的项目之间共享代码? - How to share code between projects of different types in visual studio? 如何在 Visual Studio 代码中的多个项目之间包含共享的 class - how to include a shared class between several project in visual studio code 在Visual Studio下管理多个目标项目 - Manage multiple target projects under Visual Studio 如何在Visual Studio中的项目之间共享资源文件? - How can I share a resource file between projects in Visual Studio? 如何在 Visual Studio 中切换 .NET Core 项目的目标框架 - How to switch between target frameworks for .NET Core projects in Visual Studio 如何使用Visual Studio 2012 Professional管理我的项目版本 - How can I manage my projects versions using Visual Studio 2012 Professional 如何使用 Visual Studio 2022 为解决方案中的所有项目全局管理 nuget 包? - How to manage nuget packages globally for all projects in a solution using Visual Studio 2022? 如何在多个Visual Studio项目之间共享属性? (特别是C#项目) - How to share properties between multiple Visual Studio projects? (especially C# projects) Visual Studio 2010中项目之间的Intellisense - Intellisense between projects in visual studio 2010
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM