Skip to content

Instantly share code, notes, and snippets.

@omerfarukz
Last active December 1, 2016 18:22
Show Gist options
  • Save omerfarukz/5683f25f99e8831c4fb4e4f6a61afbbd to your computer and use it in GitHub Desktop.
Save omerfarukz/5683f25f99e8831c4fb4e4f6a61afbbd to your computer and use it in GitHub Desktop.
using System;
using AutoMapper;
namespace pp
{
class MainClass
{
public static void Main (string [] args)
{
Mapper.Initialize ((obj) => obj.AddProfile<MyProfile> ());
Mapper.AssertConfigurationIsValid ();
int typeCode = 0;
while (true) {
if (int.TryParse (Console.ReadLine (), out typeCode)) {
var newResponse = new NewResponse () {
Results = new NewResult [] {
new NewResult () {
Parent = new NewParentType () {
TypeA = new TypeA () { Number = 2, Text="TWO" },
TypeB = new TypeB () { Number = 4, Text="FOUR" },
TypeC = new TypeC () { Number = 6, Text="SIX" },
TypeCode = typeCode
}
}
}
};
var oldResponse = Mapper.Map<OldResponse> (newResponse);
Console.WriteLine (oldResponse.Results [0].Text);
}
}
}
}
public class MyProfile : Profile
{
protected override void Configure ()
{
CreateMap<TypeA, OldResult> ().ForMember (f => f.Number, c => c.MapFrom (k => k.Number));
CreateMap<TypeB, OldResult> ().ForMember (f => f.Number, c => c.MapFrom (k => k.Number));
CreateMap<TypeC, OldResult> ().ForMember (f => f.Number, c => c.MapFrom (k => k.Number));
CreateMap<NewParentType, TypeA> ().ProjectUsing ((f) => f.TypeA);
CreateMap<NewParentType, TypeB> ().ProjectUsing ((f) => f.TypeB);
CreateMap<NewParentType, TypeC> ().ProjectUsing ((f) => f.TypeC);
CreateMap<NewResult, NewParentType> ().ForMember (f => f.TypeA, c => c.MapFrom (k => k.Parent.TypeA)).ForAllOtherMembers ((obj) => obj.Ignore ());
CreateMap<NewResult, NewParentType> ().ForMember (f => f.TypeB, c => c.MapFrom (k => k.Parent.TypeB)).ForAllOtherMembers ((obj) => obj.Ignore ());
CreateMap<NewResult, NewParentType> ().ForMember (f => f.TypeC, c => c.MapFrom (k => k.Parent.TypeC)).ForAllOtherMembers ((obj) => obj.Ignore ());
CreateMap<NewResponse, OldResponse> ().ForMember (f => f.Results, c => c.MapFrom (k => k.Results));
// PROBLEM KAOS:
// CreateMap<YeniSonuc, EskiSonuc> ().ForMember (f => f.Sayi, c => c.MapFrom (k => k.UstTip.ATipi.Sayi));
// PROBLEM N FIELD
// CreateMap<YeniSonuc, EskiSonuc> ()
// .ForMember (f => f.Sayi, c => c.ResolveUsing<SayiResolver> ())
// .ForMember (f => f.Metin, c => c.ResolveUsing<MetinResolver> ());
// PROBLEM Projection
CreateMap<NewResult, OldResult> ().ProjectUsing ((f) => CreateOldFrom (f));
}
OldResult CreateOldFrom (NewResult source)
{
OldResult result = null;
if (source.Parent.TypeCode == 0) {
result = Mapper.Map<OldResult> (source.Parent.TypeA);
} else if (source.Parent.TypeCode == 1) {
result = Mapper.Map<OldResult> (source.Parent.TypeB);
} else if (source.Parent.TypeCode == 2) {
result = Mapper.Map<OldResult> (source.Parent.TypeC);
}
return result;
}
}
public class NumberResolver : IValueResolver<NewResult, OldResult, int>
{
public int Resolve (NewResult source, OldResult destination, int destMember, ResolutionContext context)
{
OldResult result = new OldResult () { Text = "EKSI BIR", Number = -1 };
if (source.Parent.TypeCode == 0) {
result = Mapper.Map<OldResult> (source.Parent.TypeA);
} else if (source.Parent.TypeCode == 1) {
result = Mapper.Map<OldResult> (source.Parent.TypeB);
} else if (source.Parent.TypeCode == 2) {
result = Mapper.Map<OldResult> (source.Parent.TypeC);
}
return result.Number;
}
}
public class MetinResolver : IValueResolver<NewResult, OldResult, string>
{
public string Resolve (NewResult source, OldResult destination, string destMember, ResolutionContext context)
{
OldResult result = new OldResult () { Text = "EKSI BIR", Number = -1 };
if (source.Parent.TypeCode == 0) {
result = Mapper.Map<OldResult> (source.Parent.TypeA);
} else if (source.Parent.TypeCode == 1) {
result = Mapper.Map<OldResult> (source.Parent.TypeB);
} else if (source.Parent.TypeCode == 2) {
result = Mapper.Map<OldResult> (source.Parent.TypeC);
}
return result.Text;
}
}
public class OldResponse
{
public string Message { get; set; }
public OldResult [] Results { get; set; }
}
public class OldResult
{
public int Number { get; set; }
public string Text { get; set; }
}
public class NewResponse
{
public string Message { get; set; }
public NewResult [] Results { get; set; }
}
public class NewResult
{
public string Message { get; set; }
public NewParentType Parent { get; set; }
}
public class NewParentType
{
public string Message { get; set; }
public int TypeCode { get; set; }
public TypeA TypeA { get; set; }
public TypeB TypeB { get; set; }
public TypeC TypeC { get; set; }
}
public class TypeA
{
public int Number { get; set; }
public string Text { get; set; }
}
public class TypeB
{
public int Number { get; set; }
public string Text { get; set; }
}
public class TypeC
{
public int Number { get; set; }
public string Text { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment