Looking for Best Practices when starting out a new Blazor Web App (dbFactoryContext) and any others

I have gone through multiple iterations of what I tend to implement as my project ‘stubbing’ since MVC. This has evolved with each new flavor of the technology and I’m attempting to do the same here. I’ve noticed some collision issues since .net 8 came out and my implementation of DI.

I’ve noticed that the below has been causing my problems and from what I’ve read I should be using a DBFactory Context.

 public myService(AppDbContext iDbContext)
 {
     dbContext = iDbContext;
 }

This had me thinking that I should have another look at my entire stubbing and see if anyone notices anything glaringly obvious that is missing, wrong and just outright dumb.

Program.cs

//Connection string is stored in AppSettings
var connectionString = builder.Configuration.GetConnectionString("DBConnection");

//New implementation using AddDbContextFactory which read my connectionstring
builder.Services.AddDbContextFactory<TestContext>(options =>
                            options.UseSqlServer(connectionString));

//I like using a seperate file for all my Service Registrations
builder.Services.AddHttpServices();

ServiceExtensions

public static class ServiceExtensions
{
    public static void AddHttpServices(this IServiceCollection services)
    {
        services.AddScoped<CountryService>();
    }
}

CountryService

namespace TestingDBFactory.Services
{
    public interface ICountryService
    {
        Task<IEnumerable<Country>> Lookup();
    }

    public class CountryService : ICountryService
    {
        private readonly IDbContextFactory<TestContext> _contextFactory;

        public CountryService(IDbContextFactory<TestContext> contextFactory)
        {
            _contextFactory = contextFactory;
        }
        public async Task<IEnumerable<Country>> Lookup()
        {
            using var context = _contextFactory.CreateDbContext();

            return await context.Country
                .AsNoTracking()
                .ToListAsync();
        }
    }
}

I've read that a short lifespan for the Context is what's now needed to avoid collisions (the infamous 'A second operation was started on this context instance before a previous operation completed' issue). The MS documentation says I should use Dispose but I always understood a Using implemented Dispose?

Country (Page)

@page "/"
@inject CountryService countryService
@using TestingDBFactory.Services
@using TestingDBFactory.Models

@if (_countries is not null)
{
    foreach (var c in _countries)
    {
        @c.Name<br/>
    }
}

@code {
    #nullable disable

    IEnumerable<Models.Country> _countries;

    protected override async Task OnInitializedAsync()
    {
        _countries = await countryService.Lookup();
    }
}

My question at this point is am I missing something? I understand some people may say DTOs and I will likely add those in my project but I’m wondering about anything else? I’m thinking logging but the package I used to use years ago was Open Sourced for MVC and when it wa smade avail for .NetCore I was a paid product (I forget the name). I’d rather have the logging as part of my DB than an ternal Azure dependancy.

Anyhow, any hints or pointing to existing projects is helpful as well. I’ve played with BlazorHero but it’s rather big and busy for what I need to do.

Thanx in advance

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật