简体   繁体   English

.NET解决方案 - 许多项目与一个项目

[英].NET solution - many projects vs one project

We currently have a rapidly growing C# codebase. 我们目前有一个快速增长的C#代码库。 Currently we have about 10 projects, split up in the usual categories, common/util stuff, network layer, database, ui components/controls etc. 目前我们有大约10个项目,分为通常类别,common / util东西,网络层,数据库,ui组件/控件等。

We run into the occasional circular dependency where project x depends on something in y and vice-versa. 我们遇到偶尔的循环依赖,其中项目x依赖于y中的某些东西,反之亦然。 We are looking at maybe collapsing the projects down to one and just managing using structure folders/namespaces. 我们正在考虑将项目简化为一个,只使用结构文件夹/命名空间进行管理。 We have a Java project which of course organises just using folders/packages so we're not sure what, if any, benefit having multiple projects brings. 我们有一个Java项目,当然只使用文件夹/包进行组织,所以我们不确定有多个项目带来的好处(如果有的话)。 None of our projects require special project properties, except the main run project, which we may kept separate (and very thin). 我们的项目都不需要特殊的项目属性,除了主要运行项目,我们可以将它们分开(并且非常薄)。

Does anyone have any prior experience in why one project is better/worse than multiple projects and could suggest the best approach? 有没有人有任何先前的经验,为什么一个项目比多个项目更好/更差,并可以建议最好的方法? And also any issues with circular dependencies would be useful in either approach would be useful. 而且循环依赖的任何问题在这两种方法中都很有用。

Any input appreciated. 任何输入赞赏。

如果您的项目具有循环依赖性,则表明代码设计存在问题,而不是解决方案/项目模型。

In my experience, separating code which creates a single executable in multiple projects can be useful if you want to 根据我的经验,如果您愿意,在多个项目中创建单个可执行文件的代码分离可能很有用

  • use different programming languages in different parts, 在不同的部分使用不同的编程语言,
  • develop libraries that are also used by other applications, or 开发也被其他应用程序使用的库,或
  • conceptually separate multiple layers (ie, let Visual Studio ensure that there are no direct references from project Lib to project App ). 在概念上分离多个层(即,让Visual Studio确保没有项目Lib到项目App的直接引用)。

Personally, I base most of my decisions on the second point. 就个人而言,我的大多数决定都是基于第二点。 Do I think that part of the application can be a more general library that I am likely to need in other application? 我认为应用程序的一部分可能是我在其他应用程序中可能需要的更通用的库吗? Put it in a separate project. 把它放在一个单独的项目中。 Otherwise, as you point out, having a single project usually makes development easier. 否则,正如您所指出的,拥有一个项目通常会使开发变得更容易。

About the circular dependencies: The recommended way to solve this is to put interfaces of the referenced stuff into a third project. 关于循环依赖:推荐的解决方法是将引用的东西的接口放入第三个项目。 For example, if you have two applications both sharing some objects through remoting, you put interfaces of the shared objects in a library project to ensure that they are available to both applications. 例如,如果有两个应用程序都通过远程处理共享某些对象,则将共享对象的接口放在库项目中,以确保它们对两个应用程序都可用。

Without knowing the exact design of your application, it's difficult to give more concrete advise. 如果不知道应用程序的确切设计,就很难给出更具体的建议。

When making dependencies between projects, it helps to always think of one as "Lower" and the other as "Higher" 在项目之间建立依赖关系时,总是将其视为“较低”,将另一个视为“较高”

A higher level project (such as a web interface) should only depend on lower projects. 更高级别的项目(例如Web界面)应该仅依赖于较低的项目。 A lower project (such as a utility) should never depend on something higher, such as a web interface. 较低的项目(例如实用程序)不应该依赖于更高的东西,例如Web界面。 If this happens, it either means your higher level project has something that really should be in the lower project, or vice versa. 如果发生这种情况,则意味着您的更高级别的项目确实应该在较低的项目中,反之亦然。

Generally speaking, having multiple VS projects (within a VS solution) does just make sense in these cases 一般来说,拥有多个VS项目(在VS解决方案中)在这些情况下才有意义

  • You can potentially reuse the produced DLL in another project (a class library) 您可以在另一个项目(类库)中重用生成的DLL
  • You want to separate things like in a layered architecture where you may drop the DAO dll and exchange it with another 您希望将分层​​架构中的内容分开,您可以在其中删除DAO dll并将其与另一个进行交换
  • There are just different front-end projects (ie ASP.net MVC apps) which need to be deployed in different physical locations but use the same BL, DAL. 只有不同的前端项目(即ASP.net MVC应用程序)需要部署在不同的物理位置,但使用相同的BL,DAL。

If your saying you're having the problem of circular dependencies, then you're having a problem in your code design. 如果你说你有循环依赖的问题,那么你的代码设计就会遇到问题。 Probably you may put that logic which is used by multiple projects inside a class library designed to be reused in many projects. 也许您可以将多个项目使用的逻辑放在一个类库中,该类库被设计为在许多项目中重用。

Generally I'd say you shouldn't add more projects if you don't really need it. 一般来说,如果你真的不需要,我会说你不应该添加更多的项目。 Splitting up into projects means adding more complexity, so when you're doing so, you should gain a reasonable benefit from it. 分裂成项目意味着增加更多的复杂性,所以当你这样做时,你应该从中获得合理的好处。

We've noticed that Visual Studio's performance degrades significantly as the number of projects grows. 我们注意到随着项目数量的增长,Visual Studio的性能会显着下降。 Something as simple as switching from 'Debug' to 'Release' configurations can takes upwards of 15 seconds for solutions with around a dozen C# projects in them. 从“调试”切换到“发布”配置这样简单的事情,对于包含大约12个C#项目的解决方案,可能需要15秒。

Also, as a counter point to Reed's comment about build times, I've seen build times grow because Visual Studio seems to be spending a lot of time on the project overhead. 另外,作为Reed关于构建时间的评论的反对点,我看到构建时间增长,因为Visual Studio似乎花费了大量时间在项目开销上。 The actual compile times seem fast, but the total time from hitting build to being able to run is significant. 实际的编译时间似乎很快,但是从命中构建到能够运行的总时间非常重要。

My advice would be keep the number of projects to the minimum you can get away with. 我的建议是将项目数量保持在最低限度。 If you need multiple projects for good reasons then use them as necessary, but prefer to keep things together. 如果你有充分的理由需要多个项目,那么必要时使用它们,但更喜欢将它们放在一起。 You can also refactor to split a project into two if necessary. 如有必要,您还可以重构将项目拆分为两个。

There are several reasons for separating a solution into different projects (and thus assemblies), and it mainly comes down to re-usabilit y and separation of responsibilities . 将解决方案分成不同的项目(以及组件)有几个原因,主要归结为可重新使用责任分离

Now your goal should be to make an assembly (aka project) has the minimum amount of dependencies on other assemblies in your solution, otherwise you may as well have everything in fewer assemblies. 现在,你的目标应该是使一个程序集(也称为项目)对解决方案中的其他程序集具有最小的依赖性,否则你可能拥有更少程序集中的所有内容。 If for example your UI components have a strong dependency on your data access code then there is probably something wrong. 例如,如果您的UI组件对您的数据访问代码有很强的依赖性,则可能存在问题。

Really, this comes down to programming against common interfaces. 实际上,这归结为针对通用接口的编程。

Note However: 注意但是:

When I say "otherwise you may as well have everything in fewer assemblies", I wasn't necessarily suggesting this is the wrong thing to do. 当我说“否则你可能拥有更少的装配中的所有东西”时,我并不一定暗示这是错误的做法。 In order to achieve true separation of concerns your going to be writing a lot more code and having to think about your design a lot more. 为了实现真正的关注点分离,您需要编写更多代码,并且必须更多地考虑您的设计。 All this extra work may not be very beneficial to you, so think about it carefully. 所有这些额外的工作对你来说可能不是很有益,所以仔细考虑一下。

You might find the following Martin article worthwhile: Design Principles and Design Patterns (PDF)(Java) . 您可能会发现以下Martin文章值得: 设计原则和设计模式(PDF)(Java)

A revised version in C# specifically is available in Agile Principles, Patterns, and Practices in C# also by Martin. C#中的修订版本特别适用于C#中的敏捷原则,模式和实践

