Piccolo.Net is a lightweight framework designed to simplify the development of modular monolith applications in .NET. It provides a robust foundation for building loosely coupled modules with built-in support for event-driven communication and dependency injection.
$ dotnet add package Piccolo.CorePiccolo is a .NET library that simplifies and empowers the creation of modular monoliths. Inspired by the Namekians from Dragon Ball and their ability to fuse and regenerate, Piccolo enables a host application to integrate and manage modules dynamically and resiliently.
This repository contains:
A modular monolith deploys as a single application while being organized as cohesive modules (e.g., Users, Orders, Inventory). Piccolo acts as the fusion manager for these modules, letting teams develop modules independently and plug them into the application without tight coupling.
Piccolo can “fuse” external modules with the host application. Each module represents a feature area. The host discovers, loads, and initializes these modules at startup.
How fusion works in Piccolo:
IModule and, optionally, application-specific interfaces or the IEventBus for decoupled communication.Inspired by Piccolo’s regeneration, the library aims to support isolating and restarting faulty modules without affecting the rest of the app.
Today, Piccolo.Core already supports isolation at the assembly loading level using collectible AssemblyLoadContext, enabling controlled unloading of modules. Health checks and automatic restarts can be layered on top in your host.
IModule — Basic contract that modules implement:
Name: stringConfigureServices(IServiceCollection services, IConfiguration configuration): Register services in the module's isolated container.Initialize(IServiceProvider serviceProvider): Async initialization logic (e.g., subscribing to events).Shutdown(): Cleanup logic.ModuleLoader — Discovers, loads, initializes, and shuts down modules. It supports two modes:
AssemblyLoadContext)AssemblyLoadContextIEventBus — Abstraction for event-driven communication. Piccolo provides:
InMemoryEventBus: Simple in-process event bus.EventRouter: Routes events to the appropriate bus based on configuration.MqttEventBus implementation demonstrating how to create distributed buses.ModuleLoader reads configuration from the Piccolo section in appsettings.json:
Modules: Optional list of module type names to enable (filter). If empty or missing, all discovered IModule types are loaded.ModuleAssemblies: Optional list of module assembly file paths. If provided, each is loaded into its own AssemblyLoadContext and scanned for IModule implementations.SharedAssemblies: List of assembly names that should be shared across all modules (loaded in the default context). This is critical for sharing types like Events or Interfaces between the host and modules.Routing: Configuration for the Event Bus routing.Piccolo supports a flexible event routing system. You can define:
DefaultBus: The bus to use if no specific rule matches.Publishers: Which buses an event should be published to (can be multiple).Subscribers: Which bus a handler should subscribe to for a given event.{
"Piccolo": {
"Modules": [
"HelloModule"
],
"ModuleAssemblies": [
"Example/Modules/HelloModule/bin/Debug/net9.0/HelloModule.dll"
],
"SharedAssemblies": [
"Piccolo.Example.Core"
],
"Routing": {
"DefaultBus": "in-memory",
"Publishers": {
"OrderCreatedEvent": ["in-memory", "queue"],
"OrderProcessedEvent": ["queue"]
},
"Subscribers": {
"OrderCreatedEvent": "in-memory",
"OrderProcessedEvent": "queue"
}
}
}
}
Notes:
ModuleAssemblies can be absolute or relative to the host process working directory.SharedAssemblies ensures that types defined in those assemblies are treated as the same type across different load contexts.Open the solution:
Build the solution:
Run the Example host (Example/Piccolo.Example):
Exit cleanly:
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Piccolo.Core;
public class MyModule : IModule
{
public string Name => "MyModule";
public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
// Register services here
// services.AddTransient<IMyService, MyService>();
}
public Task Initialize(IServiceProvider serviceProvider)
{
// Subscribe to events, start background work, etc.
return Task.CompletedTask;
}
public void Shutdown()
{
// Clean up resources
}
}
This project is licensed under the MIT License. See the LICENSE file for details.