@ -0,0 +1,16 @@
namespace BlazorApp.Data
{
public class FastFoodService : IFoodService
public IEnumerable<Food> GetFoods()
List<Food> foods = new List<Food>()
new Food() { Name = "감자튀김", Price = 1500 },
new Food() { Name = "햄버거", Price = 6000 },
};
return foods;
}
@ -0,0 +1,8 @@
public class Food
public string Name { get; set; }
public int Price { get; set; }
@ -0,0 +1,17 @@
public class FoodService : IFoodService
new Food() { Name = "비빔밥", Price = 9000 },
new Food() { Name = "김밥", Price = 4000 },
new Food() { Name = "쫄면", Price = 7000 },
@ -0,0 +1,7 @@
public interface IFoodService
public IEnumerable<Food> GetFoods();
@ -0,0 +1,12 @@
public class PaymentService
private IFoodService _service;
public PaymentService(IFoodService service)
_service = service;
@ -0,0 +1,47 @@
public class SingletonService : IDisposable
public Guid ID { get; set; }
public SingletonService()
ID = Guid.NewGuid();
public void Dispose()
Console.WriteLine($"SingletonService was disposed (Guid: {this.ID.ToString()})");
public class TransientService : IDisposable
public TransientService()
Console.WriteLine($"TransientService was disposed (Guid: {this.ID.ToString()})");
public class ScopedService : IDisposable
public ScopedService()
Console.WriteLine($"ScopedService was disposed (Guid: {this.ID.ToString()})");
@ -4,8 +4,8 @@ namespace BlazorApp.Data
private static readonly string[] Summaries = new[]
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
public Task<WeatherForecast[]> GetForecastAsync(DateTime startDate)
@ -0,0 +1,32 @@
@page "/food"
@using BlazorApp.Data;
@inject IFoodService _foodService;
@inject PaymentService _paymentService;
@inject SingletonService _singleton;
@inject ScopedService _scoped;
@inject TransientService _transient;
<h3>Food</h3>
<div>
@foreach (var food in _foodService.GetFoods())
<div>@food.Name</div>
<div>@food.Price</div>
</div>
<h1>Singleton</h1>
<label>Guid: @_singleton.ID</label>
<h1>Scoped</h1>
<label>Guid: @_scoped.ID</label>
<h1>Transient</h1>
<label>Guid: @_transient.ID</label>
@code {
@ -15,6 +15,15 @@ namespace BlazorApp
builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<WeatherForecastService>();
// Dependency Injection
builder.Services.AddSingleton<IFoodService, FastFoodService>();
builder.Services.AddSingleton<PaymentService>();
// 3가지 모드
builder.Services.AddSingleton<SingletonService>(); // 서버 시작시
builder.Services.AddScoped<ScopedService>(); // 접속시
builder.Services.AddTransient<TransientService>(); // 요청시
var app = builder.Build();
// Configure the HTTP request pipeline.
@ -34,6 +34,11 @@
<span class="oi oi-flag" aria-hidden="true"></span> User
</NavLink>
<div class="nav-item px-3">
<NavLink class="nav-link" href="food">
<span class="oi oi-basket" aria-hidden="true"></span> Food
</nav>