I systematically had a problem for mapping object resulting from a database query following underscore naming convention to objects following C# naming convention.
For instance,
private class DBUser {
public int user_id { get; set; }
public string user_name { get; set; }
}
private class User {
public int UserId { get; set; }
public string UserName { get; set; }
}
public int user_id { get; set; }
public string user_name { get; set; }
}
private class User {
public int UserId { get; set; }
public string UserName { get; set; }
}
This is obviously not a hard problem. I solved it using Automapper, its profiles and its naming convention. Here is the code:
public class FromDatabase : Profile {
public override string ProfileName { get { return "FromDatabase"; } }
protected override void Configure() {
SourceMemberNamingConvention = new LowerUnderscoreNamingConvention ();
DestinationMemberNamingConvention = new PascalCaseNamingConvention ();
CreateMap <DatabaseObject, BusinessObject> ();
}
}
public class ToDatabase : Profile {
public override string ProfileName { get { return "ToDatabase"; } }
protected override void Configure() {
SourceMemberNamingConvention = new PascalCaseNamingConvention ();
DestinationMemberNamingConvention = new LowerUnderscoreNamingConvention ();
CreateMap <BusinessObject, DatabaseObject> ();
}
}
public override string ProfileName { get { return "FromDatabase"; } }
protected override void Configure() {
SourceMemberNamingConvention = new LowerUnderscoreNamingConvention ();
DestinationMemberNamingConvention = new PascalCaseNamingConvention ();
CreateMap <DatabaseObject, BusinessObject> ();
}
}
public class ToDatabase : Profile {
public override string ProfileName { get { return "ToDatabase"; } }
protected override void Configure() {
SourceMemberNamingConvention = new PascalCaseNamingConvention ();
DestinationMemberNamingConvention = new LowerUnderscoreNamingConvention ();
CreateMap <BusinessObject, DatabaseObject> ();
}
}
and
Last, I can automagically get my objects with their right convention:
var underscore = new DatabaseObject () { user_name = "ancailliau" };
Console.WriteLine (string.Format ("Hello, I'm {0}!", underscore.user_name));
var pascal = Mapper.Map <DatabaseObject, BusinessObject> (underscore);
Console.WriteLine (string.Format ("Hello, I'm {0}!", pascal.UserName));
pascal = new BusinessObject () { UserName = "pcailliau" };
Console.WriteLine (string.Format ("Hello, I'm {0}!", pascal.UserName));
underscore = Mapper.Map <BusinessObject, DatabaseObject> (pascal);
Console.WriteLine (string.Format ("Hello, I'm {0}!", underscore.user_name));
Console.WriteLine (string.Format ("Hello, I'm {0}!", underscore.user_name));
var pascal = Mapper.Map <DatabaseObject, BusinessObject> (underscore);
Console.WriteLine (string.Format ("Hello, I'm {0}!", pascal.UserName));
pascal = new BusinessObject () { UserName = "pcailliau" };
Console.WriteLine (string.Format ("Hello, I'm {0}!", pascal.UserName));
underscore = Mapper.Map <BusinessObject, DatabaseObject> (pascal);
Console.WriteLine (string.Format ("Hello, I'm {0}!", underscore.user_name));
Nice, no?