Both express different guidelines that will help you decide what belongs where. 两者都表达了不同的指导方针,可以帮助您确定属于哪里。 As pointed out, however, cyclic dependencies indicate that there are either problems with design or that something is in a component that belongs in a different one. 然而,正如所指出的,循环依赖性表明存在设计问题或者某个组件属于不同组件。

Multiple projects allows better reuse of specific types within multiple applications. 多个项目允许在多个应用程序中更好地重用特定类型。 It can also improve build time, since certain projects will not need to be rebuilt for all code changes. 它还可以缩短构建时间,因为不需要为所有代码更改重建某些项目。

A single project makes life easier, since you don't have to worry about dependencies. 单个项目使生活更轻松,因为您不必担心依赖性。 Just realize that the ease comes at a cost - it also makes it easier to let poor design decisions creep into the code base. 只是意识到轻松是有代价的 - 它也使得更容易让糟糕的设计决策蔓延到代码库中。 Circular dependencies, whether in one project or multiple, are typically a design flaw, not a requirement. 循环依赖关系,无论是在一个项目还是多个项目中,通常都是设计缺陷,而不是要求。

Where I work, we opted for an approach where the aim is to have a single project per solution. 在我工作的地方,我们选择了一种方法,其目标是每个解决方案都有一个项目。 All code library projects also have a test harness application and/or a unit test app. 所有代码库项目还具有测试工具应用程序和/或单元测试应用程序。

As long as the code libraries pass testing, the release versions (with Xml Documentation file of course) get transferred into a “Live” folder. 只要代码库通过测试,发布版本(当然使用Xml文档文件)就会转移到“Live”文件夹中。

Any projects that requires functionality from these other projects have to reference them from the “Live” folder. 任何需要这些其他项目功能的项目都必须从“实时”文件夹中引用它们。

The advantages are pretty clear. 优点非常明显。 Any project always accesses known working code. 任何项目总是访问已知的工作代码。 There is never a chance of referencing a work in progress assembly. 永远不可能引用正在进行的工作装配。 Code gets tested per assembly, making it far easier to understand where a bug originates. 每个程序集都会对代码进行测试,从而更容易理解bug的来源。 Smaller solutions are easier to manage. 更小的解决方案更易于管理。

Hope this helps! 希望这可以帮助!

Shad

Start with single project. 从单个项目开始。 The only benefit in splitting your codebase into more projects is simply to improve build time. 将代码库拆分为更多项目的唯一好处就是缩短构建时间。

When I have some reusable functionality that I really want to isolate from main project, I'll just start brand new solution for it. 当我有一些可重用的功能,我真的想要从主项目中分离出来时,我会为它开始全新的解决方案。

暂无
暂无

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

相关问题 使用 VS 2017 映像,无法在具有一个 .NET Core 3 项目的解决方案中构建其他项目 - Using VS 2017 image, unable to build other projects in a solution which has one .NET Core 3 project 一个解决方案中的多个VS项目 - Multiple VS Projects in One Solution .NET Intellisense:一个解决方案中的 2 个项目 - .NET Intellisense: 2 projects in one solution Gloabal Json文件在一个解决方案中与许多项目共享(asp.net core 2.0) - Gloabal Json file shared with many projects in one solution (asp.net core 2.0) .Net解决方案中的多个控制台应用程序项目要在一个控制台应用程序项目中引用 - .Net multiple console apps projects in a solution to be a referenced in one console app project 如何在一个解决方案中共享源项目之间的许多二进制文件? - How to share many binary files between source projects in one solution? 在一个ASP.NET MVC 5解决方案中具有单独的项目 - Having separate projects in one ASP.NET MVC 5 solution 如何在一个.NET解决方案中跨项目进行通信? - How to communicate across projects inside one .NET solution? 将解决方案中的项目合并到一个项目中? - Merge projects in solution into a single project? 将 Web 应用项目添加到我的解决方案中,并且解决方案中的其他项目已经使用 .net 框架,我可以将 .net 核心用于新项目吗? - adding a web app project to my solution, and the other projects in the solution already use .net framework, can I use .net core for the new project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM