첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ CustomPropertyTypeMap 클래스 : 커스텀 컬럼 매핑 사용하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


EmployeeModel.cs

 

 

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

 

 

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

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요