LazyMapper 1.0.0

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package LazyMapper --version 1.0.0
                    
NuGet\Install-Package LazyMapper -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="LazyMapper" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LazyMapper" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="LazyMapper" />
                    
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 LazyMapper --version 1.0.0
                    
#r "nuget: LazyMapper, 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 LazyMapper@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=LazyMapper&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=LazyMapper&version=1.0.0
                    
Install as a Cake Tool

LazyMapper

License .NET xUnit BenchmarkDotNet

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);
});
  • BeforeMap receives the source object and runs before any property is mapped.
  • AfterMap receives 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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