VS.NET:项目引用与程序集引用

发布于 2024-08-17 05:14:01 字数 622 浏览 2 评论 0原文

关于从其他项目引用我们的公共代码库哪个更好:通过项目还是通过程序集,存在一些争论。我赞成引用该项目,特别是因为我们有自动化的单元测试,可以证明通用代码可以满足其需要。

另一个阵营的想法是锁定这些项目,每月只发布一次程序集或类似的东西。然后强制所有项目引用程序集。他们认为这将保护他们免于部署未经测试的代码。他们“太忙”了,无法编写自动化单元测试并配置他们的项目以进行持续集成,而我对此没有影响力,所以请不要关注这方面。

以下是我能想到的项目参考是更好解决方案的原因。我也在寻找其他意见。

优点:

  • 引用项目可确保您使用最新的代码。您无需等待任何事情。
  • 减少重复。如果没有最新的代码,则更有可能重新发明轮子。
  • 如果开发人员需要某些东西,但无法将其添加到其所属的程序集中,那么它将在任何可用的位置创建它,从而造成许多不一致和代码重复。
  • 开发变得更加容易,因为您可以轻松查看/调试引用代码中发生的情况。
  • 我们常见的东西不会经常改变,但当改变时,它通常是有用的。为什么要增加维护和程序集发布管理的额外负担。

缺点:

  • 加载可能需要更长的时间。
  • 将项目添加到新解决方案然后添加程序集引用可能需要稍长的时间。

There's some debate here over which is better for referencing our common code base from other projects: by project or by assembly. I'm in favor of referencing the project, especially since we have automated unit tests that prove that the common code does what it needs to.

The thought from the other camp is to lock down those projects and only release assemblies once a month or something. Then force all projects to reference the assemblies. They think this will protect them from deploying untested code. They are "too busy" to write automated unit tests and configure their projects for continuous integration and I have no influence on that, so please do not focus on this aspect.

Here's the reasons I can think of why project references are the better solution. I'm looking for other opinions as well.

PROS:

  • Referencing projects ensures you are working with the latest code. You don't have to wait on anything.
  • Reducing duplication. Without having the latest code, there is a greater chance of reinventing the wheel.
  • If a developer needs something and can't add it to the assembly where it belongs, it will be created in any location that will work, creating many inconsistencies and code duplication.
  • Development is easier because you can easily see/debug what is happening in the referenced code.
  • Our common stuff doesn't change that often, but when it does, it's usually something useful. Why add the extra burden of maintenance and assembly release management.

CONS:

  • Could possibly take longer to load.
  • Can take slightly longer to add projects to a new solution then adding assembly references.

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

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

发布评论

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

