■ ConfigurationProvider 클래스에서 커스텀 구성 공급자를 사용하는 방법을 보여준다. (SQL SERVER 사용)
▶ Models/Configuration.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.Models { /// <summary> /// 구성 /// </summary> public class Configuration { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID - ID /// <summary> /// ID /// </summary> public string ID { get; set; } #endregion #region 값 - Value /// <summary> /// 값 /// </summary> public string Value { get; set; } #endregion } } |
▶ Data/DatabaseContext.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 |
using Microsoft.EntityFrameworkCore; using TestProject.Models; namespace TestProject.Data { /// <summary> /// 데이터베이스 /// </summary> public class DatabaseContext : DbContext { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 구성 - Configuration /// <summary> /// 구성 /// </summary> public DbSet<Configuration> Configuration { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - DatabaseContext(options) /// <summary> /// 생성자 /// </summary> /// <param name="options">옵션</param> public DatabaseContext(DbContextOptions options) : base(options) { } #endregion } } |
▶ CustomConfigurationProvider.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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.Linq; using TestProject.Data; using TestProject.Models; namespace TestProject { /// <summary> /// 커스텀 구성 공급자 /// </summary> public class CustomConfigurationProvider : ConfigurationProvider { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 옵션 액션 - OptionsAction /// <summary> /// 옵션 액션 /// </summary> Action<DbContextOptionsBuilder> OptionsAction { get; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - CustomConfigurationProvider(optionsAction) /// <summary> /// 생성자 /// </summary> /// <param name="optionsAction"></param> public CustomConfigurationProvider(Action<DbContextOptionsBuilder> optionsAction) { OptionsAction = optionsAction; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 로드하기 - Load() /// <summary> /// 로드하기 /// </summary> public override void Load() { DbContextOptionsBuilder<DatabaseContext> builder = new DbContextOptionsBuilder<DatabaseContext>(); OptionsAction(builder); using(DatabaseContext context = new DatabaseContext(builder.Options)) { context.Database.EnsureCreated(); if(!context.Configuration.Any()) { Data = CreateData(context); } else { Data = context.Configuration.ToDictionary ( configuration => configuration.ID, configuration => configuration.Value ); } } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 데이터 생성하기 - CreateData(context) /// <summary> /// 데이터 생성하기 /// </summary> /// <param name="context">데이터베이스 컨텍스트</param> /// <returns>데이터</returns> private static IDictionary<string, string> CreateData(DatabaseContext context) { Dictionary<string, string> sourceDictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) { { "Key1", "Value1" }, { "Key2", "Value2" }, { "Key3", "Value3" } }; context.Configuration.AddRange ( sourceDictionary.Select ( keyValuePair => new Configuration { ID = keyValuePair.Key, Value = keyValuePair.Value } ) .ToArray() ); context.SaveChanges(); return sourceDictionary; } #endregion } } |
▶ CustomConfigurationSource.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 |
using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using System; namespace TestProject { /// <summary> /// 커스텀 구성 소스 /// </summary> public class CustomConfigurationSource : IConfigurationSource { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 옵션 액션 /// </summary> private readonly Action<DbContextOptionsBuilder> optionsAction; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - CustomConfigurationSource(optionsAction) /// <summary> /// 생성자 /// </summary> /// <param name="optionsAction">옵션 액션</param> public CustomConfigurationSource(Action<DbContextOptionsBuilder> optionsAction) { this.optionsAction = optionsAction; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 빌드하기 - Build(builder) /// <summary> /// 빌드하기 /// </summary> /// <param name="builder">빌더</param> /// <returns>구성 공급자</returns> public IConfigurationProvider Build(IConfigurationBuilder builder) { return new CustomConfigurationProvider(this.optionsAction); } #endregion } } |
▶ CustomExtension.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 |
using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using System; namespace TestProject { /// <summary> /// 커스텀 확장 /// </summary> public static class CustomExtension { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region EF 구성 추가하기 - AddEFConfiguration(builder, optionsAction) /// <summary> /// EF 구성 추가하기 /// </summary> /// <param name="builder">빌더</param> /// <param name="optionsAction">옵션 액션</param> /// <returns>구성 빌더</returns> public static IConfigurationBuilder AddEFConfiguration ( this IConfigurationBuilder builder, Action<DbContextOptionsBuilder> optionsAction ) { return builder.Add(new CustomConfigurationSource(optionsAction)); } #endregion } } |
▶ appsettings.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
{ "Logging" : { "LogLevel" : { "Default" : "Information", "Microsoft" : "Warning", "Microsoft.Hosting.Lifetime" : "Information" } }, "AllowedHosts" : "*", "ConnectionStrings" : { "DefaultConnection" : "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=TestDB;Integrated Security=True;" } } |
▶ 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 |
using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; namespace TestProject { /// <summary> /// 프로그램 /// </summary> public class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 프로그램 시작하기 - Main(argumentArray) /// <summary> /// 프로그램 시작하기 /// </summary> /// <param name="argumentArray">인자 배열</param> public static void Main(string[] argumentArray) { CreateHostBuilder(argumentArray).Build().Run(); } #endregion #region 호스트 빌더 생성하기 - CreateHostBuilder(argumentArray) /// <summary> /// 호스트 빌더 생성하기 /// </summary> /// <param name="argumentArray">인자 배열</param> /// <returns>호스트 빌더</returns> public static IHostBuilder CreateHostBuilder(string[] argumentArray) => Host.CreateDefaultBuilder(argumentArray) .ConfigureAppConfiguration ( (context, builder) => { var root = builder.AddJsonFile("appsettings.json", optional : true, reloadOnChange : true).Build(); string connectionString = root.GetConnectionString("DefaultConnection"); builder.AddEFConfiguration(options => options.UseSqlServer(connectionString)); } ) .ConfigureWebHostDefaults ( builder => { builder.UseStartup<Startup>(); } ); #endregion } } |
▶ Controllers/TestController.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 |
using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using System.Text; namespace TestProject.Controllers { /// <summary> /// 테스트 컨트롤러 /// </summary> public class TestController : Controller { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 구성 /// </summary> private IConfiguration configuration; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - TestController(configuration) /// <summary> /// 생성자 /// </summary> /// <param name="configuration">구성</param> public TestController(IConfiguration configuration) { this.configuration = configuration; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 인덱스 페이지 처리하기 - Index() /// <summary> /// 인덱스 페이지 처리하기 /// </summary> /// <returns>액션 결과</returns> public IActionResult Index() { string value1 = this.configuration["Key1"]; string value2 = this.configuration["Key2"]; string value3 = this.configuration["Key3"]; StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine($"Key1 : {value1}"); stringBuilder.AppendLine($"Key2 : {value2}"); stringBuilder.AppendLine($"Key3 : {value3}"); return Content(stringBuilder.ToString()); } #endregion } } |