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> ();//Map IDataReader to Menumodel entity
Step Two: Convert Entities
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>///Registration Mapper Conversion Rules Convention///</summary> static void Configure () {mapper.createmap<idatareader, M Enumodel> ();
...
Mapper.createmap<idatareader, xxxxxmodel> (); }
Step two: In the project application set, use the already written extension method
[CSharp]View Plaincopy
- 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
[CSharp]View Plaincopy
- Using AutoMapper;
- Using System.Data;
- Namespace Utitity.automapper
- {
- // <summary>
- /// Entity Mapping Helper class
- // </summary>
- public static class Mapperhelper
- {
- #region Configure mapping rules
- // <summary>
- /// Ensure that the mapping configuration is registered only once
- // </summary>
- static mapperhelper ()
- {
- Configure ();
- }
- // <summary>
- /// registration Mapper Conversion Rule Conventions
- // </summary>
- static void Configure ()
- {
- Mapper.createmap<idatareader, xxxxmodel> (); //Only need to agree on the basic type, do not write list<xxxxmodel> this form
- }
- #endregion
- #region Entity Mapping Extension methods
- // <summary>
- /// convert IDataReader to entity object
- // </summary>
- /// <typeparam name= "T" ></typeparam>
- /// <param name= "Dr" ></param>
- // <returns></returns>
- public Static T-getentity<t> (thisIDataReader dr)
- {
- return mapper.map<t> (DR);
- }
- // <summary>
- /// convert DataSet to entity object
- // </summary>
- /// <typeparam name= "T" ></typeparam>
- /// <param name= "ds" ></param>
- // <returns></returns>
- public Static T-getentity<t> (thisDataSet 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" ></typeparam>
- /// <param name= "DT" ></param>
- // <returns></returns>
- public Static T-getentity<t> (thisDataTable 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