0

I am new to Automapper. With below links, I am trying to understand it in action.

I am using its Automapper v 5.2.0

Here is my stuff. https://codepaste.net/xph2oa

class Program
{
    static void Main(string[] args)
    {
        //PLEASE IGNORE NAMING CONVENTIONS FOR NOW.Sorry!!
        //on Startup 
        AppMapper mapperObj = new AppMapper();
        mapperObj.Mapping();

        DAL obj = new DAL();
        var customer = obj.AddCustomers();


    }
}

class Customer
{
    public int CustomerId { get; set; }

    public string CustName { get; set; }
}


class CustomerTO
{
    public int CustId { get; set; }

    public object CustData { get; set; }
}


class AppMapper
{
    public void Mapping()
    {
        var config = new MapperConfiguration(cfg =>
                    {
                        cfg.CreateMap<Customer, CustomerTO>();
                    });

        IMapper mapper = config.CreateMapper();

    }
}

class DAL
{
    public IEnumerable<CustomerTO> AddCustomers()
    {
        List<Customer> customers = new List<Customer>();
        customers.Add(new Customer() { CustName = "Ram", CustomerId = 1 });
        customers.Add(new Customer() { CustName = "Shyam", CustomerId = 2 });
        customers.Add(new Customer() { CustName = "Mohan", CustomerId = 3 });
        customers.Add(new Customer() { CustName = "Steve", CustomerId = 4 });
        customers.Add(new Customer() { CustName = "John", CustomerId = 5 });

        return customers;   //throws error

    }
}

Error -Cannot implicitly convert type System.Collections.Generic.List' to ' System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)

How do I map List<Customer> to List<CustomerTO> ?

Please note, in Customer I have property of type string with name Custname while CustomerTO I have the property with name CustData of type object. So how do I map this different name property?

Thanks.

d219
  • 2,707
  • 5
  • 31
  • 36
Kgn-web
  • 7,047
  • 24
  • 95
  • 161
  • Check [this](http://stackoverflow.com/questions/37348788/automapper-5-0-global-configuration) i think it will help you. But I don't know if you can `map` from `string` to `object` – Elmer Dantas Feb 21 '17 at 10:31
  • Have you looked at the wiki? It has the most up-to-date documentation instead of my blog, which can be out of date (for example the static API is still, and will be, there). – Jimmy Bogard Feb 21 '17 at 22:31
  • @JimmyBogard, Thanks for the blog. Your blog + some other links was suffcient enough me to get started. I didn't check to wiki yet. – Kgn-web Feb 22 '17 at 07:03

1 Answers1

1

Using the same names for properties in the types to be mapped is the simplest way to us AutoMapper. That way the config you have now will work.

However, in the case where you don't do that you need to specify how the properties are to be mapped, as follows

cfg.CreateMap<Customer, CustomerTO>()
.ForMember(dto => dto.CustData, opt => opt.MapFrom(entity => entity.CustName))
.ForMember(dto => dto.CustId, opt => opt.MapFrom(entity, entity.CustomerId));

I'm assuming that you want to straight map CustName to CustData above, and this will work fine.

Barry O'Kane
  • 1,189
  • 7
  • 12
  • Suppose I have 10+ properties in Customer & CustomerTO. 9 properties have same name but 1 property name & type is different. In such case I need to write .ForMember<> 10 times?? – Kgn-web Feb 21 '17 at 10:35
  • No you only need to specify for the member that is differently named. – Barry O'Kane Feb 21 '17 at 10:36
  • Can you please check the updated post, I am getting build error, in the DAL method – Kgn-web Feb 21 '17 at 10:41
  • You're not actually mapping from `Customer` to `CustomerTO` at any point in your `AddCustomers()` method. You need to call `.Map>(customers);` on an instance of your `AppMapper` class. – Barry O'Kane Feb 21 '17 at 10:48
  • In Main() I did, So again I need to associate this mapping glue?? – Kgn-web Feb 21 '17 at 10:51
  • In `AddCustomers()` the return type is `IEnumerable`. So either change the signature to `IEnumerable` or move the mapping into `AddCustomers()`. Also your code doesn't actually show any mapping, so is it just not shown here? – Barry O'Kane Feb 21 '17 at 11:18
  • I think moving Mapping into AddCustomer is violation of SOLID. am I right?? – Kgn-web Feb 21 '17 at 11:23
  • That's an entirely separate question – Barry O'Kane Feb 21 '17 at 11:27
  • Yes. right. I am trying your stuff but stuck in the further error, http://stackoverflow.com/questions/42366605/how-do-i-map-this-using-automapper – Kgn-web Feb 21 '17 at 11:55