AutoMapper Configuration method

Source: Internet
Author: User

In MVC development, we often need to build a viewmodel out for the page to use, between the PO and VO when the value of each other, if the entity field is more, then the value will become very cumbersome, but also makes the code very bloated. AutoMapper can help us to automatically map two entities, and we can also customize some mapping rules, which is really very convenient. Study for a night, find a variety of information, finally put automapper configuration out, in fact, configuration is not so difficult, but I try to map the time to map a Pagelist<t> object, the results of a long time to try to find, is pagelist<t > seemingly can not be simple mapping, specifically how to map I did not study out, if who has a good mapping pagelist approach, welcome to share with me ah, grateful.

The following is a reference to the network of data write mapping configuration, this configuration method only need to be executed at the time of application startup, very convenient.

Define an interface Istartuptask

Public interface Istartuptask    {        void Execute ();    }

The following method is defined in Golbal.asax:

private void Executestartuptasks () {list<istartuptask> startuptasks = new List<istartuptask&gt            ;();            Assembly ASM; Get path of executing (BIN) folder from the current executing assembly string codeBase = Httpruntime.bindire            Ctory;            UriBuilder uri = new UriBuilder (codeBase); String path = Uri.unescapedatastring (Uri.            Path);            String bin = Path.getdirectoryname (Path);            string[] Assemblies = Directory.GetFiles (Bin, "*.dll"); foreach (String file in assemblies) {try {if (File.exists (fi                        Le)) {asm = Assembly.LoadFrom (file); var query = from T in ASM. GetTypes () where T.isclass && T.getinterface (typeof (Istartuptask).                        FULLNAME)! = NULL Select t; Add types to LiSt of Startup tasks foreach (type type in query) {                        Startuptasks.add ((istartuptask) activator.createinstance (type));  }}} catch (Exception ex) {throw new Exception (ex.                Message); }} foreach (Istartuptask task in startuptasks) {task.            Execute (); }        }

Then execute this method in Application_Start:

protected void Application_Start ()        {            executestartuptasks ();        }

Add a mapping class file to the model folder to implement the Istartuptask interface

public class Modelmapping:istartuptask    {public        void Execute ()        {                      Create<adminuserlistmodel,user > ();        }        protected virtual void create<t1, t2> ()        {            mapper.createmap<t1, t2> ();            Mapper.createmap<t2, t1> ();        }      }

The general meaning of this configuration method is that when the program is started, the use of reflection in the Bin directory asked find all the DLL files, then in the DLL to find all the implementation of the Istartuptask interface class, and then to initialize, that is, the implementation of AutoMapper required initialization of some steps, I didn't study it in detail. If you need to map a DLL for a class that is not in the bin directory of the Web site, you can set string codeBase = Httpruntime.bindirectory; To change the initial folder.

AutoMapper Configuration method

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.