Created
January 24, 2014 11:00
-
-
Save martijnburgers/8595426 to your computer and use it in GitHub Desktop.
AutoMapper Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using AutoMapper; | |
using Newtonsoft.Json; | |
namespace AutoMapper.PlayGround | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
SetupAutoMapper(); | |
//----JSON <-> OBJECT MAPPER | |
const string jsonStr = "{ 'product': '1YZXCV234', 'quantity': 15, 'name': 'Flux Capacitor' }"; | |
var shoppingCartObj = Mapper.Map<string, ShoppingCart>(jsonStr); | |
var shoppingCartStr = Mapper.Map<ShoppingCart, string>(shoppingCartObj); | |
//---- OBJECT 2 OBJECT MAPPER VIA JSON INTERMEDIATE SERIALIZING - JUST TESTING THE CONCEPT | |
var customerSource = new CustomerType() | |
{ | |
PartyId = 1, | |
FirstName = "Martijn", | |
ContactAddress = new AddressType() | |
{ | |
StreetName = "ABCStreet", | |
ZipCode = "1234AB" | |
} | |
}; | |
var customerDest = Mapper.Map<CustomerType, Customer>(customerSource); | |
var customerSourceBack = Mapper.Map<Customer, CustomerType>(customerDest); | |
//---- OBJECT 2 OBJECT MAPPING WITH ALIAS RECOGNITION | |
SetupAutoMapperWithAliasRecognition(); | |
var customerDestViaAlias = Mapper.Map<CustomerType, Customer>(customerSource); | |
var customerSourceBackViaAlias = Mapper.Map<Customer, CustomerType>(customerDestViaAlias); | |
} | |
private static void SetupAutoMapper() | |
{ | |
//----JSON <-> OBJECT MAPPER | |
Mapper.CreateMap<string, ShoppingCart>().ConvertUsing<FromJsonTypeConverter<ShoppingCart>>(); | |
Mapper.CreateMap<ShoppingCart, string>().ConvertUsing<ToJsonTypeConverter>(); | |
//if you need to supply special settings for the json serializer you can use the following | |
//Mapper.CreateMap<string, ShoppingCart>().ConvertUsing(source => JsonConvert.DeserializeObject<ShoppingCart>(source, new JsonSerializerSettings())); | |
//---- OBJECT 2 OBJECT MAPPER VIA JSON INTERMEDIATE SERIALIZING - JUST TESTING THE CONCEPT | |
//Mapper.CreateMap<CustomerType, Customer>().ConvertUsing<IntermediateJsonTypeConverter<CustomerType, Customer>>(); | |
//Mapper.CreateMap<Customer, CustomerType>().ConvertUsing<IntermediateJsonTypeConverter<Customer, CustomerType>>(); | |
//or use the custom extension method which is less verbose / easier to use. | |
Mapper.CreateMap<CustomerType, Customer>().ConvertUsingJsonIntermediateTypeConverter(); | |
Mapper.CreateMap<Customer, CustomerType>().ConvertUsingJsonIntermediateTypeConverter(); | |
} | |
private static void SetupAutoMapperWithAliasRecognition() | |
{ | |
Mapper.Reset(); | |
Mapper.Initialize(cfg => | |
{ | |
// can also add alias through AM Profiles. | |
cfg.RecognizeAlias("PartyId", "CustomerId"); | |
cfg.RecognizeAlias("FirstName", "FName"); | |
cfg.RecognizeAlias("ContactAddress", "Address"); | |
cfg.RecognizeAlias("StreetName", "Street"); | |
cfg.RecognizeAlias("ZipCode", "AreaCode"); | |
}); | |
Mapper.CreateMap<AddressType, Address>() | |
.ReverseMap(); | |
Mapper.CreateMap<CustomerType, Customer>() | |
.ReverseMap(); | |
} | |
} | |
#region Type Converters | |
internal class FromJsonTypeConverter<TDestination> : ITypeConverter<string, TDestination> | |
{ | |
public TDestination Convert(ResolutionContext context) | |
{ | |
var source = (string) context.SourceValue; | |
return JsonConvert.DeserializeObject<TDestination>(source); | |
} | |
} | |
internal class ToJsonTypeConverter : ITypeConverter<object, string> | |
{ | |
public string Convert(ResolutionContext context) | |
{ | |
return JsonConvert.SerializeObject(context.SourceValue); | |
} | |
} | |
internal class IntermediateJsonTypeConverter<TSource, TDestination> : ITypeConverter<TSource, TDestination> | |
{ | |
public TDestination Convert(ResolutionContext context) | |
{ | |
var source = JsonConvert.SerializeObject(context.SourceValue); | |
return JsonConvert.DeserializeObject<TDestination>(source); | |
} | |
} | |
#endregion | |
#region AutoMapperExtensions | |
public static class AutoMapperExtension | |
{ | |
public static IMappingExpression<TSource, TDestination> ConvertUsingJsonIntermediateTypeConverter | |
<TSource, TDestination>( | |
this IMappingExpression<TSource, TDestination> expression) | |
{ | |
expression.ConvertUsing<IntermediateJsonTypeConverter<TSource, TDestination>>(); | |
return expression; | |
} | |
} | |
#endregion | |
#region DTO's | |
class ShoppingCart | |
{ | |
public string Name { get; set; } | |
[JsonProperty("product")] | |
public string ProductCode { get; set; } | |
[JsonProperty("quantity")] | |
public int Amount { get; set; } | |
} | |
public class CustomerType | |
{ | |
public long PartyId { get; set; } | |
public string FirstName { get; set; } | |
public AddressType ContactAddress { get; set; } | |
} | |
public class AddressType | |
{ | |
public string StreetName { get; set; } | |
public string ZipCode { get; set; } | |
} | |
public class Customer | |
{ | |
[JsonProperty("PartyId")] | |
public long CustomerId { get; set; } | |
[JsonProperty("FirstName")] | |
public string FName { get; set; } | |
[JsonProperty("ContactAddress")] | |
public Address Address { get; set; } | |
} | |
public class Address | |
{ | |
[JsonProperty("StreetName")] | |
public string Street { get; set; } | |
[JsonProperty("ZipCode")] | |
public string AreaCode { get; set; } | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment