Mapping IDataReader, datasets, DataTable to entity classes using AutoMapper

Source: Internet
Author: User

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
    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

[CSharp]View Plaincopy
  1. Using AutoMapper;
  2. Using System.Data;
  3. Namespace Utitity.automapper
  4. {
  5. // <summary>
  6. /// Entity Mapping Helper class
  7. // </summary>
  8. public static class Mapperhelper
  9. {
  10. #region Configure mapping rules
  11. // <summary>
  12. /// Ensure that the mapping configuration is registered only once
  13. // </summary>
  14. static mapperhelper ()
  15. {
  16. Configure ();
  17. }
  18. // <summary>
  19. /// registration Mapper Conversion Rule Conventions
  20. // </summary>
  21. static void Configure ()
  22. {
  23. Mapper.createmap<idatareader, xxxxmodel> (); //Only need to agree on the basic type, do not write list<xxxxmodel> this form
  24. }
  25. #endregion
  26. #region Entity Mapping Extension methods
  27. // <summary>
  28. /// convert IDataReader to entity object
  29. // </summary>
  30. /// <typeparam name= "T" ></typeparam>
  31. /// <param name= "Dr" ></param>
  32. // <returns></returns>
  33. public Static T-getentity<t> (thisIDataReader dr)
  34. {
  35. return mapper.map<t> (DR);
  36. }
  37. // <summary>
  38. /// convert DataSet to entity object
  39. // </summary>
  40. /// <typeparam name= "T" ></typeparam>
  41. /// <param name= "ds" ></param>
  42. // <returns></returns>
  43. public Static T-getentity<t> (thisDataSet ds)
  44. {
  45. if (ds = = Null | | ds. Tables.count = = 0 | | Ds. Tables[0]. Rows.Count = = 0)
  46. return default (T);
  47. var dr = ds. Tables[0]. CreateDataReader ();
  48. return mapper.map<t> (DR);
  49. }
  50. // <summary>
  51. /// convert DataTable to entity object
  52. // </summary>
  53. /// <typeparam name= "T" ></typeparam>
  54. /// <param name= "DT" ></param>
  55. // <returns></returns>
  56. public Static T-getentity<t> (thisDataTable dt)
  57. {
  58. if (dt = = Null | | dt. Rows.Count = = 0)
  59. return default (T);
  60. var dr = dt. CreateDataReader ();
  61. return mapper.map<t> (DR);
  62. }
  63. #endregion
  64. }

Mapping IDataReader, datasets, DataTable to entity classes using AutoMapper

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.