Skip to content

AddressService

Derek Peacock edited this page Dec 21, 2022 · 2 revisions

Blazor Address Service

This service class will have CRUD methods for:-

  1. GetAllAddresses()
  2. GetAddress(int id)
  3. CreateAddress()
  4. EditAddress(int id)
  5. ViewAddress(int id)

These can be setyup as an Interface (IAddressService) which defines the methods for the AddressService to implement. This would be more flexible for future enhancements.

using BlazorShoes.Data;
using BlazorShoes.Models;

namespace BlazorShoes.Services
{
    public class AddressService
    {
        private readonly ApplicationDbContext _context;

        public AddressService(ApplicationDbContext context)
        {
            _context = context;
        }

        public List<Address> GetAddresses()
        {
            var addressList = _context.Addresses.ToList();
            return addressList;
        }
    }
}

Registering the Service

The Service must be registered in the program.cs class

builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();

// Add Services and API Controllers

builder.Services.AddScoped<BasketService>();
builder.Services.AddScoped<AddressService>();
builder.Services.AddScoped<AddressesController>();

Clone this wiki locally