Version: 1.0.2
Target Framework: net10.0+
NuGet Package ID: Marai.LightMapper
Introduction
Marai.LightMapper is a lightweight object mapper for .NET that maps between source and destination types using a marker-interface convention. It supports both automatic property mapping (by name and type) and custom mapping logic defined directly on your models.
Major capabilities:
- Convention-based auto-mapping by matching property names (case-insensitive)
- Custom mapping logic via
IMapFrom<TSource>andIMapTo<TDestination>interfaces - Automatic type conversions: numeric types,
Guid,DateTime,bool, enums, andstringvariants - Assembly scanning to discover and register all mappings at startup
- Microsoft Dependency Injection integration
- Safe for concurrent use — mappings are stored in a
ConcurrentDictionary
Table of Contents
- Installation
- Getting Started
- Mapping Conventions
- Using the Mapper
- Supported Type Conversions
- Exceptions Reference
- Full API Reference
Installation
Getting Started
Register with Dependency Injection
Call AddMaraiLightMapper in your Program.cs, passing the assemblies that contain your mapping types.
You can call AddMaraiLightMapper multiple times with different assemblies. Subsequent calls reuse the same MappingRegistry and accumulate mappings.
AddMaraiLightMapper registers:
MappingRegistry(singleton) — stores all registered mapping functionsIMapper(singleton) — the mapping entry point
Use without DI
Mapping Conventions
Auto-Mapping
When a type implements IMapFrom<TSource> or IMapTo<TDestination> without overriding the default interface method, LightMapper automatically maps properties by matching names (case-insensitive). Source properties with no matching destination property are ignored. Destination properties with no matching source property are left at their default value.
Custom Mapping with IMapFrom
Override MapFrom on the destination type to control exactly how the source is mapped.
TheMapFromoverride must be declared directly on the class (DeclaredOnly). Interface default implementations are not treated as custom logic.
Custom Mapping with IMapTo
Implement IMapTo<TDestination> on the source type to push mapping logic onto the source.
Using the Mapper
Inject IMapper wherever you need to convert objects.
To map to a type known only at runtime:
Mappingnullreturnsnullwithout throwing.
Supported Type Conversions
When auto-mapping, LightMapper converts mismatched property types using the following rules:
| Source Type | Destination Type | Behavior |
|---|---|---|
string |
Guid |
Guid.TryParse — skips property if invalid |
Guid |
string |
ToString() |
string |
Numeric types | Convert.ChangeType — skips if blank |
| Numeric | Numeric | Convert.ChangeType |
string |
Enum | Enum.Parse (case-insensitive) — skips if blank |
| Numeric | Enum | Enum.ToObject |
| Enum | string |
ToString() |
string |
DateTime |
DateTime.TryParse — skips if blank |
DateTime |
string |
ISO 8601 round-trip format ("O") |
string |
bool |
bool.TryParse — skips if blank |
| Any | Nullable variants | Unwraps nullable and applies matching rule |
Properties that cannot be converted are silently skipped.
Exceptions Reference
| Exception | Thrown When | Properties |
|---|---|---|
MappingNotFoundException |
IMapper.Map is called for a source/destination pair with no registered mapping |
None beyond the message |
A mapping is only registered when the source or destination type implementsIMapFrom<>orIMapTo<>and the assembly containing that type was passed toAddMaraiLightMapper.
Full API Reference
License
This project is licensed under the Marai Proprietary Software License Agreement.
Free for personal and commercial use.
For the complete license agreement and terms, please visit: https://marai.dev/proprietary-software-license-agreement



