DI Setup using HostApplicationBuilder
This commit is contained in:
@@ -1,29 +1,28 @@
|
||||
#if POSTGRES
|
||||
#if POSTGRES
|
||||
using EF_Demo.Data.Postgres;
|
||||
#else
|
||||
using EF_Demo.Data.MSSQL;
|
||||
#endif
|
||||
|
||||
using EF_Demo.Models;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace EF_Demo;
|
||||
|
||||
internal class Demo
|
||||
internal class Demo(IDbContextFactory<EventsContext> contextFactory, ILogger<Demo> logger)
|
||||
{
|
||||
internal static int? AddEvent(IServiceProvider serviceProvider, string eventName, DateOnly eventDate)
|
||||
internal int? AddEvent(string eventName, DateOnly eventDate)
|
||||
{
|
||||
ILogger logger = serviceProvider.GetRequiredService<ILogger<Demo>>();
|
||||
try
|
||||
{
|
||||
using var ctx = serviceProvider.GetRequiredService<EventsContext>();
|
||||
using var ctx = contextFactory.CreateDbContext();
|
||||
Event @event = new()
|
||||
{
|
||||
Name = eventName,
|
||||
EventDate = eventDate
|
||||
};
|
||||
ctx.Add(@event); //or ctx.Events.Add(@event) or ctx.Set<Event>().Add(@event);
|
||||
ctx.Add(@event);
|
||||
ctx.SaveChanges();
|
||||
logger.LogInformation($"Event {eventName} successfully added with id {@event.Id}");
|
||||
return @event.Id;
|
||||
@@ -35,16 +34,15 @@ internal class Demo
|
||||
}
|
||||
}
|
||||
|
||||
internal static void DeleteEvent(IServiceProvider serviceProvider, int eventId)
|
||||
internal void DeleteEvent(int eventId)
|
||||
{
|
||||
ILogger logger = serviceProvider.GetRequiredService<ILogger<Demo>>();
|
||||
try
|
||||
{
|
||||
using var ctx = serviceProvider.GetRequiredService<EventsContext>();
|
||||
using var ctx = contextFactory.CreateDbContext();
|
||||
Event? @event = ctx.Events.Find(eventId);
|
||||
if (@event != null)
|
||||
{
|
||||
ctx.Remove(@event); //or ctx.Entry(product).State = EntityState.Deleted;
|
||||
ctx.Remove(@event);
|
||||
ctx.SaveChanges();
|
||||
logger.LogInformation($"Event {@event.Name} deleted");
|
||||
}
|
||||
@@ -59,12 +57,11 @@ internal class Demo
|
||||
}
|
||||
}
|
||||
|
||||
internal static void PostponeEvent(IServiceProvider serviceProvider, int eventId, int days)
|
||||
internal void PostponeEvent(int eventId, int days)
|
||||
{
|
||||
ILogger logger = serviceProvider.GetRequiredService<ILogger<Demo>>();
|
||||
try
|
||||
{
|
||||
using var ctx = serviceProvider.GetRequiredService<EventsContext>();
|
||||
using var ctx = contextFactory.CreateDbContext();
|
||||
Event? @event = ctx.Events.Find(eventId);
|
||||
if (@event != null)
|
||||
{
|
||||
@@ -74,12 +71,12 @@ internal class Demo
|
||||
}
|
||||
else
|
||||
{
|
||||
logger?.LogWarning($"Event #{eventId} does not exists");
|
||||
logger.LogWarning($"Event #{eventId} does not exists");
|
||||
}
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
logger?.LogError($"Error trying to change evenbt #{eventId}: {exc.CompleteExceptionMessage()}");
|
||||
logger.LogError($"Error trying to change evenbt #{eventId}: {exc.CompleteExceptionMessage()}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,12 +84,12 @@ internal class Demo
|
||||
/// Print all people born in the provided year, ordered by country, and then by transliterated last name, and first name
|
||||
/// query is projected to an anonymous class
|
||||
/// </summary>
|
||||
internal static void PrintPeople(IServiceProvider serviceProvider, int year)
|
||||
internal void PrintPeople(int year)
|
||||
{
|
||||
using var ctx = serviceProvider.GetRequiredService<EventsContext>();
|
||||
using var ctx = contextFactory.CreateDbContext();
|
||||
var query = ctx.People
|
||||
.Where(p => p.BirthDate.Year == year)
|
||||
.OrderBy(p => p.CountryCodeNavigation.Name)
|
||||
.OrderBy(p => p.CountryCodeNavigation.Name)
|
||||
.ThenBy(p => p.LastNameTranscription)
|
||||
.ThenBy(p => p.FirstNameTranscription)
|
||||
.Select(p => new
|
||||
|
||||
Reference in New Issue
Block a user