Separate projects in visual studio vs folders in a single project for microservices

domain-driven-designmicroservicesvisual studio

Unsure if the question is too broad, or opinion based. Though will ask anyway.

I'm currency reading through Microsofts "Architecture eBook", found https://www.microsoft.com/net/learn/architecture. It has a supporting solution that is found on Github, https://github.com/dotnet-architecture/eShopOnContainers

The sample application provides different patterns to implement within your microservices, along with a collection of different technology and infrastructure.

This leads me to my question.

When looking at the Ordering.API project (https://github.com/dotnet-architecture/eShopOnContainers/tree/dev/src/Services/Ordering) the author has chose to put their Domain and Infrastructure code within separate projects to the microservice API that is using them.

This image shows 3 of the services, with the ordering one implemented with multiple projects

enter image description here

Oddly, the project used to be split up further; having an Ordering.Application project too, which is now part of the Ordering.API project. This out of date image in the docs demonstrates this.

enter image description here

Given that Microservices should be self contained in that they have a defined bounded context, and these domain models should not be used outside of this microservice what would be the benefit of structuring the project like this, as opposed to a folder structure within the Ordering.API project?

Best Answer

  1. Quicker builds. If you have a single project for your microservice, it has to recompile everything (compiler optimizations notwithstanding). If you split your code into multiple projects, only the project you're changing and the projects that depend on it are recompiled

  2. More provably decoupled code. Even in microservices with narrow, isolated functionality, the fundamentals of decoupled code still apply. When you have your different concerns in different, independent projects, it is much easier to verify that you've separated your tiers in an organized way. For example, say you're using an ORM for data access. Without using projects, how easy is it for you to prove that you're not using the ORM right in your controller? With projects, your controller is in a different binary than your data access layer, so it's pretty trivial to prove: your controller project doesn't even have a reference to it!

  3. Easier unit tests. With your stuff separated out into projects, you don't have to include references to all of the crazy third party frameworks you're using for data access or web hosting when you just want to test your core business logic. Granted, in a perfect world you should have 100% code coverage, but we're not in a perfect world. The harder it is to test the simple things, the less likely it is they'll be tested.

  4. Smaller deployments. .NET projects, more or less, translate to DLLs on the output side (this is a simplification, but in general, this is the end result unless you specifically avoid it). When you only change one project, you only have to redeploy one DLL. (Note that this is a terrible reason to choose projects vs. folders since you should be using automated builds/deployments anyways, but it is a reason nonetheless!)