■ CustomPropertyTypeMap 클래스에서 커스텀 컬럼 매핑을 사용하는 방법을 보여준다.
▶ EmployeeModel.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
namespace TestProject { /// <summary> /// 직원 모델 /// </summary> public class EmployeeModel { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID - ID /// <summary> /// ID /// </summary> public string ID { get; set; } #endregion #region 성명 - Name /// <summary> /// 성명 /// </summary> public string Name { get; set; } #endregion } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
using System; using System.Collections.Generic; using System.Reflection; using System.Linq; using Oracle.ManagedDataAccess.Client; using Dapper; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 직원 매핑 딕셔너리 /// </summary> private static Dictionary<string, string> _employeeMappingDictionary = new Dictionary<string, string> { { "EMPNO", "ID" }, { "ENAME", "Name" } }; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { CustomPropertyTypeMap employeeMap = new CustomPropertyTypeMap ( typeof(EmployeeModel), (type, columnName) => SelectEmployeeProperty(type, columnName) ); SqlMapper.SetTypeMap(typeof(EmployeeModel), employeeMap); string connectionString = @"Data Source=(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521)))(CONNECT_DATA = (SERVICE_NAME = orcl)));User ID=SYSTEM;Password=P@ssw0rd"; string sql = @" SELECT EMPNO ,ENAME FROM SCOTT.EMP "; using(OracleConnection connection = new OracleConnection(connectionString)) { var resultList = connection.Query<EmployeeModel>(sql).ToList(); foreach(var item in resultList) { Console.WriteLine($"{item.ID} {item.Name}"); } } } #endregion #region 직원 속성 선택하기 - SelectEmployeeProperty(type, columnName) /// <summary> /// 직원 속성 선택하기 /// </summary> /// <param name="type">타입</param> /// <param name="columnName">컬럼명</param> /// <returns>속성 정보</returns> private static PropertyInfo SelectEmployeeProperty(Type type, string columnName) { if(_employeeMappingDictionary.ContainsKey(columnName)) { return type.GetProperty(_employeeMappingDictionary[columnName]); } else { return type.GetProperty(columnName); } } #endregion } } |