0

I have Source class and Destination class to map. Destination class is a generic class such as Person which will be used as a field in one of my class (MainClass) in various fields e.g Father,Mother,Brother etc. How can I map data coming from source to destination which is present for Father,Mother etc.

I can create

CreateMap<Source, MainClass>()
.ForMember(dest => dest.Mother, m => m.MapFrom(source => source))
.ForMember(dest => dest.Father, m => m.MapFrom(source => source))
.ForMember(dest => dest.Brother, m => m.MapFrom(source => source));

 Mapper.CreateMap<Source, Destination>()  
.ForMember(dest => dest.Name,  m => m.MapFrom(source => source.motherName))
.ForMember(dest => dest.ID,  m => m.MapFrom(source => source.motherId))
.ForMember(dest => dest.Address,  m => m.MapFrom(source => source.motherAddress));

but how can I handle mapping for father,brother etc. to achieve

 Mapper.CreateMap<Source, Destination>()  
.ForMember(dest => dest.Name,  m => m.MapFrom(source => source.FatherName))
.ForMember(dest => dest.ID,  m => m.MapFrom(source => source.FatherId))
.ForMember(dest => dest.Address,  m => m.MapFrom(source => source.FatherAddress));
Imgane5h
  • 304
  • 1
  • 3
  • 16
  • I think if you try to map the same source and destination you while get an error right? Try to validate it with: AutoMapper.Mapper.Configuration.AssertConfigurationIsValid(); – Andre Fritzsche Mar 03 '20 at 06:29
  • And if im right you can create a method where you send in the object and the name of the sufix of the property like "name" and then check the propertienames Is the name of the properties FatherName or MotherName or BrotherName you can return the value of the So depending on the kind of Person the name of this properties change right? This could be helpfull if im getting it right: https://stackoverflow.com/questions/737151/how-to-get-the-list-of-properties-of-a-class – Andre Fritzsche Mar 03 '20 at 06:32
  • I am not sure if you got my point here. The problem i am facing here is I can define one mapping either for father,mother or brother as the same class is used as destination. I am using only Person class in all cases. How can I map multiple things here is the question. If you address the same in your answer can you provide code snippet for my example that would be great. – Imgane5h Mar 03 '20 at 06:37
  • Ok may be i dont get it Do you classes look this? public class MainClass { public Destination Mother { get; set; } public Destination Father { get; set; } public Destination Brother { get; set; } } public class Source { public string MotherName{ get; set; } public string FatherName { get; set; } ... public int MotherId { get; set; } ... } public class Destination { public string Name { get; set; } public string Id { get; set; } ... } – Andre Fritzsche Mar 03 '20 at 06:57
  • Yes you got it right – Imgane5h Mar 03 '20 at 07:02
  • Ok great. And on more thing i need to know. Do you have multiple Objects where just the motherName and motherId is filled or one object where everything is filled? – Andre Fritzsche Mar 03 '20 at 07:11
  • one object where everything is filled – Imgane5h Mar 03 '20 at 08:12

1 Answers1

-1

Ok so this is an untested Code from an notepad editor :D

you can try this and change it in the way you need it. This will not work from the very beginning!

opt.MapFrom(source => SetName(source, "Mother")))

    private object SetName(Person y, string personState)
    {
        Person person = new Person();
        var properties = DictionaryFromType(y);
        foreach(var property in properties)
        {
            if(property.Key.ToLower().Contains(personState.ToLower()))
            {
     // you should make the real mapping to id here. This is just example code on how it could work
                PropertyInfo propertyInfo = person.GetType().GetProperty(property.Key);
                propertyInfo.SetValue(person, Convert.ChangeType(property.Value, propertyInfo.PropertyType), null);
            }
        }

        return person;
    }

    public static Dictionary<string, object> DictionaryFromType(object atype)
    {
        if (atype == null) return new Dictionary<string, object>();
        Type t = atype.GetType();
        PropertyInfo[] props = t.GetProperties();
        Dictionary<string, object> dict = new Dictionary<string, object>();
        foreach (PropertyInfo prp in props)
        {
            object value = prp.GetValue(atype, new object[] { });
            dict.Add(prp.Name, value);
        }
        return dict;
    }

may be you have to debug a little bit to make it work but somehow like this you can make it.

It is possible that there are better solutions with automapper, but i only had this in my mind for the moment.

Hope this can help you even if this is not a finished answer! (Sorry my time is low atm)

Andre Fritzsche
  • 126
  • 2
  • 11