Skip to content

AngeloBestetti/Mapfy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mapfy - Mapper .NET Classes Easy

Solution generated by ChatGpt 5 with Code Copilot

This repo is provided as it is, I´m not planning to make any improvments. Feel free to fork and improve! This it´s just a example with the good prompt AI can build some daily solutions.

Benchmark 1

Payload: 100.000 rows from EF Core

Load time from EF: 00:00:09.0380295 (Our SQL Server is in USA and I ran the Solution in Brazil)

Processing Time: 00:00:00.0168305 (I´m surprised)

Benchmark 2

Payload: 1.000.000 rows from EF Core

Load time from EF: 00:01:15.7912795

Processing Time: 00:00:00.3713606 (I´m scared)

Example of use

Program.cs

namespace MapfyDemo;
using Mapfy;

public class PostalCode
{
    public string OldCode { get; set; }
}

public class PostalCodeDto
{
    public string Code { get; set; }
}

public class Address
{
    public string City { get; set; }
    public string State { get; set; }
    public PostalCode PostalCode { get; set; }
}

public class AddressDto
{
    public string City { get; set; }
    public string Province { get; set; }
    public PostalCodeDto PostalCode { get; set; }
}

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public string Email { get; set; }

    public string Code; // field
    public Address? Address { get; set; }
    public List<string> Tags { get; set; }
    public string[] Aliases { get; set; }
}

public class PersonDto
{
    public string FullName { get; set; }
    public int Age { get; set; }
    public string Email { get; set; }

    public string Code; // field
    public AddressDto? Address { get; set; }
    public List<string> Tags { get; set; }
    public string[] Aliases { get; set; }
}

internal static class Demo
{
    public static void Run()
    {
        var cfg = new MapperConfiguration(c =>
        {

            c.For<PostalCode, PostalCodeDto>()
                .Strategy(MappingStrategy.CompiledExpressions)
                .CaseInsensitive()
                .ForMember(d => d.Code, s => s.MapFrom(i => i.OldCode))
            ;

            c.For<Address, AddressDto>()
                .Strategy(MappingStrategy.CompiledExpressions)
                .CaseInsensitive()
                .ForMember(d => d.Province, s => s.MapFrom(i => i.State))
            ;


            c.For<Person, PersonDto>()
                .Strategy(MappingStrategy.CompiledExpressions)
                .CaseInsensitive()
                .ForMember(d => d.FullName, s => s.MapFrom(i => i.FirstName + " " + i.LastName))
                .ForMember(d => d.Code, s => s.MapFrom(i => i.Code));

        });

        var mapper = cfg.CreateMapper();
        MapfyContext.SetDefault(mapper); // Enable p.Map<TDest>()

        var p = new Person()
        {
            Code = "X-42",
            FirstName = "John",
            LastName = "Doe",
            Age = 36,
            Email = "john@example.com",
            Address = new Address { City = "Miami", State = "FL", PostalCode = new PostalCode() { OldCode = "031320" } },
            Tags = new List<string> { "math", "programming" },
            Aliases = new[] { "John", "Doe" }
        };

        // 1) Original style
        var dto = mapper.Map<Person, PersonDto>(p);

        // 2) Short style using runtime source type
        var dto2 = mapper.Map<PersonDto>(p);

        // 3) Object extension via global context
        var dto3 = p.Map<PersonDto>();

        Console.WriteLine("FullName: " + dto3.FullName);
        Console.WriteLine("Age: " + dto3.Age);
        Console.WriteLine("Email: " + dto3.Email);
        Console.WriteLine("Code: " + dto3.Code);
        Console.WriteLine("Address.City: " + (dto3.Address != null ? dto3.Address.City : null) + ", Province: " + (dto3.Address != null ? dto3.Address.Province : null));
        Console.WriteLine("Tags: " + string.Join(",", dto3.Tags));
        Console.WriteLine("Aliases: " + string.Join(",", dto3.Aliases));

        var many = new[]
        {
            new Person()   {
            Code = "A-1",
            FirstName = "Ada",
            LastName = "Lovelace",
            Age = 36,
            Email = "ada@example.com",
            Address = new Address { City = "Miami", State = "FL", PostalCode = new PostalCode() { OldCode = "031320" } },

        },
            new Person()   {
            Code ="B-2",
            FirstName = "Ada",
            LastName = "Lovelace",
            Age = 36,
            Email = "ada@example.com",
            Address = new Address { City = "Miami", State = "FL", PostalCode = new PostalCode() { OldCode = "031320" } },
        }
        };

        var dtos = mapper.MapList<Person, PersonDto>(many);
        Console.WriteLine("Mapped list count: " + dtos.Count);
    }
}

public static class Program
{
    public static void Main()
    {
        Demo.Run();
    }
}

About

Mapfy - Mapper .NET Classes Easy

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages