Use AutoMapper to map IDataReader, DataSet, and able to object class, And automapperdatatable

Source: Internet
Author: User

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
  1. 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
  1. Using AutoMapper;
  2. Using System. Data;
  3. Namespace Utitity. AutoMapper
  4. {
  5. /// <Summary>
  6. /// Entity ing help class
  7. /// </Summary>
  8. Public static class MapperHelper
  9. {
  10. # Region configuration ing rules
  11. /// <Summary>
  12. /// Make sure that the ing configuration is registered only once
  13. /// </Summary>
  14. Static MapperHelper ()
  15. {
  16. Configure ();
  17. }
  18. /// <Summary>
  19. /// Register the Mapper conversion rule Convention
  20. /// </Summary>
  21. Static void Configure ()
  22. {
  23. Mapper. CreateMap <IDataReader, xxxxModel> (); // you only need to specify the basic type. Do not write it as List <xxxxModel>.
  24. }
  25.  
  26. # Endregion
  27.  
  28.  
  29. # Region object ing Extension Method
  30. /// <Summary>
  31. /// Convert IDataReader into an object
  32. /// </Summary>
  33. /// <Typeparam name = "T"> </typeparam>
  34. /// <Param name = "dr"> </param>
  35. /// <Returns> </returns>
  36. Public static T GetEntity <T> (this IDataReader dr)
  37. {
  38. Return Mapper. Map <T> (dr );
  39. }
  40. /// <Summary>
  41. /// Convert DataSet into an object
  42. /// </Summary>
  43. /// <Typeparam name = "T"> </typeparam>
  44. /// <Param name = "ds"> </param>
  45. /// <Returns> </returns>
  46. Public static T GetEntity <T> (this DataSet ds)
  47. {
  48. If (ds = null | ds. Tables. Count = 0 | ds. Tables [0]. Rows. Count = 0)
  49. Return default (T );
  50. Var dr = ds. Tables [0]. CreateDataReader ();
  51. Return Mapper. Map <T> (dr );
  52. }
  53. /// <Summary>
  54. /// Convert the DataTable object
  55. /// </Summary>
  56. /// <Typeparam name = "T"> </typeparam>
  57. /// <Param name = "dt"> </param>
  58. /// <Returns> </returns>
  59. Public static T GetEntity <T> (this DataTable dt)
  60. {
  61. If (dt = null | dt. Rows. Count = 0)
  62. Return default (T );
  63. Var dr = dt. CreateDataReader ();
  64. Return Mapper. Map <T> (dr );
  65. }
  66.  
  67. # Endregion
  68. }

Related Article

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.