첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ Models/DataModel.cs

namespace TestProject.Models
{
    /// <summary>
    /// 데이터 모델
    /// </summary>
    public class DataModel
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region ID - ID

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

        #endregion
        #region 성명 - Name

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

        #endregion
        #region 직함 - Title

        /// <summary>
        /// 직함
        /// </summary>
        public string Title { get; set; }

        #endregion
    }
}

 

728x90

 

▶ Models/DataRepository.cs

using System.Collections.Generic;
using System.Linq;

namespace TestProject.Models
{
    /// <summary>
    /// 데이터 저장소
    /// </summary>
    public class DataRepository
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 소스 리스트
        /// </summary>
        private readonly List<DataModel> sourceList = new List<DataModel>()
        {
            new DataModel { ID = 1, Name = "김철수", Title = "차장" },
            new DataModel { ID = 2, Name = "이영희", Title = "과장" },
            new DataModel { ID = 3, Name = "홍길동", Title = "대리" }
        };

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 리스트 구하기 - GetList()

        /// <summary>
        /// 리스트 구하기
        /// </summary>
        /// <returns>리스트</returns>
        public List<DataModel> GetList()
        {
            return this.sourceList;
        }

        #endregion
        #region 리스트 구하기 - GetList(name)

        /// <summary>
        /// 리스트 구하기
        /// </summary>
        /// <param name="name">성명</param>
        /// <returns>리스트</returns>
        public List<DataModel> GetList(string name)
        {
            return this.sourceList.Where(n => n.Name.ToLower().Equals(name.ToLower())).ToList();
        }

        #endregion
        #region 데이터 구하기 - GetData(id)

        /// <summary>
        /// 데이터 구하기
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>데이터</returns>
        public DataModel GetData(int id)
        {
            return this.sourceList.Where(n => n.ID == id).SingleOrDefault();
        }

        #endregion
    }
}

 

300x250

 

▶ Models/DataFinder.cs

using System.Threading.Tasks;

namespace TestProject.Models
{
    /// <summary>
    /// 데이터 파인더
    /// </summary>
    public class DataFinder
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 데이터 저장소
        /// </summary>
        private DataRepository repository = new DataRepository();

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 데이터 구하기 - GetData(id)

        /// <summary>
        /// 데이터 구하기
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>데이터</returns>
        public async Task<DataModel> GetData(int id)
        {
            return await Task.FromResult(repository.GetData(id));
        }

        #endregion
    }
}

 

▶ Controllers/DataController.cs

using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

using TestProject.Models;

namespace TestProject.Controllers
{
    /// <summary>
    /// 데이터 컨트롤러
    /// </summary>
    public class DataController : Controller
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 인덱스 페이지 처리하기 - Index()

        /// <summary>
        /// 인덱스 페이지 처리하기
        /// </summary>
        /// <returns>액션 결과</returns>
        public IActionResult Index()
        {
            DataRepository repository = new DataRepository();

            List<DataModel> list = repository.GetList();

            return View(list);
        }

        #endregion
    }
}

 

▶ Views/Data/Index.cshtml

@using TestProject.Models
@model IEnumerable<DataModel>
@inject DataFinder Finder
@{
    ViewData["Title"] = "Data Page";
}
<h3>데이터 목록 출력</h3>
<ul>
    @foreach (DataModel data in Model)
    {
        <li>@data.ID : @data.Name, @data.Title</li>
    }
</ul>
<hr />
<h3>단일 데이터 출력</h3>
@{
    DataModel firstData = await Finder.GetData(1);
}
<div>
    1번 데이터 : @firstData.ID, @firstData.Name, @firstData.Title
</div>

 

▶ Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace TestProject
{
    /// <summary>
    /// 시작
    /// </summary>
    public class Startup
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 구성 - Configuration

        /// <summary>
        /// 구성
        /// </summary>
        public IConfiguration Configuration { get; }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - Startup(configuration)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="configuration">구성</param>
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 서비스 컬렉션 구성하기 - ConfigureServices(serviceCollection)

        /// <summary>
        /// 서비스 컬렉션 구성하기
        /// </summary>
        /// <param name="serviceCollection">서비스 컬렉션</param>
        public void ConfigureServices(IServiceCollection serviceCollection)
        {
            serviceCollection.AddControllersWithViews();

            serviceCollection.AddTransient<TestProject.Models.DataFinder>();
        }

        #endregion
        #region 구성하기 - Configure(builder, environment)

        /// <summary>
        /// 구성하기
        /// </summary>
        /// <param name="builder">애플리케이션 빌더</param>
        /// <param name="environment">웹 호스트 환경</param>
        public void Configure(IApplicationBuilder builder, IWebHostEnvironment environment)
        {
            if(environment.IsDevelopment())
            {
                builder.UseDeveloperExceptionPage();
            }
            else
            {
                builder.UseExceptionHandler("/Home/Error");

                builder.UseHsts();
            }

            builder.UseHttpsRedirection();

            builder.UseStaticFiles();

            builder.UseRouting();

            builder.UseAuthorization();

            builder.UseEndpoints
            (
                endpoints =>
                {
                    endpoints.MapControllerRoute
                    (
                        name    : "default",
                        pattern : "{controller=Home}/{action=Index}/{id?}"
                    );
                }
            );
        }

        #endregion
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요