AutoMapper is a. NET object mapping tool.
Project address: Https://github.com/AutoMapper/AutoMapper.
Help Document: Https://github.com/AutoMapper/AutoMapper/wiki
Main purpose
The transformation between the domain object and the DTO, and the database query results are mapped to the entity object.
The main point here is to use AutoMapper to convert IDataReader, datasets, and DataTable into entities.
Dependent files: AutoMapper.dll, AutoMapper.Net4.dll two DLL files
AutoMapper.Net4.dll This file can download the code to compile itself, the file is encapsulated with support for IDataReader
Simply say AutoMapper use
First step: Declaring a mapping convention
mapper.createmap<idatareader, menumodel> ();//Will idatareader map to menumodel entity
Step two: Convert entity
//idatareader =>menumodel
using (IDataReader dr = ...)
{
var list = mapper.map<list<menumodel>> (DR);
Dr. Close ();
}
Here is a automapper helper class for your own package
How to use:
First step: the model to be converted is under the convention in the static constructor
<summary>
Registering Mapper Conversion Rule Conventions
</summary>
static void Configure ()
{
Mapper.createmap<idatareader, menumodel> ();
...
Mapper.createmap<idatareader, xxxxxmodel> ();
}
Step two: In the project application set, use the already written extension method
Using Utitity.automapper
IDataReader dr = ...;
var list1 = Dr. Getentity<list<menumodel>> ();
DataSet ds = ...;
var list2 = ds. Getentity<list<menumodel>> ();
DataTable dt = ...;
var list3 = dt. Getentity<list<menumodel>> ();
Mapperhelper Source
Using automapper;using system.data;namespace utitity.automapper{//<summary>//Entity Mapping helper class///</summary > public static class Mapperhelper {#region Configuration mapping Rules///<summary>//Ensure that the mapping configuration is registered only once </summary> static Mapperhelper () {Configure (); }///<summary>//Registration Mapper Conversion Rules Convention///</summary> static void Configure () {Mapper.createmap<idatareader, xxxxmodel> ();//only need to contract the underlying type, not to write list<xxxxmodel> this form} #endregion #region Entity Mapping extension method///<summary>///convert IDataReader to Entity object///</summary> <typeparam name= "T" ></typeparam>//<param name= "Dr" ></param>//<re turns></returns> public static T getentity<t> (this IDataReader dr) {return Mapper . Map<t> (DR); }//<summary> Convert DataSet to Entity object///</summary>//<typeparam name= "T" ></typeparam>//<pa Ram Name= "DS" ></param>///<returns></returns> public static T getentity<t> (this DataSet ds) {if (ds = = null | | | ds. Tables.count = = 0 | | Ds. Tables[0]. Rows.Count = = 0) return default (T); var dr = ds. Tables[0]. CreateDataReader (); return mapper.map<t> (DR); }///<summary>//Convert DataTable to Entity object////</summary>//<typeparam name= "T" &G t;</typeparam>//<param name= "DT" ></param>///<returns></returns> PU Blic static T getentity<t> (this DataTable dt) {if (dt = = NULL | | dt. Rows.Count = = 0) return default (T); var dr = dt. CreateDataReader (); return mapper.map<t> (DR); } #endregion}}
Mapping IDataReader, datasets, DataTable to entity classes using AutoMapper