. NET Platform Open source project quick glance-the fastest object mapping component Tiny Mapper Project practice

Source: Internet
Author: User

Mood small: Recently changed work, bitter force in 22:00 after work, room a messy ~ small Cui despise to: "You are suitable for living in the garbage heap!!!" "

Visit the blog in the evening to see a very useful blog:. NET Platform Open Source project Quick glance (14) Fastest object mapping component tiny Mapper, took 10 minutes to get a quick look.

It is also valuable to see comments, and many novice students are asked to use the situation in the actual project.

The following is a slight adjustment on the basis of the original author's code to illustrate the use of the actual project scenario:

Step One: Understanding Class Library methods: Tinymapper has two main functions:

Tinymapper.bind<t1, t2> ();//Binding mapping relationship

Tinymapper.map<t> (obj);//Get the desired object from the object

Step Two: Initialize the mapping settings

Description: Similar mapping settings are nothing more than two modes: code static object initialization mode, XML configuration mode, and it is an honor to Tinymapper support a static dictionary.

 Public Static classTinymapcontext { Public Static voidinitmapping () {Tinymapper.bind<person, persondto>(); Tinymapper.bind<person, persondto> (config ={config. Ignore (x= X.id);//Ignore ID fieldConfig. Bind (x = x.name, y = y.username);//bind the field of the source type and the target type to matchConfig. Bind (x = x.age, y = y.age);//bind the field of the source type and the target type to match            }); Tinymapper.bind<person, persondto> (config ={config. Ignore (x= X.id);//Ignore ID field//bind the field of the source type and the target type to matchConfig. Bind (x = x.name, y = =y.username); Config. Bind (x= = X.age, y =y.age); Config. Bind (x= = x.address, y =y.address); Config. Bind (x= = X.emails, y =y.emails);            }); Tinymapper.bind<PersonDto,Person>(); Tinymapper.bind<PersonDto,Person> (config ={config. Bind (x= x.id,y=>y.id); Config. Bind (x= = X.username, y =y.name); Config. Bind (x= = X.age, y =y.age);            }); Tinymapper.bind<PersonDto,Person> (config ={config. Bind (x= X.id,y=>y.id);//Ignore ID field//bind the field of the source type and the target type to matchConfig. Bind (x =>x.username,y=>y.name); Config. Bind (x= = X.age, y =y.age); Config. Bind (x= = x.address, y =y.address); Config. Bind (x= = X.emails, y =y.emails);        }); }         Public StaticT getmapobject<t> (ObjectMB ()whereT:class        {           returnTinymapper.map<t>(obj); }    }

Description: In the above mapping mapping, for the original author code, added: by persondto= "person" mapping relationship.

The role of the where T:class in T getmapobject<t> (object obj) is reflected in the code that follows. A simple method, the power can not be small hush ~ ~

Step three: Dtomodel-"Model

Mapping from a database model to a domain model:

var p = tinymapcontext.getmapobject<person> (persondto);

Fourth step: model-"Dtomodel

From the domain model to the database model:

var persondto = tinymapcontext.getmapobject<persondto> (person);

Fifth Step:list<model>=>list<dtomodel> or list<dtomodel>=>list<model>

        /// <summary>        ///The test List object. /// </summary>        Static voidTest4 () {List<Person> personlist =NewList<person>(){            NewPerson {Id=Guid.NewGuid (). ToString (), Name="John1", Age= A, Address=NewAddress () {Phone ="1880393", Street ="Shanghai", ZipCode ="121212"}, Emails=Newlist<string> () {"[email protected]","[email protected]" }            },            NewPerson {Id=Guid.NewGuid (). ToString (), Name="John2", Age= A, Address=NewAddress () {Phone ="1880393", Street ="Shanghai", ZipCode ="121212"}, Emails=Newlist<string> () {"[email protected]","[email protected]" }            },              NewPerson {Id=Guid.NewGuid (). ToString (), Name="John3", Age= A, Address=NewAddress () {Phone ="1880393", Street ="Shanghai", ZipCode ="121212"}, Emails=Newlist<string> () {"[email protected]","[email protected]" }            },              NewPerson {Id=Guid.NewGuid (). ToString (), Name="John4", Age= A, Address=NewAddress () {Phone ="1880393", Street ="Shanghai", ZipCode ="121212"}, Emails=Newlist<string> () {"[email protected]","[email protected]" }            }            }; varPersondtolist = tinymapcontext.getmapobject<list<persondto>>(personlist); foreach(varIteminchpersondtolist) {Debugoutpututil.debugoutput (item.            UserName); }            varPList = tinymapcontext.getmapobject<list<person>>(persondtolist); foreach(varIteminchpList) {Debugoutpututil.debugoutput (item.            Name); }        }

Special note: For objects that get list types, you do not need to add additional IList-type objects in the static initialization mappings. Tinymapper automatically helps you get the data you want based on the "mapped underlying type" dynamically.

Code for this article: example

Description: The code is easy to understand, but people are not perfect, coding is not standard place, hand error, or code has defects, or serious performance problems, I hope the friends of the park to criticize.

. NET Platform Open source project quick glance-the fastest object mapping component Tiny Mapper Project practice

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.