First, install the build AutoMapper
In Nugget control console input command:pm> install-package automapper, enter confirm installation AutoMapper
Second, the Code
After installing AutoMapper, add references
1. Code module
First, there are two objects to complete the conversion (I use netbook,textbook here):
(1) The Netbook object is the object used to receive data, which is the complete object that receives the database data.
1 Public classNetBook {2 Public intNId {Get;Set; }3 Public stringUse {Get;Set; }4 Public stringContent {Get;Set; }5 Public stringProperty {Get;Set; }6 Public intTotal {Get;Set; }7 Public stringBackgroud {Get;Set; }8 Public stringUser {Get;Set; }9 Public stringUserInfo {Get;Set; }Ten Public BOOLIsdel {Get;Set; } One PublicDateTime Date {Get;Set; } A}
(2) The textbook object is used to display the lost object, reduce the data redundancy and do not need to display the data
1 Public classtextbook{2 Public intTId {Get;Set; }3 Public stringUse {Get;Set; }4 Public stringContentText {Get;Set; }5 Public stringProperty {Get;Set; }6 Public intPagetotal {Get;Set; }7 Public stringBackgroud {Get;Set; }8 Public stringUser {Get;Set; }9}
(3) Code body part
1StringBuilder str =NewStringBuilder ( $);2 3 //---data source---4NetBook NetBook =NewNetBook () {5Backgroud="Blue",6Content="This mylife",7property="Private", 8Use="Log",9Date=DateTime.Now,TenIsdel=false, OneNid=1, ATotal=int. MaxValue, -User ="Coder", -userinfo="code is part of life." the }; - - //---Create a mapping relationship--- - //---One by one mapping--- +Mapper.createmap<netbook, textbook>() -. Formember (d = d.contenttext, dto = dto. Mapfrom (c =c.content)) +. Formember (d = d.use, dto = dto. Mapfrom (c =c.use)) A. Formember (d = d.pagetotal, dto = dto. Mapfrom (c =c.total)) at. Formember (d = d.property, opt = = opt. Ignore ());//do not map this property - - //---Create a new object--- -TextBook TextBook = Mapper.map<netbook, textbook>(netbook); - - //---display--- inStr. Append ("<br/>id:"+textbook. TID); -Str. Append ("<br/> How to use:"+textbook. use); toStr. Append ("<br/> Content:"+textbook. ContentText); +Str. Append ("<br/> Total Pages:"+textbook. Pagetotal); -Str. Append ("<br/> Cover:"+textbook. Backgroud); theStr. Append ("<br/> Properties:"+textbook. property); *Str. Append ("<br/> Users:"+textbook. User); $Response.Write (str. ToString ());View Code
(4) The result of the output
id:0
How to use: Log
Content: This MyLife
Total Pages: 2147483647
Cover: Blue
Property:
User: Coder
(5) Analysis
1. The mapping of the TID is not implemented in the above, and the result is displayed as 0 (that is, the int type default value)
2.. Formember (d = d.use, dto = dto. Mapfrom (c = c.use)) implements two conversion with the same property name, the output is the same, the mapping succeeds
3. No mapping is created for the Backgroud property, but the result is the same
4.. Formember (d = d.pagetotal, dto = dto. Mapfrom (c = c.total)) mapping between different attribute names, resulting in equal output and successful mapping
5. The property attribute name of two objects is the same, but the result output is null
Iii. Summary
1. Object property names are consistent and can be converted without creating a mapping relationship
2. Implement a mapping with inconsistent property names, whose mappings are:. Formember (d = d.pagetotal (new data Object properties), Dto = Dto. Mapfrom (c = c.total (source object property)), same as property name
3. Do not want to implement mappings between the same property names, whose mappings are:. Formember (d + + d.property (new data Object properties), opt = = opt. Ignore () (does not implement mapping relationships))
PS: The above is just my study summary, if you have a better summary, updated knowledge and technology, please provide to share Ah!
Simple use of AutoMapper