Lightmapper

Object to object mapper called LightMapper


Project maintained by bearpawmaxim Hosted on GitHub Pages — Theme by mattgraham

LightMapper

Build status Test status

Features

A small mapper with a base set of functionality, that:

Examples

Initialization & Mapping creation

var mapper = LightMapper.Instance; // or new LightMapper(); 

// Creates a mapping from SourceClass to TargetClass
// including properties and fields
var mappingItem = _mapper.CreateMapping<SourceClass, TargetClass>(true);
// Registers a mapping
mapper.AddMapping(mappingItem);

Setting custom target type constructor

// // Creates a mapping from SourceClass to TargetClass
var mappingItem = _mapper.CreateMapper<SourceClass, TargetClass>(true)
    // sets a custom constructor
    .SetConstructorFunc(() => new TargetClass());
// Registers a mapping
mapper.AddMapping(mappingItem);

Setting Explicit actions and fields

// Creates a mapping from SourceClass to TargetClass
var mappingItem = _mapper.CreateMapper<SourceClass, TargetClass>(true)
    // sets BoolProp to true if DateTimeProp value is older than the week
    .Explicit((src, trg) => trg.BoolProp = src.DateTimeProp < DateTime.Now.AddDays(-7))
    // sets that the DiffField1 of SourceClass will be mapped into DiffField2 of TargetClass
    .ExplicitMember(t => t.DiffField2, s => s.DiffField1);
// Registers a mapping (LightMapper will compile mapping methods)
mapper.AddMapping(mappingItem);

Updating & Deleting a mapping

Updating a mapping

// gets existing mapping fo SourceClass to TargetClass
var mappingItem = _mapper.GetMapping<SourceClass, TargetClass>();
// sets that the DiffProp1 of SourceClass will be mapped into DiffProp2 of TargetClass
mappingItem.ExplicitMember(t => t.DiffProp2, s => s.DiffProp1);
// updates mapping (LightMapper will recompile mapping methods)
_mapper.UpdateMapping(mappingItem);

Removing a mapping

// removes mapping of SourceClass to TargetClass
var mappingItem = _mapper.RemoveMapping<SourceClass, TargetClass>();

Mapping

Mapping of single instance of SourceClass to TargetClass

SourceClass src = GetFromDbOrCreateOne();
TargetClass targ = _mapper.Map<SourceClass, TargetClass>(src);

Mapping of collection of SourceClass to collection of TargetClass

IEnumerable<SourceClass> srcList = GetFromDbOrCreateCollection();
IEnumerable<TargetClass> = _mapper.Map<SourceClass, TargetClass>(srcList);