Reflection Ve IL Emit Kullanarak Basit Object Mapper

2023-05-08

Daha önce çalıştığımız reflection makalelerini kullanarak, .NET 6 ile AutoMapper benzeri bir yapıda mapping uygulamasını reflection ve IL emit kullanarak performanslı olacak şekilde yazalım.

Mapping API’sini oluşturmak için, MyMappingLibrary projesine IMapper adında bir arayüz ve Mapper adında bir sınıf ekleyin.

IMapper.cs

public interface IMapper
{
    TDestination Map<TSource, TDestination>(TSource source);
    void CreateMap<TSource, TDestination>();
}

Mapper.cs

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;

public class Mapper : IMapper
{
    private readonly Dictionary<(Type Source, Type Destination), Delegate> _mappingFunctions = new();

    public TDestination Map<TSource, TDestination>(TSource source)
    {
        if (source == null)
        {
            throw new ArgumentNullException(nameof(source));
        }

        var key = (typeof(TSource), typeof(TDestination));

        if (!_mappingFunctions.TryGetValue(key, out var mapFunction))
        {
            throw new InvalidOperationException($"'{typeof(TSource).Name}' ve '{typeof(TDestination).Name}' arasında bir haritalama işlemi tanımlanmadı.");
        }

        return ((Func<TSource, TDestination>)mapFunction)(source);
    }

    public void CreateMap<TSource, TDestination>()
    {
        var sourceType = typeof(TSource);
        var destinationType = typeof(TDestination);
        var dynamicMethod = CreateMapDynamicMethod(sourceType, destinationType);

        var mapFunction = (Func<TSource, TDestination>)dynamicMethod.CreateDelegate(typeof(Func<TSource, TDestination>));
        var key = (sourceType, destinationType);
        _mappingFunctions[key] = mapFunction;
    }

    private DynamicMethod CreateMapDynamicMethod(Type sourceType, Type destinationType)
    {
        var dynamicMethod = new DynamicMethod(
            $"Map_{sourceType.Name}_To_{destinationType.Name}",
            destinationType,
            new[] { sourceType },
            typeof(Mapper),
            true);

        var ilGenerator = dynamicMethod.GetILGenerator();

        // Yeni hedef nesnesi örneği oluştur
        ilGenerator.Emit(OpCodes.Newobj, destinationType.GetConstructor(Type.EmptyTypes));

        // Kaynak ve hedef nesneler arasında eşleşen özelliklerin değerlerini kopyala
        foreach (var sourceProperty in sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
        {
            var destinationProperty = destinationType.GetProperty(sourceProperty.Name, BindingFlags.Public | BindingFlags.Instance);

            if (
            	destinationProperty != null && destinationProperty.CanWrite && destinationProperty.PropertyType == sourceProperty.PropertyType)
            {
                // Hedef nesneyi yığın üstünde sakla
                ilGenerator.Emit(OpCodes.Dup);

                // Kaynak nesnenin özellik değerini yığın üstünde sakla
                ilGenerator.Emit(OpCodes.Ldarg_0);
                ilGenerator.Emit(OpCodes.Call, sourceProperty.GetMethod);

                // Hedef nesnenin özellik değerini ayarla
                ilGenerator.Emit(OpCodes.Call, destinationProperty.SetMethod);
            }
        }

        // Hedef nesneyi geri döndür
        ilGenerator.Emit(OpCodes.Ret);

        return dynamicMethod;
    }
}

Örnek Kullanım

public class Source
{
    public int Id { get; set; }
    public string FullName { get; set; }
}

public class Destination
{
    public int Id { get; set; }
    public string FullName { get; set; }
}

public static void Main()
{
    IMapper mapper = new Mapper();

    // CreateMap metodu ile kaynak ve hedef nesneler arasındaki dönüşümü tanımlayın
    mapper.CreateMap<Source, Destination>();

    // Kaynak nesneyi oluşturun
    var sourceObject = new Source
    {
        Id = 1,
        FullName = "Murat Genç"
    };

    // Haritalama işlemi gerçekleştirin
    var destinationObject = mapper.Map<Source, Destination>(sourceObject);

    // Çıktıyı göster
    Console.WriteLine($"ID: {destinationObject.Id}, FullName: {destinationObject.FullName}");
}

Yukarıdaki örnek, reflection ve IL emit kullanarak performanslı bir şekilde nesneler arasında haritalama yapmaktadır. Bu yaklaşım, haritalama işleminin daha hızlı ve daha verimli olmasını sağlar. Bu sayede, özellikle büyük miktarda veri ile çalışırken önemli performans kazanımları elde edebilirsiniz.



More posts like this

The SemaphoreSlim Class: Multitask-Based Programming in C#

2023-06-11 | #net #semaphoreslim

Overview The SemaphoreSlim class is a structure used in C# to control one or more threads using a specific resource or operation concurrently. SemaphoreSlim limits the number of threads that can access the resource at the same time. The use of SemaphoreSlim is often used to prevent deadlock situations in multi-threaded applications and to ensure that a specific resource is used by only one or more threads at the same time.

Continue reading 


LINQ and Optimistic Concurrency: Methods for Ensuring Data Integrity

2023-06-11 | #linq #net #optimistic-concurrency

When working with databases, it is of great importance to properly manage concurrent operations and ensure data integrity. Concurrency control is used to manage multiple users or processes accessing the same data concurrently. In this article, we will explore how to ensure data integrity using LINQ (Language Integrated Query) with optimistic concurrency. What is Optimistic Concurrency? Optimistic concurrency is a method where resources are not locked when a transaction begins, but changes are checked before the transaction is completed.

Continue reading 


LINQ and Pessimistic Concurrency: Methods for Ensuring Data Integrity

2023-06-11 | #linq #net #pessimistic-concurrency

When working with databases, managing concurrent operations and ensuring data integrity are of great importance. Concurrency control is used to manage multiple users or processes accessing the same data concurrently. In this article, we will explore how to use LINQ (Language Integrated Query) with pessimistic concurrency to maintain data integrity. What is Pessimistic Concurrency? Pessimistic concurrency is based on the principle of locking the relevant data when a transaction begins and maintaining the lock until the transaction is completed.

Continue reading 