Home

Powerful convention-based, customizable and fastest tool for object-object mapping.It is useful for Entity to DTO, DTO to Entity mapping strategies.

Where can I get it?

The latest builds can be found at NuGet

PM> Install-Package FastMapper

New to FastMapper?

Check out the Getting Started page first.

Usage

using FastMapper;
Create a new object:
//get customer data
Customer customer = GetCustomer();

// map
CustomerDTO customerDto = TypeAdapter.Adapt<Customer, CustomerDTO>(customer);
Map to existing object:
// create a new CustomerDTO
CustomerDTO dto = new CustomerDTO();

// map to existing object
TypeAdapter.Adapt(customer, dto);

Customize Type Adapter

using FastMapper;

TypeAdapterConfig<Customer, CustomerDTO>()
.NewConfig()
.IgnoreMember(dest => dest.Address)
.MapFrom(dest => dest.FullName, 
         src => string.Format("{0} {1}", src.FirstName, src.LastName));

//get customer data
Customer customer = GetCustomer();

// create new dto object 
CustomerDTO customerDto = TypeAdapter.Adapt<Customer, CustomerDTO>(customer);

Queryable Extensions

using FastMapper;

using(MyDbContext context = new MyDbContext())
{
    // Build a Select Expression from DTO
    var customers = context.Customers.Project().To<CustomerDTO>().ToList();

    //Handwriting Mapping
    var customerList = context.Customers.Select(c => new CustomerDTO(){
        Id = p.Id,
        Name = p.Name,
        Surname = p.Surname,
        ....
    })
    .ToList();
}

General Features

Benchmark (Complex Model)

FastMapper vs AutoMapper Performance Comparison

Test Classes
public class Address
{
    public int Id { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
}

public class AddressDTO
{
    public int Id { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
}

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal? Credit { get; set; }
    public Address Address { get; set; }
    public Address HomeAddress { get; set; }
    public Address[] Addresses { get; set; }
    public ICollection<Address> WorkAddresses { get; set; }
}

public class CustomerDTO
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Address Address { get; set; }
    public AddressDTO HomeAddress { get; set; }
    public AddressDTO[] Addresses { get; set; }
    public List<AddressDTO> WorkAddresses { get; set; }
    public string AddressCity { get; set; }
}
Data
private Customer GetCustomer()
{
    Customer customer = new Customer()
    {
        Id = 1,
        Name = "Timucin Kivanc",
	Credit = 234.7m,
	Address = new Address() { 
	    City = "Istanbul", 
	    Country = "Turkey", 
	    Id = 1, 
	    Street = "Istiklal cad." 
	},
	HomeAddress = new Address() { 
	    City = "Istanbul", 
	    Country = "Turkey", 
	    Id = 2, 
	    Street = "Istiklal cad." 
	},
        WorkAddresses = new List<Address>() { 
            new Address() { 
	        City = "Istanbul", 
	        Country = "Turkey", 
		Id = 5, 
		Street = "Istiklal cad." 
	    },
	    new Address() { 
		City = "Izmir", 
		Country = "Turkey", 
		Id = 6, 
		Street = "Konak" 
	    }
	},
	Addresses = new List<Address>() { 
	    new Address() { 
		City = "Istanbul", 
		Country = "Turkey", 
		Id = 3, 
		Street = "Istiklal cad." 
	    },
	    new Address() { 
                City = "Izmir", 
		Country = "Turkey", 
		Id = 4, 
		Street = "Konak" 
	    }
        }.ToArray()
    };

    return customer;
}
Mapping
public void MapFromFastMapper(int iterations)
{
    var customer = GetCustomer();
    for(int i = 0; i < iterations; i++)
        TypeAdapter.Adapt<Customer, CustomerDTO>(customer);
}

public void MapFromAutoMapper(int iterations)
{
    Mapper.CreateMap<Customer, CustomerDTO>(); 

    var customer = GetCustomer();
    for(int i = 0; i < iterations; i++)
        Mapper.Map<Customer, CustomerDTO>(customer);
}

Result

Iterations : 100

Handwriting Mapper : 2 miliseconds
FastMapper : 0 miliseconds
AutoMapper : 82 miliseconds

Iterations : 1000

Handwriting Mapper : 2 miliseconds
FastMapper : 2 miliseconds
AutoMapper : 106 miliseconds

Iterations : 10000

Handwriting Mapper : 6 miliseconds
FastMapper : 31 miliseconds
AutoMapper : 1088 miliseconds

Iterations : 100000

Handwriting Mapper : 62 miliseconds
FastMapper : 289 miliseconds
AutoMapper : 10620 miliseconds

Iterations : 1000000

Handwriting Mapper : 608 miliseconds
FastMapper : 2976 miliseconds
AutoMapper : 106336 miliseconds


Last edited Apr 15, 2014 at 12:52 PM by timucinkivanc, version 22