dependency injection

main
syneffort 2 years ago
parent 32c3637f4e
commit e5b4984515
  1. 16
      BlazorApp/BlazorApp/Data/FastFoodService.cs
  2. 8
      BlazorApp/BlazorApp/Data/Food.cs
  3. 17
      BlazorApp/BlazorApp/Data/FoodService.cs
  4. 7
      BlazorApp/BlazorApp/Data/IFoodService.cs
  5. 12
      BlazorApp/BlazorApp/Data/PaymentService.cs
  6. 47
      BlazorApp/BlazorApp/Data/SingletonService.cs
  7. 4
      BlazorApp/BlazorApp/Data/WeatherForecastService.cs
  8. 32
      BlazorApp/BlazorApp/Pages/Food.razor
  9. 9
      BlazorApp/BlazorApp/Program.cs
  10. 5
      BlazorApp/BlazorApp/Shared/NavMenu.razor

@ -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 @@
namespace BlazorApp.Data
{
public class Food
{
public string Name { get; set; }
public int Price { get; set; }
}
}

@ -0,0 +1,17 @@
namespace BlazorApp.Data
{
public class FoodService : IFoodService
{
public IEnumerable<Food> GetFoods()
{
List<Food> foods = new List<Food>()
{
new Food() { Name = "비빔밥", Price = 9000 },
new Food() { Name = "김밥", Price = 4000 },
new Food() { Name = "쫄면", Price = 7000 },
};
return foods;
}
}
}

@ -0,0 +1,7 @@
namespace BlazorApp.Data
{
public interface IFoodService
{
public IEnumerable<Food> GetFoods();
}
}

@ -0,0 +1,12 @@
namespace BlazorApp.Data
{
public class PaymentService
{
private IFoodService _service;
public PaymentService(IFoodService service)
{
_service = service;
}
}
}

@ -0,0 +1,47 @@
namespace BlazorApp.Data
{
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 Guid ID { get; set; }
public TransientService()
{
ID = Guid.NewGuid();
}
public void Dispose()
{
Console.WriteLine($"TransientService was disposed (Guid: {this.ID.ToString()})");
}
}
public class ScopedService : IDisposable
{
public Guid ID { get; set; }
public ScopedService()
{
ID = Guid.NewGuid();
}
public void Dispose()
{
Console.WriteLine($"ScopedService was disposed (Guid: {this.ID.ToString()})");
}
}
}

@ -4,8 +4,8 @@ namespace BlazorApp.Data
{ {
private static readonly string[] Summaries = new[] private static readonly string[] Summaries = new[]
{ {
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
}; };
public Task<WeatherForecast[]> GetForecastAsync(DateTime startDate) 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>
<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>
</div>
@code {
}

@ -15,6 +15,15 @@ namespace BlazorApp
builder.Services.AddServerSideBlazor(); builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<WeatherForecastService>(); 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(); var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.

@ -34,6 +34,11 @@
<span class="oi oi-flag" aria-hidden="true"></span> User <span class="oi oi-flag" aria-hidden="true"></span> User
</NavLink> </NavLink>
</div> </div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="food">
<span class="oi oi-basket" aria-hidden="true"></span> Food
</NavLink>
</div>
</nav> </nav>
</div> </div>

Loading…
Cancel
Save