[C#/ASP.NET MVC] DAPPER를 사용해 ASP.NET MVC 애플리케이션 만들기
■ DAPPER를 사용해 ASP.NET MVC 애플리케이션을 만드는 방법을 보여준다. ▶ TestDB.sql
1 2 3 4 5 6 7 8 9 10 |
CREATE TABLE dbo.Task ( ID INT NOT NULL PRIMARY KEY IDENTITY(1, 1) -- ID ,Title NVARCHAR(140) NOT NULL -- 제목 ,IsCompleted BIT -- 완료 여부 ,CreateDate DATETIME DEFAULT(GETDATE()) -- 생성일 ) GO |
▶ TaskModel.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 |
using System; namespace TestProject { /// <summary> /// 작업 모델 /// </summary> public class TaskModel { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID - ID /// <summary> /// ID /// </summary> public int ID { get; set; } #endregion #region 제목 - Title /// <summary> /// 제목 /// </summary> public string Title { get; set; } #endregion #region 완료 여부 - IsCompleted /// <summary> /// 완료 여부 /// </summary> public bool IsCompleted { get; set; } #endregion #region 생성일 - CreateDate /// <summary> /// 생성일 /// </summary> public DateTime CreateDate { get; set; } #endregion } } |
▶ TaskRepository.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 |
using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using Dapper; namespace TestProject { /// <summary> /// 작업 저장소 /// </summary> public class TaskRepository { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// DB 연결 /// </summary> private IDbConnection connection = new SqlConnection ( ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region 추가하기 - Add(task) /// <summary> /// 추가하기 /// </summary> /// <param name="task">작업</param> public void Add(TaskModel task) { string sql = @" INSERT INTO dbo.Task ( Title ,IsCompleted ) VALUES ( @Title ,CAST('false' AS BIT) ) "; this.connection.Execute(sql, task); } #endregion #region 리스트 구하기 - GetList() /// <summary> /// 리스트 구하기 /// </summary> /// <returns>리스트</returns> public List<TaskModel> GetList() { return this.connection.Query<TaskModel>("SELECT * FROM dbo.Task ORDER BY ID DESC").ToList(); } #endregion #region 작업 완료하기 - CompleteTask(id) /// <summary> /// 작업 완료하기 /// </summary> /// <param name="id">ID</param> public void CompleteTask(int id) { this.connection.Execute("UPDATE dbo.Task SET IsCompleted = ~IsCompleted WHERE ID = @ID", new { id }); } #endregion } } |
▶ TaskController.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 |
using System.Web.Mvc; namespace TestProject.Controllers { /// <summary> /// 작업 컨트롤러 /// </summary> public class TaskController : Controller { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 태스크 저장소 /// </summary> private TaskRepository repository = new TaskRepository(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 인덱스 페이지 처리하기 - Index() /// <summary> /// 인덱스 페이지 처리하기 /// </summary> /// <returns>액션 결과</returns> public ActionResult Index() { var resultList = this.repository.GetList(); return View(resultList); } #endregion #region 생성 페이지 처리하기 - Create() /// <summary> /// 생성 페이지 처리하기 /// </summary> /// <returns>액션 결과</returns> public ActionResult Create() { return View(); } #endregion #region 생성 처리 페이지 처리하기 - ProcessCreate(title) /// <summary> /// 생성 처리 페이지 처리하기 /// </summary> /// <param name="title">제목</param> /// <returns>액션 결과</returns> [HttpPost] public ActionResult ProcessCreate(string title) { this.repository.Add(new TaskModel { Title = title }); return RedirectToAction("Index"); } #endregion #region 작업 완료 페이지 처리하기 - CompleteTask(id) /// <summary> /// 작업 완료 페이지 처리하기 /// </summary> /// <param name="id">ID</param> /// <returns>액션 결과</returns> public ActionResult CompleteTask(int id) { this.repository.CompleteTask(id); return RedirectToAction("Index"); } #endregion } } |
▶