Create Project Create Controller Add Swagger Add SeriLog Add MediatR Add EntityFramework Core Add Migration Apply Database 기타 초기화 Add Web Api Refactoring with Extension Method 9. 기타 초기화 Scrutor 추가 개발자 프롬프트에서 프로젝트 폴더로 이동 (현재 .sln 파일이 있는 폴더라면 .csproj 파일이 있는 폴더로 이동) 아래 명령 실행 dotnet add package Scrutor Injectables 폴더 추가 Injectable.cs 추가 namespace WebApiBasicTutorial.Injectables { public interface IInjectableService { } public interface ITransientService : IInjectableService { } public interface IScopedService : IInjectableService { } public interface ISingletonService : IInjectableService { } } Program.cs 에 다음 코드 추가 using WebApiBasicTutorial.Injectables; ... ... builder.Services.Scan(scan => scan .FromAssemblies(AssemblyHelper.GetAllAssemblies()) .AddClasses(classes => classes.AssignableTo<ITransientService>()) .AsImpleme...
작성 순서 Create Project Create Controller Add Swagger Add SeriLog Add MediatR Add EntityFramework Core Add Migration Apply Database Add Web Api Refactoring with Extension Method 1. Create Project Create 2. Create Controller Add NewFolder to project ( Controller ) UserController.cs using Microsoft.AspNetCore.Mvc; namespace WebApiBasicTutorial.Controller { [ApiController] [Route("[controller] ")] public class UserController :ControllerBase { } } Program.cs 에 다음 코드 추가 삭제 ==> app.MapGet( "/" , () => "Hello World!" ); 추가 ==> builder.Services.AddControllers(); 3. Add Swagger 개발자 프롬프트에서 프로젝트 폴더로 이동 (현재 .sln 파일이 있는 폴더라면 .csproj 파일이 있는 폴더로 이동) 아래 명령 실행 dotnet add package Swashbuckle.AspNetCore Program.cs 에 다음 코드 추가 builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); app.UseSwagger(); app.UseSwaggerUI(); Properties --> launchSettings.json --> "launchUrl": "swagger",...
서론 Dependency Injection 을 이용하여 의존성 문제를 해결 하였으나 그것만으로는 부족할 수 있다. library 간의 강결합을 해결 하기 위한 Event mechanism 이 필요하다. Observer, Event Bus, Event Aggregator 등 MediatR 이라는 Mediator 를 구현한 library 를 이용해 보자 설치 dotnet add package MediatR.Extensions.Microsoft.DependencyInjection 초기설정 Program.cs 로 이동 아래 추가 builder.Services.AddMediatR(AssemblyHelper.GetAllAssemblies().ToArray()); AssemblyHelper.cs 추가 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public class AssemblyHelper { public static List<Assembly> GetAllAssemblies(SearchOption searchOption = SearchOption.TopDirectoryOnly) { List<Assembly> assemblies = new List<Assembly>(); foreach ( string assemblyPath in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll" , searchOption)) { try { var assembly = System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromA...
댓글
댓글 쓰기