ghts.MoleculasDeDesarrollo 1.0.0

dotnet add package ghts.MoleculasDeDesarrollo --version 1.0.0
                    
NuGet\Install-Package ghts.MoleculasDeDesarrollo -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ghts.MoleculasDeDesarrollo" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ghts.MoleculasDeDesarrollo" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="ghts.MoleculasDeDesarrollo" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ghts.MoleculasDeDesarrollo --version 1.0.0
                    
#r "nuget: ghts.MoleculasDeDesarrollo, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ghts.MoleculasDeDesarrollo@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ghts.MoleculasDeDesarrollo&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=ghts.MoleculasDeDesarrollo&version=1.0.0
                    
Install as a Cake Tool

MoleculasDeDesarrollo (Development Molecules)


What is it?

MoleculasDeDesarrollo is a small pattern, plus a lightweight .NET library that supports it, for a situation every team runs into: the MVP is delivered and structured, and the client keeps asking for small, specific changes ("send this extra field", "apply 10% off for these customers", "use this code when the product is X").

Without a rule, each of those requests ends up as an if buried in the main flow, and after a few months nobody can tell which part of the code is the MVP and which part is a patch the client asked for.

With the pattern, every request is a molecule: its own class in its own file, behind a single facade per module. The MVP calls it in one line:

order.Discount = await _molecules.VipDiscount.ResolveAsync(order, ct);

The library registers molecules automatically, checks the rules of the pattern at startup and keeps a catalog of every request the application carries.


Key concepts

Molecule

One requirement equals one class, in one file. It implements IMolecule (or the typed IMolecule<TInput, TResult>), computes a value and returns it. The MVP decides what to do with the value.

Molecule set

The facade of a module (OrderMolecules, ContractMolecules...). It implements IMoleculeSet and exposes the module's molecules as properties. The MVP injects the set, never each molecule, so adding a requirement never changes the MVP constructor.

The MVP line

The only change to the main flow: one call, with the assignment visible in the MVP.


The rules

# Rule Checked by the library
1 One requirement = one class = one file. Name it after what it resolves; reference the ticket with [Molecule("REQ-101")]. — (convention)
2 It returns a value and the MVP assigns it. It does not persist, change state or audit. — (convention)
3 A molecule does not depend on another molecule or on a set. If two need each other, they are one. ✅ at registration
4 Each molecule decides explicitly what happens when an external dependency fails (block or continue) and writes it down. — (convention)
5 One set per module, flat. A set only depends on molecules. ✅ at registration
6 When a molecule becomes a core business rule, promote it into the MVP. Molecules are for specific requests, not a place to hide the main logic. — (convention)

If rules 3 or 5 are broken, AddMolecules throws a MoleculeRuleViolationException that lists every violation at once, at startup instead of in production.


Installation

Targets .NET Standard 2.0 and .NET 8.0, so it works on .NET Framework 4.6.1+, .NET Core 2.0+ and .NET 5 through 10.

dotnet add package ghts.MoleculasDeDesarrollo

Quick start

1. One file per request

[Molecule("REQ-117", Description = "VIP customers get 10% off.")]
public sealed class VipCustomerDiscount : IMolecule<Order, decimal>
{
    private readonly ICustomerDirectory _customers;

    public VipCustomerDiscount(ICustomerDirectory customers) => _customers = customers;

    public async Task<decimal> ResolveAsync(Order input, CancellationToken cancellationToken = default)
    {
        // Failure policy (rule 4): if the directory is down, no discount and the order goes on.
        try
        {
            return await _customers.IsVipAsync(input.CustomerId, cancellationToken) ? input.Total * 0.10m : 0m;
        }
        catch (HttpRequestException)
        {
            return 0m;
        }
    }
}

2. The module's set

public sealed class OrderMolecules : IMoleculeSet
{
    public OrderMolecules(FreeShippingOverThreshold freeShipping, VipCustomerDiscount vipDiscount)
    {
        FreeShipping = freeShipping;
        VipDiscount = vipDiscount;
    }

    public FreeShippingOverThreshold FreeShipping { get; }
    public VipCustomerDiscount VipDiscount { get; }
}

3. Register once

builder.Services.AddMolecules(typeof(Program).Assembly);
// or
builder.Services.AddMoleculesFromAssemblyContaining<OrderMolecules>();

4. One line in the MVP

public sealed class OrderService
{
    private readonly OrderMolecules _molecules;

    public OrderService(OrderMolecules molecules) => _molecules = molecules;

    public async Task<Order> CreateAsync(Order order, CancellationToken ct)
    {
        // ... MVP logic

        order.FreeShipping = await _molecules.FreeShipping.ResolveAsync(order, ct);
        order.Discount = await _molecules.VipDiscount.ResolveAsync(order, ct);

        return order;
    }
}

A new request from then on takes:

  1. A new molecule file.
  2. A parameter and a property in the set.
  3. A line in the MVP.

The DI setup and the MVP constructor stay as they are.

A suggested folder layout:

Orders/
├── OrderService.cs                 ← MVP
└── Molecules/
    ├── OrderMolecules.cs           ← set (facade)
    ├── FreeShippingOverThreshold.cs
    └── VipCustomerDiscount.cs

Catalog

AddMolecules also registers IMoleculeCatalog, which lists every molecule and set with its requirement, description and lifetime. You can log it at startup or expose it in a diagnostics endpoint to see which client requests the application carries:

var catalog = app.Services.GetRequiredService<IMoleculeCatalog>();
foreach (var m in catalog.Molecules)
    logger.LogInformation("Molecule {Requirement} {Type}: {Description}", m.Requirement, m.Type.Name, m.Description);

Options

services.AddMolecules(options =>
{
    options.DefaultLifetime = ServiceLifetime.Scoped; // default
    options.EnforceRules = true;                      // default
}, typeof(Program).Assembly);

Per-class lifetime override:

[Molecule("REQ-130", Lifetime = MoleculeLifetime.Singleton)]
public sealed class CountryCodeMap : IMolecule { /* ... */ }

Notes:

  • Molecules and sets are registered by their concrete type. Inject VipCustomerDiscount, not IMolecule<Order, decimal>.
  • AddMolecules is safe to call more than once; a type is only registered once.
  • Molecule dependencies (repositories, HTTP clients, etc.) are registered as usual; the library does not touch them.

Resumen en español

Una librería para el momento en que el MVP ya está entregado y el cliente sigue pidiendo cambios puntuales. Cada pedido se convierte en una molécula: una clase en su propio archivo, agrupada en una fachada por módulo (IMoleculeSet). El MVP la llama en una sola línea y el constructor del MVP no cambia nunca más.

AddMolecules registra todo automáticamente y valida al arrancar que una molécula no dependa de otra y que una fachada solo exponga moléculas. Además expone un catálogo (IMoleculeCatalog) con cada pedido y su ticket. Cuando una molécula se vuelve regla central del negocio, se promueve al MVP.


License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 109 10/20/2026