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