Skip to content

Specs v1 #1

@aoancea

Description

@aoancea
  • Custom type mapping
  • Array to Array of custom types mapping
  • List to List of custom types mapping
  • Array to List of custom types mapping
  • List to Array of custom types mapping
  • Primitive types mapping
  • Array to Array of primitive types mapping
  • List to List of primitive types mapping
  • Array to List of primitive types mapping
  • List to Array of primitive types mapping
  • Enum mapping
  • Array to Array of Enum types mapping
  • List to List of Enum types mapping
  • Array to List of Enum types mapping
  • List to Array of Enum types mapping
  • Dictionary mapping
  • Array of objects mapping
  • List of objects mapping
  • Base and derived types mapping
  • To base class type mapping
  • Interface types mapping
  • IEnumerable collection mapping
  • Generic types mapping
  • Aggregated types mapping
  • Generic types mapping
  • Same names but different underlying types mapping(int -> class; class -> int?)
  • Fields mapping
  • Non-editable property/fields mapping
namespace Runtime.Mapper
{
    public class Cow
    {
        public Guid Id { get; set; }

        public string Name { get; set; }

        public Mule Mule { get; set; }

        public int[] IntArray { get; set; }

        public List<Mule> MuleList { get; set; }
    }

    public class Mule
    {
        public Guid Id { get; set; }

        public DateTime BirthDate { get; set; }
    }

    public class Program
    {
        static ConcurrentDictionary<Tuple<Type, Type>, Func<object, object, object>> mappingFunctions = new ConcurrentDictionary<Tuple<Type, Type>, Func<object, object, object>>();

        static int Main(string[] args)
        {
            mappingFunctions.GetOrAdd(new Tuple<Type, Type>(typeof(Cow), typeof(Cow)), (s, d) => { return Map_Cow(s, d); });
            mappingFunctions.GetOrAdd(new Tuple<Type, Type>(typeof(Mule), typeof(Mule)), (s, d) => { return Map_Mule(s, d); });


            Cow cow = new Cow();
            cow.Id = Guid.NewGuid();
            cow.Name = "Cow 1";
            cow.Mule = new Mule() { Id = Guid.NewGuid(), BirthDate = new DateTime(2017, 01, 01) };

            Cow destination = cow.DeepCopyTo<Cow>();  // deep copy 'cow'


            Cow source = new Cow();
            source.Id = Guid.NewGuid();
            source.Name = "Cow 2";
            source.Mule = new Mule() { Id = Guid.NewGuid(), BirthDate = new DateTime(2017, 02, 01) };

            destination = new Cow();

            Map<Cow, Cow>(cow, destination);  // map 'source' into 'destination'
        }

        public static void Map<TSource, TDestination>(TSource source, TDestination destination)
        {
            if (source == null)
                return;

            Tuple<Type, Type> key = new Tuple<Type, Type>(typeof(TSource), typeof(TDestination));

            Func<object, object, object> mappingFunction = mappingFunctions[key];

            mappingFunction(source, destination); // Map_Cow(source, destination);
        }

        public static TDestination DeepCopyTo<TDestination>(this object source)
        {
            if (source == null)
                return default(TDestination);

            Tuple<Type, Type> key = new Tuple<Type, Type>(source.GetType(), typeof(TDestination));

            Func<object, object, object> mappingFunction = mappingFunctions[key]; // Map_Cow(source, null);

            return (TDestination)mappingFunction(source, null);
        }

        public static Cow Map_Cow(object sourceObj, object destinationObj)
        {
            Cow source = (Cow)sourceObj;
            Cow destination = null;

            if (destinationObj == null)
                destination = new Cow();
            else
                destination = (Cow)destinationObj;

            destination.Id = source.Id;
            destination.Name = source.Name;
            destination.Mule = source.Mule.DeepCopyTo<Mule>();

            if (source.IntArray != null)
            {
                destination.IntArray = new int[source.IntArray.Length];

                for (int i = 0; i < source.IntArray.Length; i++)
                {
                    destination.IntArray[i] = source.IntArray[i];
                }
            }

            if (source.MuleList != null)
            {
                destination.MuleList = new List<Mule>();

                for (int i = 0; i < source.IntArray.Length; i++)
                {
                    destination.MuleList.Add(source.MuleList.DeepCopyTo<Mule>());
                }
            }

            return destination;
        }

        public static Mule Map_Mule(object sourceObj, object destinationObj)
        {
            Mule source = (Mule)sourceObj;
            Mule destination = null;

            if (destinationObj == null)
                destination = new Mule();
            else
                destination = (Mule)destinationObj;

            destination.Id = source.Id;
            destination.BirthDate = source.BirthDate;

            return destination;
        }
    }
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions