Examples of automapper applications in C #

Source: Internet
Author: User
Recently found a more interesting thing automapper, the main conversion model to dto,dto more attention to data, the domain objects are properly encapsulated, so that the behavior of the domain object will not be too exposed to the performance layer.

Let's take a look at a few instances before the two class mappings.

First define two classes of source and Dtosource:

public class Source    {public        int Id {get; set;}        public string Content {get; set;}    }    public class Dtosource    {public        int Id {get; set;}        public string Content {get; set;}    }

The source is exactly the same as the Dtosource field, and it's easy to see how they convert through AutoMapper.

Mapper.initialize (x=>{     x.createmap<source,dtosource> ();}); Source s = new source{id=1,content= "123"};D tosource dto = mapper.map<dtosource> (s);

The first step is to establish a mapping between source and Dtosource, and after initializing a source instance, take a look at the result:

After execution, you can see that the data in the DTO is the same as the data from the previously initialized s, as if you had copied the S to the DTO directly, so if the two class field names are all the same, then if the field names in the Dtosource are not the same as in the source, it is actually very simple, just

To change to a little bit of code, you can either:

We changed the field name of the content in Dtosource to Desc, when we only need to establish a mapping relationship, the specified field is available:

1 mapper.initialize (x = {2    x.createmap<source, dtosource> (). Formember (c=>c.desc,q=> {3       q.mapfrom (z = z.content); 4      }); 5});

Let's see how the results work.

You can see that the result is the same as the previous run.

So how to map the two list, in fact, is very simple, and the above code can almost be said to be no difference, just in the last step, to do a little bit of modification can be. As in the following code:

Mapper.initialize (x = {                X.createmap<source, dtosource> (). Formember (c = c.desc, q = =                {                    Q.mapfrom (z = z.content);                }            ); S.add (new Source {Id = 1, Content = "123"});            var dto = mapper.map<list<dtosource>> (s);

You can see that except for the last line of code, the other is almost identical, except in the last sentence, the target type is changed to list<dtosource>. See how the results work:

The results are in line with expectations.

In the actual project, such writing is certainly not in line with the requirements, will generally do a package, create a new sourceprofile inherited from the profile:

1 public  Sourceprofile () 2         {3             base. Createmap<source, Dtosource> (). Formember (c = c.desc, q = = {4                 q.mapfrom (z = z.content); 5             }); 6         }

All mappings can be written in this class, only one call at a time when the program is initialized:

1 mapper.initialize (x =>{  x.addprofile<sourceprofile> ();});

Bloggers Use the AutoMapper version 6.1.1.0, because AutoMapper removed the configure in profile at 6.0, so it's a little different than the 6.0 version, with 6.0 following versions:

public class Sourceprofile:profile    {        protected override void Configure ()        {            Createmap<source, Dtosource> (). Formember (c = c.desc, q = = {                Q.mapfrom (z = z.content);}            )        ;    }

The inherited profile overrides its configure, and the invocation is not much different from the above. You can add one or more profiles in Mapper.initialize.

In the application of MVC project, Mapper.initialize can be encapsulated into a class;

public static class Automapperformvc    {        public  static void Register ()        {            mapper.initialize (x = {                x.addprofile<sourceprofile> ();            });        }    }

And then one-time registration in MVC's global:

public class MvcApplication:System.Web.HttpApplication    {        protected void Application_Start ()        {            Arearegistration.registerallareas ();            Filterconfig.registerglobalfilters (globalfilters.filters);            Routeconfig.registerroutes (routetable.routes);            Bundleconfig.registerbundles (bundletable.bundles);            Register            Automapperformvc.register ();        }    }
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.