MonadicSharp.AutoMapper 0.1.0

dotnet add package MonadicSharp.AutoMapper --version 0.1.0
                    
NuGet\Install-Package MonadicSharp.AutoMapper -Version 0.1.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="MonadicSharp.AutoMapper" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MonadicSharp.AutoMapper" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="MonadicSharp.AutoMapper" />
                    
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 MonadicSharp.AutoMapper --version 0.1.0
                    
#r "nuget: MonadicSharp.AutoMapper, 0.1.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 MonadicSharp.AutoMapper@0.1.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=MonadicSharp.AutoMapper&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=MonadicSharp.AutoMapper&version=0.1.0
                    
Install as a Cake Tool

MonadicSharp.AutoMapper

Mapper funzionale minimale per C# pensato per estendere MonadicSharp.

  • Target: .NET 6 (consumabile da .NET 6, 7, 8+)
  • Zero riflessione per la risoluzione a runtime: espressioni compilate
  • Convenzioni automatiche by-name, conversioni di base, nested mapping
  • Integrazione con Option, Result, Either
  • Profili riutilizzabili e inizializzazione globale opzionale

Installazione

  • NuGet (consigliato): MonadicSharp.AutoMapper
  • Dipendenze: MonadicSharp (>= 1.3.5)

Perch� .NET 6

  • Massima accessibilit� (LTS) e compatibilit� in forward con runtime pi� recenti (7/8+).

Concetti

  • MapperConfig: builder immutabile per registrare le mappe
  • CreateMap<TSrc,TDest>: crea una mappa tra tipi
    • ForMember(d => d.Prop, s => s.From(src => ...)): override esplicito
    • Ignore(d => d.Prop): esclusione esplicita
    • ForCtor(bag => new TDest(...)): costruzione via costruttore personalizzato con ParameterBag
  • Convenzioni automatiche:
    • Match case-insensitive per nome tra membri src/dest
    • Tipo assegnabile o convertibile (stringhe, numerici, enum, Guid, DateTime)
    • Gli override (ForMember, Ignore) prevalgono sulle convenzioni
  • Costruttori e init-only:
    • Se possibile, seleziona automaticamente il costruttore �migliore� per record/immutabili
    • Dopo il ctor, assegna i rimanenti membri scrivibili, evitando init-only
  • Nested mapping:
    • Se il tipo del membro sorgente non � assegnabile a quello di destinazione ma esiste una mappa registrata tra i due, il mapper esegue automaticamente la mappatura annidata
  • Estensioni monadiche:
    • Option<T>.MapWith(mapper).To<TDest>()
    • Result<T>.MapWith(mapper).To<TDest>()
    • Either<TLeft,TRight>.MapWithRight(mapper).ToRight<TDest>()
  • Profili & globale:
    • IMappingProfile per organizzare le mappe
    • GlobalMapping.Initialize(new MyProfile(), ...) crea un singleton GlobalMapping.Mapper
    • La DI � demandata alla libreria MonadicSharp.DI

Quick start

// Modelli
public record User(int Id, string FirstName, string LastName, string? Email);
public record UserDto { public int Id { get; init; } public string FullName { get; init; } = ""; public string? Email { get; init; } } 

// Configurazione
var config = new MapperConfig()
    .CreateMap<User, UserDto>(m => m
        // Id ed Email sono mappati per convenzione (stesso nome)
        .ForMember(d => d.FullName, s => s.From(u => $"{u.FirstName} {u.LastName}"))
    );

var mapper = config.Build();

// Uso
UserDto dto = mapper.Map<User, UserDto>(new User(1, "Ada", "Lovelace", "ada@example.com"));

Esempi

  1. Record/immutabili con auto-ctor
public record SimpleUserDto(int Id, string FullName, string? Email);

var config = new MapperConfig()
    .CreateMap<User, SimpleUserDto>(m => m
        .ForMember(d => d.FullName, s => s.From(u => $"{u.FirstName} {u.LastName}"))
    );
var mapper = config.Build();
var dto = mapper.Map<User, SimpleUserDto>(user);
  1. Nested mapping (tipi annidati)
public record Address(string City, string Country);
public record AddressDto(string City, string Country);
public record Customer(string Name, Address Address);
public record CustomerDto(string Name, AddressDto Address);

var cfg = new MapperConfig()
    .CreateMap<Address, AddressDto>(m => m) // tutto per convenzione
    .CreateMap<Customer, CustomerDto>(m => m); // Name per convenzione, Address via mappa annidata

var mapper = cfg.Build();
CustomerDto dto = mapper.Map<Customer, CustomerDto>(customer);
  1. Collezioni
IEnumerable<UserDto> dtos = mapper.Project<User, UserDto>(users);
  1. Option, Result, Either
Option<UserDto> o = userOpt.MapWith(mapper).To<UserDto>();
Result<UserDto> r = userRes.MapWith(mapper).To<UserDto>();
var e2 = eitherUser.MapWithRight<int, User>(mapper).ToRight<UserDto>();
  1. ForCtor e ParameterBag
public record OrderDto(int Id, string CustomerName, decimal Total);

var cfg = new MapperConfig()
    .CreateMap<Order, OrderDto>(m => m
        .ForMember(d => d.CustomerName, s => s.From(o => o.Customer.FullName))
        .ForCtor(bag => new OrderDto(
            bag.Get<int>(nameof(OrderDto.Id)),
            bag.Get<string>(nameof(OrderDto.CustomerName)),
            bag.Get<decimal>(nameof(OrderDto.Total))
        ))
    );
  1. Profili e mapping globale
public sealed class DefaultMappingProfile : IMappingProfile
{
    public void Configure(MapperConfig config)
    {
        config
            .CreateMap<User, UserDto>(m => m
                .ForMember(d => d.FullName, s => s.From(u => $"{u.FirstName} {u.LastName}"))
            )
            .CreateMap<User, SimpleUserDto>(m => m
                .ForMember(d => d.FullName, s => s.From(u => $"{u.FirstName} {u.LastName}"))
            );
    }
}

GlobalMapping.Initialize(new DefaultMappingProfile());
IMapper mapper = GlobalMapping.Mapper;

Linee guida

  • Lascia che le convenzioni gestiscano il 90% dei casi; usa ForMember solo per membri derivati
  • Definisci mappe per i tipi annidati usati spesso (verranno richiamate automaticamente)
  • Evita side-effect nei selettori: mantieni funzioni pure
  • Gestisci la DI in MonadicSharp.DI (non inclusa qui)

Conversioni supportate

  • string ↔ numerici, enum (da string/value), Guid, DateTime
  • Tipi assegnabili direttamente (inclusi Nullable compatibili)
  • In caso di conversione impossibile: ritorno al default del tipo

Limitazioni note

  • Non esegue mapping per riflessione dinamica completa stile AutoMapper; � intenzionalmente minimale
  • Non eseguire logica business nei selettori di mapping

Licenza

  • LGPL-3.0-or-later
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 was computed.  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. 
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
0.1.0 163 10/8/2025