Git Product home page Git Product logo

mapr's Introduction

Mapr

Mapr Build

A simple mediator pattern based object to object mapper.

How to Install

Release Version on NuGet

dotnet add package Mapr --version xxx

Why Mapr?

  • Does not rely on a naming contract for properties
  • Mapping of immutable objects.
  • Easily test and mock maps.
  • Maps can be shared across projects.
  • Simple Configuration.

Although there are many mapping libraries out there, Mapr takes a different approach. No Expression tree configuration or magic behind the scenes. Just a simple mapper.

How does it work?

In a nutshell, all inheritors of IMap<TSource, TDestination> are registered in a DI container, see Mapr.DependencyInjection for IServiceCollection integration.

These IMap implementations are then loaded and injected using the DI container in the mapper whenever a Map<TSource, TDestination>(sourceObject) is called.

Here is an example of a mapper that converts from string to int and back.

// StringMapper.cs

// a map between int and string as well as a map between string and int.
public class StringMapper : IMap<string, int>, IMap<int, string>
{
    public int Map(string source)
    {
        return int.Parse(source);
    }

    public string Map(int source)
    {
        return source.ToString();
    }
}

The above map then is set up in the service registration below.

// Service Registration file
// This could be in Startup.cs for an ASP.NET Project.

var services = new ServiceCollection();

services.AddMapr(config =>
{
    // to add specific type maps.
    config.AddMap<string, int, TestMap>()
        .AddMap<int, string, TestMap>();

    // To scan an assembly for IMap inheritors.
    // config.Scan(typeof(TestMap).Assembly);

    // Gets the assembly that contains TestMap and scans it for all IMap inheritors
    // config.Scan<TestMap>();

});

var provider = services.BuildServiceProvider();

// Get the mapper from the service provider.
var mapper = provider.GetService<IMapper>();

var testString = "2";
var testInt = 10;

// will return the int: 2
int mappedStringToInt = mapper.Map<string, int>(testString);

// will return the string: "10"
string mappedIntToString = mapper.Map<int, string>(testInt);

More Complex Mapping

See samples for more complex examples

mapr's People

Contributors

rena0157 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.