寻找有关 Microsoft Visual Studio 解决方案和项目命名约定的建议

发布于 2024-09-26 12:45:12 字数 647 浏览 2 评论 0原文

似乎没有任何经过验证的最佳实践来指导您如何设置解决方案、项目及其输出的程序集。 Microsoft 似乎在 VS.net 时代尝试过,但他们此后已停用此内容。对于我读到的每一种方法,我都会读到另一种方法,声称相反的方法更好,或者只关注“如果微软愿意……”但实际上没有提供任何解决方案。

看来有很多方法可以做到这一点,所有这些方法似乎都适用于不同群体的情况,因此我想我应该问一下您使用什么约定以及为什么它们适合您的情况。

我希望这将为不同情况、小型开发团体和项目到大型不同地点的开发团体和项目提供一些良好的约定。

您使用什么约定来...

  • 命名您的解决方案,为什么?
  • 为您的项目命名,为什么?
  • 为你的程序集命名,为什么?
  • 知道何时创建新项目或添加到现有项目,为什么?
  • 知道何时将解决方案拆分为更小的解决方案,为什么?
  • 知道何时将一个项目分解为多个项目,为什么?

需要明确的是,在这些答案中,“为什么”与“如何”同样重要。这里和其他地方发布了很多关于如何使用的答案,但很少有人说明为什么他们使用一种约定而不是另一种约定。

There doesn't seem to be any tried and true set of best practices to guide you on how to setup your solutions, projects and the assemblies they output. Microsoft seemed to have tried back in the VS.net days, but they have since retired this content. For every method I read about I read another that claims the opposite is better, or a post that only concerns itself with "if only Microsoft would..." but really provide no solutions.

It appears there are many ways to do this that all seem to work for various groups in their situations, therefore I thought I would ask what conventions YOU use and why they work for YOU in your situation.

I hope that this will provide several good conventions for different situations, small development groups and projects to large diversely located development groups and projects.

What conventions do you use to...

  • name your solutions, and why?
  • name your projects, and why?
  • name your assemblies, and why?
  • know when to create a new project or add to an existing project, and why?
  • know when to split up a solution into smaller solutions, and why?
  • know when to break up a project into multiple projects, and why?

Just to be clear, the WHY is just as import as the HOW in these answers. There are many answers posted on the how here and other places, very few say why they use one convention over another.

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

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

发布评论

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

评论(3

寄意 2024-10-03 12:45:12

这是一个非常广泛的问题,但却是一个很好的问题。我将从用于 ASP.Net Web 项目的简单结构开始(MVC 看起来完全不同)。

解决方案命名对我来说不是什么大问题。我倾向于为特定目的创建解决方案,并将现有项目添加到解决方案中。如果您的解决方案超过 15 个项目(只是粗略数字),请考虑添加其中一些项目作为参考。大多数人不需要同时处理超过 15 个项目。

项目命名对我来说很重要。

// class library that supports the site itself and abstracts
// more complicated UI logic into a separate place
Company.ProductName.Web;

// website
Company.ProductName.Web.UI;

// main business object library for product
//
// of course, you can have as many of these as needed.
Company.ProductName;

我尝试在项目中使用足够的文件夹,以便可以轻松查看文件夹中的所有文件,而无需滚动解决方案资源管理器。

我的典型网络项目看起来像这样。请注意表示命名空间/可编译资源与非命名空间/可编译资源的大小写不同。

  • 客户端(css、javascript)
  • 配置(私有、自定义配置文件,如果有)
  • 内容(母版页、ASPX 和 ASCX,分为逻辑文件夹)
  • 处理程序(ASHX 等)
  • 图像
  • MSBuild(构建脚本)
  • Web 服务(这些应该很小与相关站点直接相关的服务,否则将它们分成一个单独的项目)。

我开始越来越多地使用部分类来创建综合类,这些类可以做很多事情而不会让代码变得混乱。例如,我最近创建了一个 Web 服务,其唯一目的是将 JSON 返回给客户端,但逻辑分布在近十几个部分类中,以便更好地组织它。

希望这能让你开始。

That's a very broad question, but a good one. I will start with a simple structure that I use for ASP.Net web projects (MVC will look completely different).

Solution naming isn't a big deal to me. I tend to create solutions for a specific purpose, and add existing projects to the solutions. If your solution is over 15 projects (just a rough number) consider adding some of those projects as references. Most people don't need to work on more than 15 projects at a time.

Project Naming is a big deal to me.

// class library that supports the site itself and abstracts
// more complicated UI logic into a separate place
Company.ProductName.Web;

// website
Company.ProductName.Web.UI;

// main business object library for product
//
// of course, you can have as many of these as needed.
Company.ProductName;

I try to use enough folders in my projects so that all files in a folder can easily be viewed without scrolling the solution explorer.

My typical web project looks something like this. Note the different in casing to represent namespaced/compilable resources versus those that are not.

  • client (css, javascript)
  • config (private, custom config files, if any)
  • Content (Master Pages, ASPXs and ASCXs, broken into logical folders)
  • Handlers (ASHXs and such)
  • images
  • MSBuild (build scripts)
  • WebServices (these should ONLY be small services that are directly related to the site in question. Otherwise, break them into a separate project).

I've started using partial classes more and more to create comprehensive classes that can do many things without having the code be cluttered. For example, I recently created a web service whose single purpose is to return JSON to the client, but the logic is spread across almost a dozen partial classes to organize it better.

Hope that gets you started.

等往事风中吹 2024-10-03 12:45:12

在我们的例子中,我们使项目名称与我们为特定程序集选择的命名空间完全相同。这样就可以轻松映射物理文件夹中类文件的位置。例如 - CompanyName.BusinessLine.BusinessServiceCompanyName.Framework.Security。因此,如果开发人员正在查看CompanyName.Framework.Security.Cryptography.cs,他可以立即找出该项目并打开该项目。

In our case we keep our project names quite identical to namespaces that we chose for particular assembly. That way it becomes easy to map location of a class file in physical folder. For example - CompanyName.BusinessLine.BusinessService or CompanyName.Framework.Security. So if a developer is looking at CompanyName.Framework.Security.Cryptography.cs, he can immediately figure out the project and open that project.

愛上了 2024-10-03 12:45:12

正如蒂姆所说,这是非常广泛的。需要注意的几点:

  • 解决方案通常只是项目的集合。例如,许多解决方案可以包含相同的项目。因此,这并不重要:如果您不喜欢解决方案名称,您可以将其丢弃,根本不需要重构。
  • 和 Pradeep 一样,我倾向于使用项目包含的顶级命名空间来命名项目。 “更深”的命名空间最终位于子目录中,因此 Foo.Bar.Baz 命名空间中的类可能位于项目 Foo.BarBaz 目录中>。

我倾向于将项目分为:

  • 可重用性元素(例如,一个用于 UI 的程序集,一个用于一组可重用的模型类,一个用于可重用的通用实用程序类)
  • 部署元素(例如,一个用于生产,一个用于测试,成对)
  • 引用元素(例如,如果您有一个公共程序集 Skeety.Common 以及其他类使用的一些接口,则可能有一个 Skeety.Common.Testing 程序集包含类型可帮助您使用 Skeety.Common 测试类)。这导致了这些规则:
    • 生产程序集只能引用其他生产程序集
    • 测试组件只能引用生产组件和其他生产组件
    • 测试程序集(包含测试本身的程序集)只能引用生产和测试程序集,而不能引用其他测试程序集
    • 显然不允许循环引用

在许多情况下,实际上如何拆分事物并不重要 - 但它确实有助于在计算依赖层时使设计更加清晰(因此企业逻辑程序集不应该引用 UI 程序集,但反之亦然)。

太多的项目肯定会减慢你的速度,无论是在构建时间方面还是在确定一切应该在哪里方面。项目太少会使设计不太清晰。随着时间的推移,你可能会对事情应该如何安排有更多的直觉 - 但如果我声称总是知道最好的行动方案,我会感到震惊:)

As Tim says, this is very broad. A few things to note:

  • A solution is usually just a collection of projects. Many solutions can include the same projects, for example. As such, it doesn't matter too much: if you don't like a solution name, you can throw it away with no refactoring at all.
  • Like Pradeep, I tend to name projects with the top level namespace they contain. "Deeper" namespaces end up in subdirectories, so classes within the Foo.Bar.Baz namespace might be in the Baz directory of project Foo.Bar.

I tend to split into projects across:

  • Elements of reusability (e.g. one assembly for a UI, one for a reusable set of model classes, one for a reusable general purpose utility classes)
  • Elements of deployment (e.g. one for production, one for testing, in pairs)
  • Elements of reference (e.g. if you have a common assembly Skeety.Common with some interfaces used by other classes, there might be a Skeety.Common.Testing assembly containing types which help you to test classes using Skeety.Common). This leads to these rules:
    • Production assemblies can only refer to other production assemblies
    • Testing assemblies can only refer to production assemblies and other production assemblies
    • Test assemblies (the ones containing the tests themselves) can only refer to production and testing assemblies, not to other test assemblies
    • No circular references are allowed, obviously

In many cases it actually doesn't matter too much how you split things up - but it does help to make the design cleaner as you work out the dependency layers (so a business logic assembly shouldn't have a reference to a UI assembly, but vice versa is fine).

Having too many project can definitely slow you down, both in terms of build times and just working out where everything should be. Having too few projects makes the design less clear. Over time you're likely to get more of a gut feeling for how things should be laid out - but I'm blowed if I'd claim to always know the best course of action :)

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