automapper--is destined to become a fate (a)

Source: Internet
Author: User

And AutoMapper is to do a high-efficiency platform project at the beginning of the time so that some students strongly recommend me to see, after watching the feeling this thing can really very good, the following share to everyone!

First introduce its background, combined with the project I am doing now, the front of the project is MVC, backstage with wcf+ three + Factory + reflection, looks very tall on the sample paper ~ ~, so began the impulse of the Code trip, But in the writing data contract found in the EF generated entities I use a lot of, so write AH ~ copy Ah ~ paste Ah ~ Originally thought very frost, all pure handmade, but met AutoMapper, I regret ~ Mo and ~ ~

in the process of looking at the blog of the Great Gods, we found a kind of model called anemia, which is actually the entity model that our data table map handles, this kind of model has only a fixed number of attributes, if you want to use the properties of multiple models, it will be 18 bends, for example

If the Entity Framework is likened to a machine gun, the attribute of the physical class is a bullet, each bullet can only attack the only target, and in the course of the shooting, the machine gun will jam as long as there is no target of one bullet attacking (the bullet determines the target?). )。 At this point, the Entity framework became a heap of scrap metal.

Why didn't the bullet be decided by the target? What is the target, with what bullets, save bullets, and will not jam. That is, the corresponding entity class attribute is assigned according to the result of the query. Is there a design flaw in this new weapon that does not take into account the application scenario? Or are we not going to use it?

First, download

2, The following is a small example of what I do: Remember to add a reference Oh!

<span style= "FONT-SIZE:14PX;" ><strong><span style= "color: #ff0000;" >using automapper;</span></strong>namespace consoleapplication1{    class program    {        static void Main (string[] args)        {            //Create entity mapping rules, onclass for source data contracts, timetable for target data contracts            mapper.createmap< Onclass, timetable> ();            Assign a value to the source data            onclass onclass = new Onclass            {                onclassid = "001",                Onclassno = "002",                onclassname = " Dandan "            };            Map the value of the source data to the target data            Timetable timetable = mapper.map<timetable> (onclass);            Show           Console.WriteLine (timetable. Onclassid+timetable. Onclassno+timetable. onclassname);}}}    </span>
look at the effect:

The simple automapper introduced, did not feel its great effect? If your EF doesn't meet your program implementation and needs to call other models, consider it

automapper--is destined to become a fate (a)

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.