첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
2.78MB

▶ 테이블 생성 스크립트

DROP TABLE SCOTT."USER";

CREATE TABLE SCOTT."USER"
(
    "ID"   VARCHAR(20) NOT NULL,
    "NAME" VARCHAR(20),
    "ROLE" VARCHAR(50),
    PRIMARY KEY (ID)
);

INSERT INTO SCOTT."USER" VALUES ('001', '홍길동', 'User'               );
INSERT INTO SCOTT."USER" VALUES ('002', '김철수', 'User, Administrator');

COMMIT;

 

728x90

 

▶ Role.cs

using System;

namespace TestProject
{
    /// <summary>
    /// 역할
    /// </summary>
    [Flags]
    public enum Role
    {
        /// <summary>
        /// 해당 무
        /// </summary>
        None = 0,

        /// <summary>
        /// 관리자
        /// </summary>
        Administrator = 0x1,

        /// <summary>
        /// 사용자
        /// </summary>
        User = 0x2
    }
}

 

300x250

 

▶ UserModel.cs

using System;

namespace TestProject
{
    /// <summary>
    /// 사용자 모델
    /// </summary>
    public class UserModel
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region ID - ID

        /// <summary>
        /// ID
        /// </summary>
        public string ID { get; set; }

        #endregion
        #region 성명 - NAME

        /// <summary>
        /// 성명
        /// </summary>
        public string NAME { get; set; }

        #endregion
        #region 역할 - ROLE

        /// <summary>
        /// 역할
        /// </summary>
        public Role ROLE { get; set; }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 역할 구하기 - GetRole(value)

        /// <summary>
        /// 역할 구하기
        /// </summary>
        /// <param name="value">값</param>
        /// <returns>역할</returns>
        public static Role GetRole(string value)
        {
            if(string.IsNullOrWhiteSpace(value))
            {
                return Role.None;
            }

            string[] valueItemArray = value.Split(',');

            Role role = Role.None;

            int valueItemArrayLength = valueItemArray.Length;

            for(int i = 0; i < valueItemArrayLength; i++)
            {
                string valueItem = valueItemArray[i];

                if(i == 0)
                {
                    role = (Role)Enum.Parse(typeof(Role), valueItem);
                }
                else
                {
                    role |= (Role)Enum.Parse(typeof(Role), valueItem);
                }
            }

            return role;
        }

        #endregion
    }
}

 

▶ RoleTypeHandler.cs

using System.Data;

using Dapper;

namespace TestProject
{
    /// <summary>
    /// 역할 타입 핸들러
    /// </summary>
    public class RoleTypeHandler : SqlMapper.TypeHandler<Role>
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 파싱하기 - Parse(value)

        /// <summary>
        /// 파싱하기
        /// </summary>
        /// <param name="value">값</param>
        /// <returns>역할</returns>
        public override Role Parse(object value)
        {
            return UserModel.GetRole(value.ToString());
        }

        #endregion
        #region 값 설정하기 - SetValue(parameter, value)

        /// <summary>
        /// 값 설정하기
        /// </summary>
        /// <param name="parameter">매개 변수</param>
        /// <param name="value">값</param>
        public override void SetValue(IDbDataParameter parameter, Role value)
        {
            parameter.Value = value.ToString();
        }

        #endregion
    }
}

 

▶ Program.cs

using System;
using System.Linq;

using Oracle.ManagedDataAccess.Client;

using Dapper;

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            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";

            SqlMapper.AddTypeHandler(new RoleTypeHandler());

            using(OracleConnection connection = new OracleConnection(connectionString))
            {
                var resultList = connection.Query<UserModel>("SELECT * FROM SCOTT.\"USER\"").ToList();

                foreach(var item in resultList)
                {
                    Console.WriteLine(item.ROLE);
                }
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요