Coder Social home page Coder Social logo

ph.tinymapper's Introduction

PH.TinyMapper

A c# tiny object mapper

The mapper remaps properties with the same name between source and destination (if these have the same type or if they are complex properties and a mapping between left and right is possible)

Code Sample

Copy Property from source to target

 //first define a class for Source:
public class Somesource
{
    public Guid Id { get; set; }

    public DateTime? DateTimeNullable { get; set; }
    public string    Name             { get; set; }

    public DateTime UtcNow { get; }
}

// then a class for Target:
public class SomeDestination
{
    public Guid Id { get; set; }

    public DateTime? DateTimeNullable { get; set; }
    public string    Name             { get; set; }
    public string    LastName         { get; set; }

    public DateTime? DateTimeNullable2 { get; set; }
}


//Init a Mapper            
var mapper      = Mapper.Instance;
var now         = DateTime.UtcNow;
            
 var source      = new Somesource { Id = Guid.NewGuid(), Name = "John Doe", DateTimeNullable = null };
 var destination = new SomeDestination(){ DateTimeNullable = now, DateTimeNullable2 = now };
 
 
 mapper.Map(source, destination);
 
 //now destination.Name are 'John Doe' and destination.Id is the same Guid of source.Id
 // and desination.DateTimeNullable is null.
            
            

Create a new instance of target class within mapper

var src = new Somesource { Id = Guid.NewGuid(), Name = "John Doe" };
//create a new instance of target class within mapper:
var dst = PH.TinyMapper.Mapper.Instance.Map<Somesource, SomeDestination>(src);

Skipping properties

If you need to exclude a source or destination property you need to decorate it with the SkipMappingAttribute attribute

public class DestinationOrSourceWithSkip 
{
    /// <summary>
    /// Gets or sets the name of the destination.
    /// </summary>
    /// <remarks>
    /// This property is marked with the SkipMapping attribute, 
    /// which means it will not be included in the mapping process by the TinyMapper.
    /// </remarks>
    [SkipMapping]
    public string Name     { get; set; }
    
}

ph.tinymapper's People

Contributors

paonath avatar

Watchers

 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.