Use AutoMapper to map IDataReader, DataSet, and able to object class, And automapperdatatable
AutoMapper is A. NET object ing tool.
Project address: https://github.com/automapper/automapper.
Help document: https://github.com/AutoMapper/AutoMapper/wiki
Main Purpose
The conversion between the domain object and DTO, and the database query result are mapped to the object.
The following describes how to convert IDataReader, DataSet, and able into entities using AutoMapper.
Dependent files: AutoMapper. dll and AutoMapper. Net4.dll
The AutoMapper. Net4.dll file can be downloaded and compiled by itself. This file encapsulates support for IDataReader.
To put it simply, use AutoMapper
Step 1: declare a ing Convention
Mapper. CreateMap <IDataReader, menuModel> (); // map IDataReader to menuModel object
Step 2: Convert objects
// IDataReader => menuModel
Using (IDataReader dr =...) {var list = Mapper. Map <List <menuModel> (dr); dr. Close ();}
The following is a self-encapsulated AutoMapper help class
Usage:
Step 1: Specify the model to be converted in the static constructor.
/// <Summary> // register the Mapper conversion rule Convention // </summary> static void Configure () {Mapper. CreateMap <IDataReader, menuModel> ();
...
Mapper. CreateMap <IDataReader, xxxxxModel> ();}
Step 2: Apply the assembly in the project and use the extension method that has been written
[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 code
[Csharp]View plaincopy
- Using AutoMapper;
- Using System. Data;
- Namespace Utitity. AutoMapper
- {
- /// <Summary>
- /// Entity ing help class
- /// </Summary>
- Public static class MapperHelper
- {
- # Region configuration ing rules
- /// <Summary>
- /// Make sure that the ing configuration is registered only once
- /// </Summary>
- Static MapperHelper ()
- {
- Configure ();
- }
- /// <Summary>
- /// Register the Mapper conversion rule Convention
- /// </Summary>
- Static void Configure ()
- {
- Mapper. CreateMap <IDataReader, xxxxModel> (); // you only need to specify the basic type. Do not write it as List <xxxxModel>.
- }
-
- # Endregion
-
-
- # Region object ing Extension Method
- /// <Summary>
- /// Convert IDataReader into an object
- /// </Summary>
- /// <Typeparam name = "T"> </typeparam>
- /// <Param name = "dr"> </param>
- /// <Returns> </returns>
- Public static T GetEntity <T> (this IDataReader dr)
- {
- Return Mapper. Map <T> (dr );
- }
- /// <Summary>
- /// Convert DataSet into an object
- /// </Summary>
- /// <Typeparam name = "T"> </typeparam>
- /// <Param 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 the DataTable object
- /// </Summary>
- /// <Typeparam name = "T"> </typeparam>
- /// <Param name = "dt"> </param>
- /// <Returns> </returns>
- Public 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
- }