评论(3

寻梦旅人 2024-08-24 05:14:01

以下是您错过的实时更新的一些优点

  • : 当您更改 API 转到定义时,智能感知等功能将在项目到项目引用之间自动更新
  • :如果您有程序集引用,转到定义将带您转到实际的代码定义。通过程序集引用,它将带您到生成的元数据签名。
  • 查找所有引用:将处理项目引用中的所有代码以供参考使用。对于程序集引用,您只能在元数据中看到使用
  • 快速搜索(仅限 2010 年): 与查找所有引用类似,只是在 P2P 引用中效果更好

针对您的缺点

  • 是:加载项目通常比加载引用慢。对于合理数量的项目,虽然这个时间差异并不显着,并且不会影响您的日常开发例程
  • 是:向解决方案添加项目通常比添加引用慢。但差异以秒为单位,并且是一次性成本。我认为将此视为标准的一部分是错误的。

Here are a couple of pros you missed

  • Live Updating: Features like intellisense will update automatically between project to project references as you are changing the APIs
  • GoTo Definition: If you have an assembly reference, GoTo Definition will take you to the actual code definition. With an assembly reference it will take you to the generated metadata signature.
  • Find All References: Will process all code in project references for use of a reference. For an assembly reference you will only see uses within the metadata
  • Quick Search (2010 only): Similar to find all references, just works better in a P2P reference

In response to your cons

  • Yes: Loading a project is typically slower than loading a reference. With a reasonable amount of projects though this time difference is not significant and should not impact your daily development routine
  • Yes: Adding adding a project to the solution is generally slower than adding a reference. The difference though is in seconds and is a one time cost. I think it's a mistake to consider this as part of the criteria.
二智少女猫性小仙女 2024-08-24 05:14:01

好吧,如果您使用任何类型的源代码控制,您就可以满足两个阵营。使用分支或标签(取决于您的源代码管理)。

基本上,如果您有一个团队控制您的通用代码,他们可以分支/标记稳定版本。然后您的项目将使用稳定版本。这可以防止您为其他项目部署不稳定的代码,同时为您提供测试、调试和查看所有源代码的能力。

每当您的公共控制团队创建新的稳定版本时,您都可以更新源引用以从新分支中提取。

这种情况的另一个好处是您可以对通用代码进行补丁,以免扰乱当前正在进行的开发工作。当然,这确实增加了额外的管理负担,需要在多个地方更新公共代码。

Well, if you are using any type of source control you could satisfy both camps. Using Branching or Labeling (depending on your source control).

Basically if you have a team that is in control of your common code they can branch / label a stable release. Then your projects would use the stable release. This protects you from deploying unstable code for your other projects while providing you the ability to test, debug, and view all the source.

Whenever your common controls team creates a new stable build you can update your source references to pull from the new branch.

The other up side to this scenario is you could have a patch on the common code so as not to mess with the current ongoing development efforts. Of course this does add an extra management burden of updating the common code in more than one place where necessary.

千笙结 2024-08-24 05:14:01

引用策略将始终是一个常见问题

我知道这是一个非常古老的问题,但它始终是一个具有相关答案的相关问题,我记得几年前也遇到过同样的问题以及该问题的影响两种选择(项目与程序集参考)只是在过去两年里我才变得明显起来,当时我正在全职开发一个系统的构建器,该系统包含 800 多个项目,单个 WPF 应用程序有 300 多个解决方案。

项目引用对于 VISUAL STUDIO 来说是理想的

项目引用是理想的,因为您可以让 IDE 更深入地了解代码的详细信息,因为您明确告诉 Visual Studio 引用的项目及其所有代码的位置。如果您正在使用的系统少于 200 个项目模块,并且只能拥有几个解决方案(项目分组),请查找项目参考,因为 Visual Studio 可以在设计时使用这些额外信息为您做更多事情,例如向您展示代码而不是反映引用的程序集。

汇编引用可以更好地扩展

如果您的系统远大于 200 个项目,您的构建可能会变得非常慢。我见过每个构建 20 分钟,这真的很糟糕。因此,如果您可以引用一个不会更改的 DLL,那么您就是在告诉 Visual Studio“不”构建它,这显然会对构建时间产生一些影响。

汇编引用创建了一个解耦系统的幻象

项目引用为您提供了一个更智能的 IDE,它始终知道在何处以及在多少位置使用了类型、可以在哪里找到代码以及其他一些有用的统计信息,因为所有项目都直接引用。当您无意中尝试创建循环引用时,它还会向您发出警告。

程序集引用的优点:

  1. 通过将某些项目视为外部依赖项,您可以创建更小的解决方案和更小的相关项目分组,因为不相关的项目被视为外部程序集
  2. 允许 Visual Studio 相当大地处理非常大的模块化系统出色地。
  3. 强制您在构建过程中制定 dll/程序集暂存策略。

程序集引用的缺点:

  • 循环引用成为可能,经验不足的开发人员最初不会注意到这些引用。

    <块引用>

    您可以通过将其中一个程序集视为已签入的第三方程序集、将其转移到下一个版本并最后构建它来解决循环引用的这一挑战。当使用此排除程序集的项目上的构建失败时,您可以决定重建该指定项目,并且通常会消除循环依赖关系。 (不推荐,但有可能)

    循环引用

  • 向上框架引用也成为可能,因为您添加了对针对更高.net框架编译的dll的引用,并且当发生这种情况时,Visual Studio不会给您明确的根本原因消息,因此您将在找出修复框架版本之前,尝试了很多方法来解决问题并失败了很多次。

    <块引用>

    新项目默认使用最新版本的 .net 框架,没有考虑到当前解决方案中的所有项目仍处于早期版本的 .net 中这一事实,因此当您使用程序集引用添加对该项目的引用时,您经常会遇到一个不太容易弄清楚的奇怪错误。

  • 跨分支引用也成为可能,因为您可能同时在多个分支中工作,有时在添加引用时会感到困惑。

    <块引用>

    您单击“添加引用”,向上移动几个目录,向下移动几个目录,选择 dll 并单击添加,但没有意识到该 dll 实际上与您正在处理的另一个分支中的位置太远了。只有当构建服务器尝试解析该程序集并失败时,这个问题才会变得明显。以及错误“未知的命名空间,您是否缺少程序集引用?”根本不会帮助你浪费整个团队的时间。

REFERENCING STRATEGY WILL ALWAYS BE A COMMON PROBLEM

I know this is a very old question but it will always be a relevant question with relative answers and I remember having this same question a few years ago and the impact of the two choices (project versus assembly references) only became apparent to me during the last two years while I was working full time on a builder for a system with 800+ projects in 300+ solutions for a single WPF app.

PROJECT REFERENCES ARE IDEAL FOR VISUAL STUDIO

Project references are ideal because you give the IDE much more insight into the details of your code because you are explicitly telling Visual Studio where the referenced project and all of its code is. If you are working on a system with less than 200 project modules and can afford to have only a few solutions (project groupings), go for project references because Visual Studio can do more for you with that extra information during design-time like showing you the code instead of reflecting referenced assemblies.

ASSEMBLY REFERENCES CAN SCALE BETTER

If your system is much larger than 200 projects, your builds may become very slow. I've seen 20 minutes per build and that really sucks. So if you can reference a DLL that doesn't change, you are telling Visual Studio "NOT" to build it, and that obviously has some impact on the time to build.

ASSEMBLY REFERENCES CREATE AN ILLUSION OF A DECOUPLED SYSTEM

Project references offer you a smarter IDE that always knows where and in how many places a type is used, where the code can be found, and some other useful statistics because all the projects are referenced directly. It can also warn you when you inadvertently tried to create a circular reference.

PROS of assembly references:

  1. By treating some projects as external dependencies, you can create smaller solutions and smaller groupings of related projects, because unrelated projects are treated as external assemblies
  2. Allows Visual Studio to handle very large modularized systems quite well.
  3. Forces you to work out a dll/assembly staging strategy during the build process.

DISADVANTAGES of assembly references:

  • Circular references become possible, and your less experienced developers won't notice those references initially.

    You can get around this challenge of circular references by treating one of your assemblies as a third-party assembly that its checked in, carrying it over to next version, and building it last. When a build fails on a project that uses this excluded assembly you can decide to rebuild that nominated project and often times massage out the circular dependency. (NOT RECOMMENDED, but it is possible)

  • Upward framework references become possible as well because you add references to dlls that are compiled against a higher .net framework, and Visual Studio doesn't give you clear root cause messages when this happens so you will try many things to fix the problem and fail many times before figuring out to fix the framework versions.

    New projects default to the latest version of the .net framework not considering the fact that all projects in the current solution are still in an earlier version of .net, so when you add a reference to that project using an assembly reference, you will often times get a strange error that is not so easy to figure out.

  • Cross-branch references also become possible because you are probably working in more than one branch at the same time, and sometimes get confused while adding a reference.

    You click "add reference", go a few directories up, go a few directories down, select the dll and click add without realising that the dll is actually too far up and down into another branch that you are also working on. This problem will only become apparent much later when the build server tries to resolve that assembly and fails. And the error "unknown namespace, are you missing an assembly reference?" will not help you at all wasting your whole team's time.

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