Skip to content

N_CodeJam_Mapping

andrewvk edited this page Jun 24, 2016 · 7 revisions

CodeJam.Mapping Namespace

The CodeJam.Mapping namespace contains type converters and object mappers.

Classes

 

Class Description
Public class CodeJamConvertException Defines the base class for the namespace exceptions.
Public class CodeJamMappingException Defines the base class for the namespace exceptions.
Public class Convert(TFrom, TTo) Provides data convertion functionality.
Public class Converter Provides data convertion functionality.
Public class ConvertTo(TTo) A helper class to convert a value of TTo type.
Public class DefaultValue Provides default value service.
Public class DefaultValue(T) Provides default value service.
Public classCode example Map Mapper helper class.
Public classCode example Mapper(TFrom, TTo) Maps an object of TFrom type to an object of TTo type.
Public class MapperBuilder(TFrom, TTo) Builds a mapper that maps an object of TFrom type to an object of TTo type.
Public class MappingSchema Providers object / value mapping support.
Public class MapValue Mapping value.
Public class MapValueAttribute Uses to define for enumtype.
Public class ScalarTypeAttribute Defines target type as scalar type.

Interfaces

 

Interface Description
Public interface IMapperBuilder Builds a mapper that maps an object of TFrom type to an object of TTo type.

Examples

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

using System;

using CodeJam.Expressions;
using CodeJam.Strings;

using NUnit.Framework;

#pragma warning disable 649

namespace CodeJam.Mapping.Examples
{
    [TestFixture]
    public class MapTests
    {
        class Class1
        {
            public int    Prop1 { get; set; }
            public string Field1;
        }

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

        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
                        )
                    {
                        (Class2)(.Call CodeJam.Mapping.ExpressionBuilderHelper.GetValue($dic1,$from)
                        ??
                        .Block(Class2 $obj2)
                        {
                            $obj2 = .If ($to == null) { .New Class2() } .Else { $to };

                            .Call CodeJam.Mapping.ExpressionBuilderHelper.Add($dic1, $from, $obj2);

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

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

                            $obj2
                        })
                    }".Remove(" ", "\t", "\r", "\n")));
        }
    }
}
Clone this wiki locally