Challenge:
We defined the Web-Service and were using it in an application. Web-Service returns an object or list of an object.
There was already code written to do the processing once we get the response object from web-service.But when we get the object via web-service, it append the Web Service Reference Name because of which we were not able to utilize the already written code for processing that object. We believe in Re-usability as much as possible and wanted to re-utilize that code which was also used by other functions. Web-Service was returning lot of data which includes list and list of object inside the list etc.
Solution:
From the AutoMapper CodePlex web page, “AutoMapper is an object-object mapper. Object-object mapping works by transforming an input object of one type into an output object of a different type. What makes AutoMapper interesting is that it provides some interesting conventions to take the dirty work out of figuring out how to map type A to type B. As long as type B follows AutoMapper’s established conventions, almost zero configuration is needed to map two types.” So, in other words, it provides the solution for our problem.
Getting Started Guide: https://github.com/AutoMapper/AutoMapper/wiki/Getting-started
We used this approach to convert complex objects from type A to type B.
Here is the solution that we applied in our case. There might be some other very useful cases for AutoMapper.
AutoMapper.Mapper.CreateMap<MyServiceReference.HistoryModel, HistoryModel>(); AutoMapper.Mapper.CreateMap<MyServiceReference.HistoryTax, HistoryTax>(); AutoMapper.Mapper.CreateMap<MyServiceReference.Tracking, Tracking>(); memberBase.History = AutoMapper.Mapper.Map<IEnumerable<MyServiceReference.HistoryModel>, IEnumerable<HistoryModel>>(myMemberBase.History).ToList();
If you know some better approach do let me know.
Happy AutoMapping! 🙂