LazyMapper 1.0.0
dotnet add package LazyMapper --version 1.0.0
NuGet\Install-Package LazyMapper -Version 1.0.0
<PackageReference Include="LazyMapper" Version="1.0.0" />
<PackageVersion Include="LazyMapper" Version="1.0.0" />
<PackageReference Include="LazyMapper" />
paket add LazyMapper --version 1.0.0
#r "nuget: LazyMapper, 1.0.0"
#:package LazyMapper@1.0.0
#addin nuget:?package=LazyMapper&version=1.0.0
#tool nuget:?package=LazyMapper&version=1.0.0
LazyMapper
Why LazyMapper?
LazyMapper is a straightforward convention based, no configuration library.
Since AutoMapper's licensing change, I noticed that I didn't have a unified way to handle mapping in my .NET projects. This library was written to satisfy the needs of my personal projects. Note that this is a hobby project it doesn't pretend to be a real substitute for any established library or a viable option for any serious project.
Creating a Map
To create and use a map, all that is needed is the Mapper class.
var mapper = new Mapper();
mapper.CreateMap<Foo, Bar>();
var foo = new Foo();
var bar = mapper.Map<Foo, Bar>(foo);
By convention, any property sharing the same name and type will be mapped automatically.
Property Binding
To bind two properties that share the same type but have different names, use the Bind() method during map configuration.
public record Foo(string FooName);
public record Bar(string BarName);
var mapper = new Mapper();
mapper.CreateMap<Foo, Bar>(profile =>
{
profile.Bind(f => f.FooName, b => b.BarName);
});
It's also possible to bind two properties with different types, as long as a map has already been registered for that source/destination pair.
public record Foo(string FooName);
public record Bar(string BarName);
public record FooHolder(Foo Foo);
public record BarHolder(Bar Bar);
var mapper = new Mapper();
mapper.CreateMap<Foo, Bar>(profile =>
{
profile.Bind(f => f.FooName, b => b.BarName);
});
mapper.CreateMap<FooHolder, BarHolder>(profile =>
{
profile.Bind(fh => fh.Foo, bh => bh.Bar);
});
Another possibility is to bind to a destination property a resolver as follows
public record Foo(string Name, string Surname);
public record Bar(string FullName);
var mapper = new Mapper();
mapper.CreateMap<Foo, Bar>(profile =>
{
profile.Bind(f => $"{f.Name} {f.Surname}", b => b.FullName)
});
Foo foo = new Foo("Jhon", "Doe");
Bar bar = mapper.Map<Foo, Bar>(foo);
// bar.FullName -> "Jhon Doe"
Nested collection and circular referencing classes are both supported and tested
Ignore Properties
To exclude a property from being mapped, use the Ignore() method during map configuration.
var mapper = new Mapper();
mapper.CreateMap<Foo, Bar>(profile =>
{
profile.Ignore(f => f.Id);
});
Ignored properties will be skipped entirely and left at their default value on the destination object.
Lifecycle Hooks
You can register callbacks to run before and after the mapping using BeforeMap() and AfterMap().
var mapper = new Mapper();
mapper.CreateMap<Foo, Bar>(profile =>
{
profile
.BeforeMap(f => f.Name = f.Name.Trim())
.AfterMap((f, b) => b.Id = f.Id + 1);
});
BeforeMapreceives the source object and runs before any property is mapped.AfterMapreceives both the source and the fully mapped destination object, making it useful for any post-processing that depends on both.
Class Profiles
For larger projects it may be useful to define Mapping Profiles in a class.
public class FooProfile : MapProfile<Foo, Bar>
{
public FooProfile()
{
Bind(f => f.FooName, b => b.BarName);
Ignore(f => f.Id);
BeforeMap(f => f.FooName = f.FooName.Trim());
AfterMap((f, b) =>
{
b.Id = f.Id + 1;
})
}
}
Register Profiles
To register a profile simply call the Register<T>() method.
var mapper = new Mapper();
mapper.Register<FooProfile>();
Alternatively it is possible to load all profiles from a given Assembly. For a class to be considered a valid Profile all it takes is to be an inheritor of MapProfile<TSource, TDestination>.
var mapper = new Mapper();
Assembly profilesAssembly = Assembly.GetAssembly(typeof(FooProfile));
mapper.RegisterProfilesFromAssembly(profilesAssembly);
This method will scan the given assembly and register all found profiles.
Limitations
- Projections are not supported. Mapping to
IQueryable<T>(e.g. for use with Entity Framework) is not yet implemented.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. |
-
net10.0
- No dependencies.
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 |
|---|