Skip to content

Commit 59c91ca

Browse files
committed
Adding & Using Best Practices
1 parent cff5c46 commit 59c91ca

File tree

10 files changed

+118
-33
lines changed

10 files changed

+118
-33
lines changed

BlazorEcommerce/Client/Program.cs

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
global using BlazorEcommerce.Shared;
2+
global using System.Net.Http.Json;
3+
global using BlazorEcommerce.Client.Services.ProductService;
14
using BlazorEcommerce.Client;
25
using Microsoft.AspNetCore.Components.Web;
36
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
@@ -7,5 +10,6 @@
710
builder.RootComponents.Add<HeadOutlet>("head::after");
811

912
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
13+
builder.Services.AddScoped<IProductService, ProductService>();
1014

1115
await builder.Build().RunAsync();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace BlazorEcommerce.Client.Services.ProductService
2+
{
3+
public interface IProductService
4+
{
5+
List<Product> Products { get; set; }
6+
Task GetProducts();
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace BlazorEcommerce.Client.Services.ProductService
2+
{
3+
public class ProductService : IProductService
4+
{
5+
private readonly HttpClient _http;
6+
7+
public ProductService(HttpClient http)
8+
{
9+
_http = http;
10+
}
11+
12+
public List<Product> Products { get; set; } = new List<Product>();
13+
14+
public async Task GetProducts()
15+
{
16+
var result =
17+
await _http.GetFromJsonAsync<ServiceResponse<List<Product>>>("api/product");
18+
if (result != null && result.Data != null)
19+
Products = result.Data;
20+
}
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
1-
@inject HttpClient Http
1+
@inject IProductService ProductService
22

3-
<ul class="list-unstyled">
4-
@foreach (var product in Products)
5-
{
6-
<li class="media my-3">
7-
<div class="media-img-wrapper mr-2">
8-
<a href="#">
9-
<img class="media-img" src="@product.ImageUrl" alt="@product.Title" />
10-
</a>
11-
</div>
12-
<div class="media-body">
13-
<a href="#">
14-
<h4 class="mb-0">@product.Title</h4>
15-
</a>
16-
<p>@product.Description</p>
17-
<h5 class="price">
18-
$@product.Price
19-
</h5>
20-
</div>
21-
</li>
22-
}
23-
</ul>
3+
@if (ProductService.Products == null || ProductService.Products.Count == 0)
4+
{
5+
<span>Loading Products...</span>
6+
}
7+
else
8+
{
9+
<ul class="list-unstyled">
10+
@foreach (var product in ProductService.Products)
11+
{
12+
<li class="media my-3">
13+
<div class="media-img-wrapper mr-2">
14+
<a href="#">
15+
<img class="media-img" src="@product.ImageUrl" alt="@product.Title" />
16+
</a>
17+
</div>
18+
<div class="media-body">
19+
<a href="#">
20+
<h4 class="mb-0">@product.Title</h4>
21+
</a>
22+
<p>@product.Description</p>
23+
<h5 class="price">
24+
$@product.Price
25+
</h5>
26+
</div>
27+
</li>
28+
}
29+
</ul>
30+
}
2431

2532
@code {
26-
private static List<Product> Products = new List<Product>();
27-
2833
protected override async Task OnInitializedAsync()
2934
{
30-
var result = await Http.GetFromJsonAsync<List<Product>>("api/Product");
31-
if (result != null)
32-
Products = result;
35+
await ProductService.GetProducts();
3336
}
3437
}

BlazorEcommerce/Client/_Imports.razor

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
@using BlazorEcommerce.Client
1010
@using BlazorEcommerce.Client.Shared
1111
@using BlazorEcommerce.Shared
12+
@using BlazorEcommerce.Client.Services.ProductService

BlazorEcommerce/Server/Controllers/ProductController.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ namespace BlazorEcommerce.Server.Controllers
77
[ApiController]
88
public class ProductController : ControllerBase
99
{
10-
private readonly DataContext _context;
10+
private readonly IProductService _productService;
1111

12-
public ProductController(DataContext context)
12+
public ProductController(IProductService productService)
1313
{
14-
_context = context;
14+
_productService = productService;
1515
}
1616

1717
[HttpGet]
18-
public async Task<ActionResult<List<Product>>> GetProduct()
18+
public async Task<ActionResult<ServiceResponse<List<Product>>>> GetProducts()
1919
{
20-
var products = await _context.Products.ToListAsync();
21-
return Ok(products);
20+
var result = await _productService.GetProductsAsync();
21+
return Ok(result);
2222
}
2323
}
2424
}

BlazorEcommerce/Server/Program.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
global using BlazorEcommerce.Shared;
22
global using Microsoft.EntityFrameworkCore;
33
global using BlazorEcommerce.Server.Data;
4+
global using BlazorEcommerce.Server.Services.ProductService;
45
using Microsoft.AspNetCore.ResponseCompression;
56

67
var builder = WebApplication.CreateBuilder(args);
@@ -17,6 +18,8 @@
1718
builder.Services.AddEndpointsApiExplorer();
1819
builder.Services.AddSwaggerGen();
1920

21+
builder.Services.AddScoped<IProductService, ProductService>();
22+
2023
var app = builder.Build();
2124

2225
app.UseSwaggerUI();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace BlazorEcommerce.Server.Services.ProductService
2+
{
3+
public interface IProductService
4+
{
5+
Task<ServiceResponse<List<Product>>> GetProductsAsync();
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace BlazorEcommerce.Server.Services.ProductService
2+
{
3+
public class ProductService : IProductService
4+
{
5+
private readonly DataContext _context;
6+
7+
public ProductService(DataContext context)
8+
{
9+
_context = context;
10+
}
11+
12+
public async Task<ServiceResponse<List<Product>>> GetProductsAsync()
13+
{
14+
var response = new ServiceResponse<List<Product>>
15+
{
16+
Data = await _context.Products.ToListAsync()
17+
};
18+
19+
return response;
20+
}
21+
}
22+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace BlazorEcommerce.Shared
8+
{
9+
public class ServiceResponse<T>
10+
{
11+
public T? Data { get; set; }
12+
public bool Success { get; set; } = true;
13+
public string Message { get; set; } = string.Empty;
14+
}
15+
}

0 commit comments

Comments
 (0)