Modular Monolith: The Sweet Spot Between Monoliths and Microservices

Is it one side of microservice architecture?

DotNet Full Stack Dev
4 min readOct 11, 2024

Have you ever found yourself wrestling with the complexities of microservices architecture while still yearning for the simplicity of a monolithic application? If so, you’re not alone! Enter the Modular Monolith: a design approach that strikes a balance between these two extremes. Let’s dive into what a Modular Monolith is, how it can be seen as a partial microservice architecture, and how you can implement it in your next .NET project.

What is a Modular Monolith?

In the simplest terms, a Modular Monolith is a single application that is built in a modular way. It allows developers to organize their codebase into distinct, cohesive modules while maintaining a single deployment unit. Think of it as a well-structured monolith, where each module has clear boundaries and responsibilities, making it easier to manage and evolve over time.

🔥 Don’t Miss Out! Explore More Exciting Bytes Here! 🔍https://linktr.ee/dotnetfullstackdev and dive into the content!

Why Choose a Modular Monolith?

Before we delve into the code, let’s explore why you might want to consider a Modular Monolith:

  • Easier to Understand: A modular approach keeps related functionalities grouped together, making it easier for developers to navigate and understand the codebase.
  • Faster Development: You can still leverage the benefits of a single deployment unit, which speeds up the development and testing process.
  • Simplified Scaling: If your modules are designed with clear interfaces, you can extract them into microservices when needed, allowing for a gradual transition to microservices architecture.

The Modular Monolith vs. Microservices

While a Modular Monolith is not a full-fledged microservice architecture, it shares some characteristics:

  • Bounded Contexts: Each module in a Modular Monolith can be seen as a bounded context, similar to microservices. It encapsulates its data and business logic.
  • Independent Development: Teams can develop modules independently, allowing for parallel development efforts.

However, unlike microservices, a Modular Monolith runs as a single process, which can make it easier to manage in the early stages of development.

Let’s Get Coding: A Simple Example in C#

Now, let’s put this into practice! Here’s a simple example of how you can implement a Modular Monolith using C# in a .NET Core application.

Project Structure

Let’s create a project structure where we have multiple modules:

/MyModularMonolith
├── /Modules
│ ├── /Inventory
│ │ ├── InventoryService.cs
│ │ └── InventoryController.cs
│ ├── /Orders
│ │ ├── OrderService.cs
│ │ └── OrderController.cs
├── /Startup.cs
└── /Program.cs

Sample Code

Here’s how you can implement the InventoryService and OrderService modules.

1. Inventory Module

InventoryService.cs

using System.Collections.Generic;

namespace MyModularMonolith.Modules.Inventory
{
public class InventoryService
{
private readonly List<string> _items = new List<string>();

public void AddItem(string item)
{
_items.Add(item);
}

public IEnumerable<string> GetItems()
{
return _items;
}
}
}

InventoryController.cs

using Microsoft.AspNetCore.Mvc;

namespace MyModularMonolith.Modules.Inventory
{
[ApiController]
[Route("api/inventory")]
public class InventoryController : ControllerBase
{
private readonly InventoryService _inventoryService;

public InventoryController(InventoryService inventoryService)
{
_inventoryService = inventoryService;
}

[HttpPost]
public IActionResult AddItem(string item)
{
_inventoryService.AddItem(item);
return Ok();
}

[HttpGet]
public IActionResult GetItems()
{
return Ok(_inventoryService.GetItems());
}
}
}

2. Orders Module

OrderService.cs

using System.Collections.Generic;

namespace MyModularMonolith.Modules.Orders
{
public class OrderService
{
private readonly List<string> _orders = new List<string>();

public void CreateOrder(string order)
{
_orders.Add(order);
}

public IEnumerable<string> GetOrders()
{
return _orders;
}
}
}

OrderController.cs

using Microsoft.AspNetCore.Mvc;

namespace MyModularMonolith.Modules.Orders
{
[ApiController]
[Route("api/orders")]
public class OrderController : ControllerBase
{
private readonly OrderService _orderService;

public OrderController(OrderService orderService)
{
_orderService = orderService;
}

[HttpPost]
public IActionResult CreateOrder(string order)
{
_orderService.CreateOrder(order);
return Ok();
}

[HttpGet]
public IActionResult GetOrders()
{
return Ok(_orderService.GetOrders());
}
}
}

Configuring Services in Startup.cs

Now, we need to register our services in the Startup.cs file:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace MyModularMonolith
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<Modules.Inventory.InventoryService>();
services.AddScoped<Modules.Orders.OrderService>();
services.AddControllers();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}

Wrapping Up: Your Journey into Modular Monoliths

And there you have it! By following these steps, you’ve laid the foundation for a Modular Monolith in your .NET application. This approach allows you to build clean, maintainable code that can eventually evolve into a full microservices architecture if your needs change.

Join the Conversation!

Are you currently using a Modular Monolith in your projects? What challenges have you faced, and how have you tackled them? Share your thoughts in the comments below! Let’s learn from each other’s experiences and grow together in our software development journeys. 🌟

Remember, the path to architectural excellence is a continuous journey, and your next big idea might just be a module away!

--

--

DotNet Full Stack Dev
DotNet Full Stack Dev

Written by DotNet Full Stack Dev

Join me to master .NET Full Stack Development & boost your skills by 1% daily with insights, examples, and techniques! https://dotnet-fullstack-dev.blogspot.com

No responses yet