Folders or Projects in a Visual Studio Solution? [

2020-05-14 19:25发布

问题:

When spliting a solution in to logical layers, when is it best to use a separate project over just grouping by a folder?

回答1:

By default, always just create new folder within the same project

  • You will get single assembly (without additional ILMerge gymnastic)
  • Easier to obfuscate (because you will have less public types and methods, ideally none at all)

Separating your source code into multiple projects makes only sense if you...

  • Have some portions of the source code that are part of the project but not deployable by default or at all (unit tests, extra plugins etc.)
  • More developers involved and you want to treat their work as consumable black box. (not very recommended)
  • If you can clearly separate your project into isolated layers/modules and you want to make sure that they can't cross-consume internal members. (also not recommended because you will need to decide which aspect is the most important)

If you think that some portions of your source code could be reusable, still don't create it as a new project. Just wait until you will really want to reuse it in another solution and isolate it out of original project as needed. Programming is not a lego, reusing is usually very difficult and often won't happen as planned.



回答2:

Separating features into projects is often a YAGNI architecture optimization. How often have you reused those separate projects, really? If it's not a frequent occurrence, you're complicating your development, build, deployment, and maintenance for theoretical reuse.

I much prefer separating into folders (using appropriate namespaces) and refactoring to separate projects when you've got a real-life reuse use case.



回答3:

denny wrote:

I personally feel that if reusable code is split into projects it is simpler to use other places than if it is just in folders.

I really agree with this - if you can reuse it, it should be in a separate project. With that said, it's also very difficult to reuse effectively :)

Here at SO, we've tried to be very simple with three projects:

  • MVC Web project (which does a nice job of separating your layers into folders by default)
  • Database project for source control of our DB
  • Unit tests against MVC models/controllers

I can't speak for everyone, but I'm happy with how simple we've kept it - really speeds the builds along!



回答4:

I usually do a project for the GUI a project for the business logic a project for data access and a project for unit tests.

But sometimes it is prudent to have separation based upon services (if you are using a service oriented architecture) Such as Authentication, Sales, etc.

I guess the rule of thumb that I work off of is that if you can see it as a component that has a clear separation of concerns then a different project could be prudent. But I would think that folders versus projects could just be a preference or philosophy.

I personally feel that if reusable code is split into projects it is simpler to use other places than if it is just in folders.



回答5:

Separating your source code into multiple projects makes only sense if you... ... More developers involved and you want to treat their work as consumable black box. (not very recommended) ...

Why isn't this recommended? I've found it a very useful way to manage an application with several devs working on different portions. Makes checkins much easier, mainly by virtually eliminating merges. Very rarely will two devs have to work on the same project at the same time.



回答6:

If you do go for creating several projects, make sure everyone who adds code to the solution is fully aware of the intention of them and do everything you can to get them to understand the dependencies between the projects. If you have ever tried to sort out the mess when someone has gone and added references that shouldn't have been there and got away with it for weeks you will understand this point



回答7:

I really think it is better to split the project as well, but it all depends on the size of the project and the number of people working on it.

For larger projects, I have a projects for

  • data access (models)
  • services
  • front end
  • tests

I got the model from Rob Connery and his storefront application... seems to work really well.

mvc-storefront