Skip to content

T_CodeJam_Mapping_Mapper_2

Andrew Koryavchenko edited this page Jun 17, 2018 · 9 revisions

Mapper(TFrom, TTo) Class

Maps an object of TFrom type to an object of TTo type.

Inheritance Hierarchy

System.Object
  CodeJam.Mapping.Mapper(TFrom, TTo)
Namespace: CodeJam.Mapping
Assembly: CodeJam.Blocks (in CodeJam.Blocks.dll) Version: 2.0.0.0

Syntax

C#

public class Mapper<TFrom, TTo>

VB

Public Class Mapper(Of TFrom, TTo)

F#

type Mapper<'TFrom, 'TTo> =  class end

Type Parameters

 

TFrom
Type to map from.
TTo
Type to map to.
  The Mapper(TFrom, TTo) type exposes the following members.

Methods

 

Name Description
Public method Equals Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as the default hash function. (Inherited from Object.)
Public method GetMapper Returns a mapper to map an object of TFrom type to an object of TTo type.
Public method GetMapperEx Returns a mapper to map an object of TFrom type to an object of TTo type.
Public method GetMapperExpression Returns a mapper expression to map an object of TFrom type to an object of TTo type.
Public method GetMapperExpressionEx Returns a mapper expression to map an object of TFrom type to an object of TTo type. Returned expression is compatible to IQueriable.
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method Map(TFrom) Returns a mapper to map an object of TFrom type to an object of TTo type.
Public method Map(TFrom, TTo) Returns a mapper to map an object of TFrom type to an object of TTo type.
Public method Map(TFrom, TTo, IDictionary(Object, Object)) Returns a mapper to map an object of TFrom type to an object of TTo type.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
  Back to Top

Examples

This example shows how to map one object to another. C#

[TestFixture]
public class MapTests
{
    private class Class1
    {
        public int Prop1 { get; set; }
        public string Field1;
    }

    private class Class2
    {
        public string Prop1 { get; set; }
        public DateTime? Field1;
    }

    private static readonly Mapper<Class1, Class2> _class1ToClass2Mapper =
        Map.GetMapper<Class1, Class2>();

    [Test]
    public void Test1()
    {
        // Create new Class2 and map Class1 to it.
        //
        var c2 = _class1ToClass2Mapper.Map(
            new Class1
            {
                Prop1 = 41,
                Field1 = "2016-01-01"
            });

        Assert.That(c2.Prop1, Is.EqualTo("41"));
        Assert.That(c2.Field1?.Year, Is.EqualTo(2016));

        var expr = _class1ToClass2Mapper.GetMapperExpressionEx();

        Assert.That(
            expr.GetDebugView().Remove(" ", "\t", "\r", "\n", "CodeJam.Mapping.Examples.MapTests+"),
            Is.EqualTo(@"
                .Lambda #Lambda1<System.Func`2[Class1,Class2]>(Class1 $from)
                {
                    .New Class2()
                    {
                        Prop1  = .Call ($from.Prop1).ToString(),
                        Field1 = .If ($from.Field1 != null)
                        {
                            (System.Nullable`1[System.DateTime]).Call System.DateTime.Parse(
                                $from.Field1,
                                null,
                                .Constant<System.Globalization.DateTimeStyles>(NoCurrentDateDefault))
                        } .Else {
                            null
                        }
                    }
                }".Remove(" ", "\t", "\r", "\n")));
    }

    [Test]
    public void Test2()
    {
        var c2 = new Class2();

        // Map Class1 to existing Class2.
        //
        _class1ToClass2Mapper.Map(
            new Class1
            {
                Prop1 = 41,
                Field1 = "2016-01-01"
            }, c2);

        Assert.That(c2.Prop1, Is.EqualTo("41"));
        Assert.That(c2.Field1?.Year, Is.EqualTo(2016));

        var expr = _class1ToClass2Mapper.GetMapperExpression();

        Assert.That(
            expr.GetDebugView().Remove(" ", "\t", "\r", "\n", "CodeJam.Mapping.Examples.MapTests+"),
            Is.EqualTo(@"
                .Lambda #Lambda1<System.Func`4[
                    Class1,
                    Class2,
                    System.Collections.Generic.IDictionary`2[System.Object,System.Object],
                    Class2]>
                    (
                        Class1 $from,
                        Class2 $to,
                        System.Collections.Generic.IDictionary`2[System.Object,System.Object] $dic1
                    )
                {
                    .Block(System.Collections.Generic.IDictionary`2[System.Object,System.Object] $ldic2)
                    {
                        $ldic2 = $dic1;
                        (Class2)(.Call CodeJam.Mapping.ExpressionBuilder.GetValue($ldic2,$from)
                        ??
                        .Block(Class2 $obj3)
                        {
                            $obj3 = .If ($to == null) { .New Class2() } .Else { $to };

                            .Call CodeJam.Mapping.ExpressionBuilder.Add($ldic2, $from, $obj3);

                            $obj3.Prop1  = .Call ($from.Prop1).ToString();

                            $obj3.Field1 = .If ($from.Field1 != null)
                            {
                                (System.Nullable`1[System.DateTime]).Call System.DateTime.Parse(
                                    $from.Field1,
                                    null,
                                    .Constant<System.Globalization.DateTimeStyles>(NoCurrentDateDefault))
                            }
                            .Else
                            {
                                null
                            };

                            $obj3
                        })
                    }
                }".Remove(" ", "\t", "\r", "\n")));
    }
}

See Also

Reference

CodeJam.Mapping Namespace

Clone this wiki locally