Mapping two classes easily in C#
I've a web application and a service layer running in different places and both have their 开发者_如何学Pythonown business entities, means both have their own classes to represent an employee, order etc (ex. Emp in service layer and Employee in web app). When the web application invokes the service layer to get a list of employees I want to transform the list of employees returned by service to the list of web application's employee type.
I'm looking for a way to do this easily. Any ideas will be great. By the way I'm using ASP.NET and WCF.
Use AutoMapper.
AutoMapper is a simple little library built to solve a deceptively complex problem - getting rid of code that mapped one object to another. This type of code is rather dreary and boring to write, so why not invent a tool to do it for us?
General Features
- Flattening
- Projection
- Configuration Validation
- Lists and Arrays
- Nested Mappings
- Custom Type Converters
- Custom Value Resolvers
- Custom Value Formatters
- Null Substitution
Here's a sample from : wlegant Code
Before automapper
var meeting = _repository.GetMeetingById(meetingId);
var dto = new MeetingDto();
dto.Begins = meeting.Begins;
dto.End = meeting.End;
dto.Attendees = meeting.Attendees;
dto.AttendeesCount = meeting.Attendees.Count;
//do something meaningful
and using auto mapper
var meeting = _repository.GetMeetingById(meetingId);
var dto = Mapper.Map<Meeting, MeetingDto>(meeting);
You could use Automapper:
https://github.com/AutoMapper/AutoMapper
It helps you map one type to another. Your input objects (WCF) will be transformed into an object of another type (Web application). Automapper is able (for a large part) to figure this out automatically. Little configuration is needed.
To map two types:
Mapper.CreateMap<WcfEmployee, WebAppEmployee>();
To convert a type to another:
WebAppEmployee employee = Mapper.Map<WcfEmployee, WebAppEmployee>(employee);
For the most part Automapper uses name-based convention to map two types, but IIRC you can certainly tweak this. For this you need to inform Automapper of your convention rules. In other words, the rules for how it should map your types.
Personally I wouldn't recommend doing it in a simple way but rather in a very conscius way. Only map the things from the service to the application that the app actually need and only expose what is absolutely necesary to expose. In other words keeps as much, preferrably all of the data the service exposes internal to th eapp object.
Usually data from a service is used to base functionality upon. Expose the functionality instead of the data. That will make it possible for you to change the data structure completely (as long as it supports the same mental model/functional requirements) with out having to rewrite anything based on the Application side object. You'd of course need to rewrite the application side class.
If the class properties share the same names and typing the simplest way to do this is via the JsonSerializer:
using System.Text.Json;
public class MappingService
{
/// <summary>
/// Converts model from class F to class T
/// </summary>
/// <typeparam name="T">To Class</typeparam>
/// <typeparam name="F">From Class</typeparam>
/// <returns>model of type class T</returns>
public T Map<F, T>(F from)
{
var json = JsonSerializer.Serialize(from);
var to = JsonSerializer.Deserialize<T>(json);
return to;
}
}
receivedEmployesArray.Select(x => new MyWinFormsEmploeType(x)); // if you create intializaion in constructor
receivedEmployesArray.Select(x => new MyWinFormsEmploeType() {
Name = x.Name,
Position = x.Position
}); // trasfering property to property
Or the most progressive way - use automapper
精彩评论