0

I have the following Mapping configurations:-

Initialized Data:-

private static IEnumerable<Source> InitializeData()
    {
        var source= new[]
        {
            new Source("John", "Doe", "1111111111"),
            new Source("Jack", "Handsome", "2222222222"),
            new Source("Joe", "Mackenze", "3333333333")
        };
        return source;
    }

Source Model:

  public class Source
    {
        private string First { get; set; }
        private string Last { get; set; }
        private string Phone { get; set; }

        public Source(string first, string last, string phone)
        {
            First = first;
            Last = last;
            Phone = phone;
        }
    }

Destination Model

 public class Destination
    {
        public string First { get; set; }
        public string Last { get; set; }
        public string Phone { get; set; }
    }

Main

 static void Main(string[] args)
        {

            var config = new MapperConfiguration(cfg =>
            {
                cfg.AllowNullCollections = true;
                cfg.CreateMap<Source, Destination>().ReverseMap();
            });

            var mapper = new Mapper(config);

            var source= InitializeData();
            var people = mapper.Map<IEnumerable<Destination>>(source);

            foreach (var p in people)
            {
                Console.WriteLine("Name: {0}-{1} Phone: {2}", p.First, p.Last, p.Phone);
            }

            Console.ReadLine();
        }

Problem descriptions:

I have been struggled to understand the AutoMapper mapping between source and destination models. My source model has a constructor to initialize or accept data from outside. It works fine when I removed the source constructor from the model that's mean flat mapping works fine but constructor initialization has the issue. When I debug in VS2019, it shows the number of records but all fields are empty/null.

What is wrong with the above mapping. I have gone through the AutoMapper reference docs but do not get a hold on this issue.

I highly appreciate your help!

Urgen
  • 1,095
  • 3
  • 19
  • 39

1 Answers1

0

Try calling AssertConfigurationIsValid. Check http://docs.automapper.org/en/latest/Configuration-validation.html.

Your Source properties are private. I assume you meant public.

Lucian Bargaoanu
  • 3,336
  • 3
  • 14
  • 19
  • I set them private if I left them public than there is no reason to Setup a constructor to initialize these fields. I don't want them to get their state change outside the class. How do I make them private within the class and mapping with ViewModel. I don't want them to be visible outside this class directly but through constructor – Urgen Jun 17 '20 at 08:49
  • The setters can be `private`. – Lucian Bargaoanu Jun 17 '20 at 08:56
  • This seems visibility issue however, there should a way to achieve through what I am thinking. I will do some researches myself. BTW Thanks!. – Urgen Jun 17 '20 at 09:02
  • You're making it hard on yourself. While it's possible to map from private fields, you'll find out soon enough that it's not feasible in the general case. – Lucian Bargaoanu Jun 17 '20 at 09:10