1

Why and When you should create a new project in a visual studio solution? To be more specific, when you should separate the main project in multiple projects?

We all know that Database files should be separated by main project, that Entity Framework models should be separated, but what about "components"?

Let's assume i have a WebAPI project, with some Apis and some features, including Entity Framework and Database files. Atm these features are included in the main project and my solution structure is:

- App.Api [Main project, With Feature1, Feature2, Feature3, FeatureX, Reference projects App.Models]
|- ApiControllers
|- Helpers
|- Managers
|- Assets
|- Web.config

- App.Models [Models Project]
|- Entity Framework Auto Generated Models

- App.Database [Database Project]
|- Sql Files 

What if i split my features in multiple projects and include them in the main project, using the main project as "HEAD" of the other projects? Something like:

- App.Api [Main project, reference Projects App.Feature.Feature1, App.Feature.Feature2, App.Feature.Feature3, App.Feature.FeatureX, App.Models]
|- ApiControllers
|- Web.config

- App.Feature.Feature1 [Library Project - Feature 1, splitted from main project]
|- Managers
|- Helpers
|- Other files *.cs

- App.Feature.Feature2 [Library Project - Feature 2, splitted from main project]
|- Managers
|- Helpers
|- Other files *.cs

- App.Feature.Feature3 [Library Project - Feature 3, splitted from main project]
|- Managers
|- Helpers
|- Other files *.cs

- App.Feature.FeatureX [Library Project - Feature X, splitted from main project]
|- Managers
|- Helpers
|- Other files *.cs

- App.Models [Models Project]
|- Entity Framework Auto Generated Models

- App.Database [Database Project]
|- Sql Files 

After that i could but some unit testing attached to each project:

- App.Api [Main project]
- App.Feature.Feature1 [Feature 1, splitted from main project]
- App.Feature.Feature1.Tests
- App.Feature.Feature2 [Feature 2, splitted from main project]
- App.Feature.Feature2.Tests
- App.Feature.Feature3 [Feature 3, splitted from main project]
- App.Feature.Feature3.Tests

So, this is the question. Why, why not and when create multiple projects?

I find more clean this approach instead of first

  • 2
    This is by far too broad and probably even opinion-based. Allways have in mind the single-resposibility principle. That not only applies to methods or classes, but also to assemblies in some sense. Seperate things that do not belong together, group what belongs together. Having said this there is no "single right answer" here. – MakePeaceGreatAgain Nov 20 '19 at 10:42
  • I think you have already found the reason behind the decision to have multiple related projects in the same solution. – Steve Nov 20 '19 at 10:42
  • 1
    'Separation of Concerns` and loosely coupled approach are the two factors for it. It is better to separate the projects, however you can create a single solution and bind multiple projects to it. – Sahil Sharma Nov 20 '19 at 10:43
  • @HimBromBeere I am a junior developer that started a while ago (only 2 years) so i have some questions. It is correct or not to split? Could you be more specific why the single-responsibility principle affect this specific decision? – Matteo Tinta Nov 20 '19 at 11:04
  • Well as a class, also an assembly serves a *specific* purpose. Thus only code that is relevant for that specific purpose should go into that assembly. Other code should probably be in another assembly. Anyway as I already mentioned, it´s fairlyopinion-based, you can also bake your entire code into a single assembly if that scales well enough for your needs. – MakePeaceGreatAgain Nov 20 '19 at 11:23
  • @HimBromBeere So if i understood right, you prefer to split those features instead of keeping them inside a single WebApi project because every project will serves the specific purpose of take raw data and do something. Is it right? – Matteo Tinta Nov 20 '19 at 11:37
  • That would probably do it. As an asside this also makes dependencies to other assemblies more obvious. – MakePeaceGreatAgain Nov 20 '19 at 11:40
  • @HimBromBeere Nice. Thank you very much! (How can i upvote your comment? Noob question I know) – Matteo Tinta Nov 20 '19 at 12:32

0 Answers0