Monorepository with C#: Code Organization for Beginners
In modern C# (.NET) development, the question inevitably arises sooner or later: how to organize code when you have several projects — a class library, a web API, a console application, tests? Many beginner developers create separate repositories for each part, leading to confusion with versions and dependencies. An alternative is a monorepository (monorepo). In this article, we will break down what a monorepository is, how to set it up in C#, and why it simplifies life for beginners.
What is a Monorepository?
A monorepository is an approach where all the code of several related projects is stored in a single Git repository. For example, you might have a shared library MyApp.Shared, a web application MyApp.Web, and a console tool MyApp.Cli — all in one folder, with one .sln file.
In the context of C# (.NET), a monorepository usually means a single Visual Studio Solution containing multiple projects. This is especially convenient for:
- Sharing code (via Project References, rather than NuGet packages).
- A unified build and CI/CD process.
- Simplifying version management (one branch — all changes at once).
Benefits of a Monorepository for Beginners
If you are just starting to learn C#, a monorepository will help you avoid common mistakes:
- Ease of navigation — all projects in one place, no need to switch between repositories.
- Instant connectivity — a change in the library is immediately visible in dependent projects.
- Unified style — you can configure one
.editorconfigorDirectory.Build.propsfor all projects. - Easy to run tests — a single
dotnet testcommand checks the entire solution.
Creating a Monorepository Structure: Step-by-Step Guide
Step 1. Create the root folder and solution
Open a terminal and run:
mkdir MyMonorepocd MyMonorepodotnet new sln -n MyAppNow you have a MyApp.sln file — the entry point for Visual Studio or Rider.
Step 2. Add projects
Let's create three projects: a class library, a web API, and a console application. For convenience, place them in a src subfolder:
mkdir srccd srcdotnet new classlib -n MyApp.Shareddotnet new webapi -n MyApp.Webdotnet new console -n MyApp.Clicd ..Now link them to the solution:
dotnet sln add src/MyApp.Shared/MyApp.Shared.csprojdotnet sln add src/MyApp.Web/MyApp.Web.csprojdotnet sln add src/MyApp.Cli/MyApp.Cli.csprojStep 3. Configure dependencies between projects
Suppose MyApp.Web and MyApp.Cli use classes from MyApp.Shared. Instead of publishing the library to NuGet, simply add a Project Reference:
dotnet add src/MyApp.Web/MyApp.Web.csproj reference src/MyApp.Shared/MyApp.Shared.csprojdotnet add src/MyApp.Cli/MyApp.Cli.csproj reference src/MyApp.Shared/MyApp.Shared.csprojNow the MyApp.Web.csproj file will have a section:
<ItemGroup> <ProjectReference Include="..\\MyApp.Shared\\MyApp.Shared.csproj" /></ItemGroup>Step 4. Write shared code
In the MyApp.Shared project, let's create a simple class:
// src/MyApp.Shared/GreetingService.csnamespace MyApp.Shared;
public class GreetingService{ public string GetGreeting(string name) => $"Hello, {name}! Welcome to the monorepository.";}Now use it in the console application:
// src/MyApp.Cli/Program.csusing MyApp.Shared;