Adr.SimpleMapper 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Adr.SimpleMapper --version 1.0.0
NuGet\Install-Package Adr.SimpleMapper -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="Adr.SimpleMapper" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Adr.SimpleMapper --version 1.0.0
#r "nuget: Adr.SimpleMapper, 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.
// Install Adr.SimpleMapper as a Cake Addin
#addin nuget:?package=Adr.SimpleMapper&version=1.0.0

// Install Adr.SimpleMapper as a Cake Tool
#tool nuget:?package=Adr.SimpleMapper&version=1.0.0

SimpleMapper

Stupid simple attribute based mapping for C#

Examples

Consider the following two classes

//Notice the inheritance here. <From, To>
public class FooValueObject : BaseMappable<FooValueObject, FooViewModel>
{
    public string FooName {get; set;}
  public int FooAge {get; set;}
  public bool FooIsCool {get; set;}
  public string FoosPassword {get; set;}
}



//We are only interested in identifiable info, not password... obviously
public class FooViewModel
{
  public string FooName {get; set;}
  public string FooAge {get; set;}
  public bool FooIsCool {get; set;}
}

Now lets see how we actually map data

    public void MapTheFoo()
    {
      var myFoo = new FooValueObject()
      {
        FooName = "Dhruv",
        FooAge = 30,
        FooIsCool = true //false?
      };

      //done
      var fooViewModel = myFoo.Map(myFoo);
    }  
      

Now I know what you are thinking.. How do I do more complex mapping? Just implement IMappable like this..

public class FooValueObject : IMappable<FooValueObject, FooViewModel>
{
    public string FooName {get; set;}
    public int FooAge {get; set;}
    public bool FooIsCool {get; set;}
    public string FoosPassword {get; set;}

    public FooViewModel Map(FooValueObject mapTarget)
    {
	    //complex stuff goes here
    }
}  

Attributes

  • [Bypass] is property level and will signal SimpleMapper to skip this property during mapping
  • [ImplicitlyConvertPrimitives] is class level and will tell SimpleMapper to convert between strings and numeric types
  • [RejectNullReferences] is class and property level. It will tell SimpleMapper to throw NullReferenceExceptions should it encounter a null reference at the class or property level.
  • [RequireAllProperties] is class level and will tell SimpleMapper to double check that both mapping classes have the same exact properties. If class A has property 1 but Class B does not have property 1, SimpleMapper will throw an exception.

Pre Runtime validation

Validator.ValidateMappings will take a list of Assembly types and will extract all classes that inherit from BaseMappable and run pre-runtime checks on them to make sure you can unit test your mappings. Make sure to create a unit test using this method.

Important Gotchas!

  • SimpleMapper is only focused on mapping DTOs. As such it only works mapping the following primitives bool, string, int, long, decimal, double. It will ignore reference types. If you need to map nested types, map them separately then aggregate them yourself.
  • SimpleMapper only tries to map what it can by default. This means it will skip over properties that are not present on the destination type. This can be overridden by applying the [RequireAllProperties] attribute at the class level.

Road Map

  • 1.0.1 - More unit test coverage
  • 1.1.0 - [MapFrom] attribute will allow mapping properties with different names
  • 1.2.0 - Use of dependency injection within the project
Product Compatible and additional computed target framework versions.
.NET Framework net is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has 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
2.2.0 1,218 6/25/2018
1.1.0 6,009 3/26/2018
1.0.0 1,162 3/25/2018

Initial Commit