AutoMapper First Use Experience

Source: Internet
Author: User

This example takes the example of ASP. WebForm:

Structure:

Main code: Automapperconfig class

public class Automapperconfig
{
public static void Registerautomapper ()
{
AutoMapper.Configuration.Configure ();
}
}

Configuration class:

public static void Configure ()
{
Mapper.initialize (cfg =
{
var typetoaddprofile = assembly.getexecutingassembly (). GetTypes ();
foreach (var item in Typetoaddprofile)
{
if (item. BaseType! = NULL && item. Basetype.name = = "Profile" &&!item. BaseType.Name.Equals ("Configuration"))
{
Object o = Activator.CreateInstance (item);
Cfg. Addprofile (o as Profile);
}
}
});
Mapper.assertconfigurationisvalid ();
}

Profiles under Testprofiles class:

public class Testprofiles:profile
{
protected override void Configure ()
{
Createmap<testmodel, dto_testmodel> ();
//. Formember (a = a.address, B = b.mapfrom (c = c.name));
}
}

In Golbal:

protected void Application_Start (object sender, EventArgs e)
{
AutoMapperWeb.AutoMapperConfig.RegisterAutoMapper ();
}

protected void Page_Load (object sender, EventArgs e)
{
var T1 = new List<testmodel> ()
{
New Testmodel () {id=1,name= "Zhang San", age=25,sex= "male", address= "Changsha Yuhua in Hunan Province"},
New Testmodel () {id=1,name= "John Doe", age=25,sex= "male", address= "Changsha Yuhua Future City", Fang "qq=", 9375671 "[Email email=] Phone= "15725032"}
};


var list = Mapper.map<list<testmodel>, list<dto_testmodel>> (T1);
String str = string. Format ("<div>foreach (var item in list)
{
str + = string. Format ("Name:{0},<br/>", item.) Name);
str + = string. Format ("Home Address:{0},<br/>", item. Address);
str + = string. Format ("Qq:{0},<br/>", item. QQ);
str + = string. Format ("Mailbox:{0}}
str + = string. Format ("</div>");
Response.Write (str);
Response.End ();
}

AutoMapper First Use Experience

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.