Git Product home page Git Product logo

automapper.extendedconverters's Introduction

AutoMapper.ExtendedConverters

AutoMapper.ExtendedConverters is a small set of useful AutoMapper (and Mapster) custom Type Converters

ListConverter and more general CollectionConverter are usefull for updating data in existing collections.

For example, we have list of entites from database and it's modified version from client. Then with ListConverter we can update all database entities from client objects with same Id.

using AutoMapper;
using AutoMapper.ExtendedConverters;

class Model
{
    public int Id { get; set; }
    public string Text { get; set; }
}

class Entity
{
    public int Id { get; set; }
    public string Text { get; set; }
}

class Program
{
    static IMapper Mapper;
    
    static void Configure()
    {
        var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<Model, Entity>();
            // create custom mapper for lists
            cfg.CreateMap<List<Model>, List<Entity>>()
                // configure 'Id' property selectors for Model and Entity
                .UsingListConverter(m => m.Id, e => e.Id);
        });
        
        Mapper = config.CreateMapper();
    }
    
    static void Main()
    {
        // values from Server
        var dest = new List<Entity>
        {
            new Entity { Id = 1, Text = "a" },
            new Entity { Id = 2, Text = "b" },
        };
        // values from Client
        var src = new List<Model>
        {
            new Model { Id = 1, Text = "A" },
            new Model { Id = 3, Text = "C" },
        };
        
        List<Entity> res = Mapper.Map(src, dest);
        
        // now res is equivalent to
        new List<Entity>
        {
            // Entity with Id == 1 is updated
            new Entity { Id = 1, Text = "A" },
            // Entity with Id == 2 is removed
            // ...
            // Entity with Id == 3 is added
            new Entity { Id = 3, Text = "C" },
        }
    }
}

Mapping collections other than List<T>

var config = new MapperConfiguration(cfg =>
{
    cfg.CreateMap<IEnumerable<Model>, ICollection<Entity>>()
        .UsingCollectionConverter((Model m) => m.Id, (Entity e) => e.Id);
});   

Using with Mapster

using Mapster;
using Mapster.CollectionChangeTracking;

class Model
{
    public int Id { get; set; }
    public string Text { get; set; }
}

class Entity
{
    public int Id { get; set; }
    public string Text { get; set; }
}

class Program
{
    static void Configure()
    {
        TypeAdapterConfig<List<Model>, List<Entity>>.NewConfig()
            .TrackListChanges(m => m.Id, e => e.Id);
        
        // for mapping collections other than lists
        TypeAdapterConfig<Model[], SortedSet<Entity>>.NewConfig()
            .TrackCollectionChanges((Model m) => m.Id, (Entity e) => e.Id);
    }
    
    static void Main()
    {
        // values from Server
        var dest = new List<Entity>
        {
            new Entity { Id = 1, Text = "a" },
            new Entity { Id = 2, Text = "b" },
        };
        // values from Client
        var src = new List<Model>
        {
            new Model { Id = 1, Text = "A" },
            new Model { Id = 3, Text = "C" },
        };
        
        List<Entity> res = TypeAdapter.Adapt(src, dest);
        
        // now res is equivalent to
        new List<Entity>
        {
            // Entity with Id == 1 is updated
            new Entity { Id = 1, Text = "A" },
            // Entity with Id == 2 is removed
            // ...
            // Entity with Id == 3 is added
            new Entity { Id = 3, Text = "C" },
        }
    }
}

automapper.extendedconverters's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

automapper.extendedconverters's Issues

Roadmap

Features

  • Map fields to properties and vice versa

Common

  • Unit Tests
  • Benchmarks
  • Documentation & Examples
  • XML Documentation
  • Publishing NuGet package
  • Upgrade to AutoMapper 6
  • Work with Mapster

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.