I’m working on a rather large Asp.net MVC3 project and would like to use Automapper to solve the following problem.
In the database, phone numbers are stored as a 10 digit decimal number. On the user screen, they are displayed & edited as “(xxx) yyy-zzzz”.
What I want to do is to create a custom type converter that would look like follows –
public class phoneNumber //display data type
inherits string;
public class getdata(){
Mapper.CreateMap<decimal, phoneNumber>().ConvertUsing(decimal2Phone);
Mapper.CreateMap<phoneNumber, decimal>().ConvertUsing(phone2Decimal);
Mapper.CreateMap<dbRecordTYpe, displayRecordType>();
Mapper.CreateMap<displayRecordType, dbRecordTYpe>();
}
Where the usage is like on https://github.com/AutoMapper/AutoMapper/wiki/Custom-type-converters. We are using Automapper to move all of our data from the database entity models to display view models.
I know that I cannot inherit from string.
However, If I can do this this format conversion using Automapper, it will save me a lot work work and duplicate code.
You can use a composition instead of inheritance – just use a string property in a custom
PhoneNumberclass and use that property in the mapping: