Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper issue

Trying to automap some objects.
Source objects has properties with _ before name, destination objects - have not. Is it possible to implement ONE map creation, that automapper would map all _properties to properties
for all source types.

class MyMapper<TFrom, TTo>{
    TTo PerformMap(TFrom fromObject){
        Mapper.CreateMap<From, To>(); // ???
        TTo result = Mapper.Map<From, To>(fromObject);
        //result.Id.ShouldBe(value from TFrom._Id);
        return result;
    }
}

class From
{
    public int _Id { get; set; }
    public string _Name { get; set; }
}

class To
{
    public int Id { get; set; }
    public string Name { get; set; }
}
like image 574
Arnis Lapsa Avatar asked Apr 09 '09 01:04

Arnis Lapsa


People also ask

Why you should not use AutoMapper?

1. If you use the convention-based mapping and a property is later renamed that becomes a runtime error and a common source of annoying bugs. 2. If you don't use convention-based mapping (ie you explicitly map each property) then you are just using automapper to do your projection, which is unnecessary complexity.

What is a AutoMapper?

AutoMapper in C# is a library used to map data from one object to another. It acts as a mapper between two objects and transforms one object type into another. It converts the input object of one type to the output object of another type until the latter type follows or maintains the conventions of AutoMapper.

What is AutoMapper in Entity Framework?

AutoMapper is an object-object mapper that allows you to solve the problem of manually mapping each property of a class with the same properties of another class. Before AutoMapper was introduced if we wanted to assign one object property to another object property then we were following a long procedure.


1 Answers

Something I added recently to AutoMapper might help you - custom naming conventions. If you check out the trunk (R107), look around for INamingConvention. Right now, I have two naming conventions (PascalCase and lower_case_underscore), but it's really just a matter of figuring out the right RegEx to get you going:

INamingConvention.cs

Right now, naming conventions are global and profile-scoped. Since this feature is new, there isn't any documentation other than the tests.

like image 96
Jimmy Bogard Avatar answered Sep 22 '22 03:09

Jimmy Bogard