AutoMapper (7), automapper

Source: Internet
Author: User

AutoMapper (7), automapper
Returns the replacement of Null values in the total directory.

If the attribute value of the source type member chain is Null, the replacement of the Null value allows a replaceable value. The following two classes are available: Person and PersonInfo. Each class has a Title attribute mapped from Person to PersonInfo. If the attributes of Person are not assigned a value, then, replace the corresponding property value of PersonInfo with ".

Namespace SeventhAutoMapper {class Person {public string Title {get; set ;}} class PersonInfo {public string Title {get; set ;}} class Program {static void Main (string [] args) {// ing er. createMap <Person, PersonInfo> (). forMember (dest => dest. title, opt => opt. nullSubstitute (" ute"); // if the source attribute is null, set it to "" // execute the var ing var personInfo = Mapper. map <PersonInfo> (new Person (); // The Source attribute is not assigned a value, so it is null var personInfo2 = Mapper. map <PersonInfo> (new Person () {Title = "Gao fushuai"}); // The Source attribute has a value. // output result Console. writeLine ("personInfo. title = "+ personInfo. title); Console. writeLine ("personInfo2.Title =" + personInfo2.Title); Console. read ();}}}

The test results are as follows:

Open generic

AutoMapper supports open generic ing. Create two generic classes as follows:

class Soure<T>{    public T Value { get; set; }}class Destination<T>{    public T Value { get; set; }}

We do not need to create a closed generic type (that is, do not write the specific type into angle brackets ), autoMapper will apply any configuration of the open generic type to the closed ing at runtime.

// Create an open generic Mapper. createMap (typeof (Source <>), typeof (Destination <>); var src1 = new Source <int> {Value = 22}; var dest1 = Mapper. map <Destination <int> (src1); Console. writeLine (dest1.Value); var src2 = new Source <string> {Value = "Hello, AutoMapper! "}; Var dest2 = Mapper. map <Destination <string> (src2); Console. writeLine (dest2.Value );//...... and so on. read ();

The test results are as follows:

Because C # only allows disabling generic parameters, you must use the CreateMap method without generic parameters to create your own open generic parameter ing, you can also use all available ing configurations. AutoMapper skips open generic type mappings during configuration verification.

You can also create an open generic converter:

Mapper.CreateMap(typeof(Source<>), typeof(Destination<>)).ConvertUsing(typeof(Converter<>));
Projection

A flat object model is used to convert a source type into a target type. No additional configuration is required. AutoMapper only requires a flat target type to match the naming structure of the source type. When you project a source value to a target value that does not accurately match the source structure, you must specify the member ing definition.

For example, we want to convert a source structure CalendarEvent into a target structure CalendarEventForm that facilitates user input on the web page:

public class CalendarEvent{    public DateTime Date { get; set; }    public string Title { get; set; }}public class CalendarEventForm{    public DateTime EventDate { get; set; }    public int EventHour { get; set; }    public int EventMinute { get; set; }    public string Title { get; set; }}

Because the target attribute does not match the source attribute very well (CalendarEvent. Date needs to become CalendarEventForm. EventDate), we need to specify the Member's ing rules in the type ing Configuration:

Var calender = new CalendarEvent () {Date = DateTime. now, Title = "historical moment"}; // creates a Mapper. createMap <CalendarEvent, CalendarEventForm> (). forMember (dest => dest. eventDate, opt => opt. mapFrom (src => src. date. date )). forMember (dest => dest. eventHour, opt => opt. mapFrom (src => src. date. hour )). forMember (dest => dest. eventMinute, opt => opt. mapFrom (src => src. date. minute); // execute the var ing var calenderForm = Mapper. map <CalendarEventForm> (calender); // outputs the Console of the object before ing. writeLine ("calender. date = {0}, Title = {1} ", calender. date, calender. title); // output the mapped object foreach (PropertyInfo info in calenderForm. getType (). getProperties () {Console. writeLine (info. name + "=" + info. getValue (calenderForm ));}

The test results are as follows:

 

All right, the series of tutorials on AutoMapper come to an end. At that time, the original intention of this series of tutorials was to smoothly develop the project and now it will be used, the topic about AutoMapper is now available, but there will certainly be a blog about AutoMapper in the future.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.