SHARE
TWEET

Untitled

a guest Feb 17th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using Dapper;
  4. using Microsoft.Extensions.Configuration;
  5.  
  6. namespace Foo.Bar
  7. {
  8.     public static class DapperMappers
  9.     {
  10.         public static void Init(IConfiguration configuration)
  11.         {
  12.             var mappings = configuration.GetSection("ModelMappings");
  13.             foreach (var typeSection in mappings.GetChildren())
  14.             {
  15.                 var columnNames = typeSection.Get<IDictionary<string, string>>();
  16.                 var mapType = Type.GetType(typeSection.Key);
  17.  
  18.                 if (mapType == null)
  19.                     continue;
  20.  
  21.                 SqlMapper.SetTypeMap(mapType, new CustomPropertyTypeMap(
  22.                     mapType,
  23.                     (type, columnName) =>
  24.                         type.GetProperty(columnNames.ContainsKey(columnName) ? columnNames[columnName] : columnName)));
  25.             }
  26.         }
  27.     }
  28. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top