An attempt to send messages out of the scope

This commit is contained in:
TheXamlGuy
2024-05-12 20:21:55 +01:00
parent 837e249c64
commit 2842fc7108
11 changed files with 71 additions and 63 deletions
-11
View File
@@ -31,15 +31,4 @@ public class ContainerActivatedHandler(IContainerHostCollection containers,
}
}
}
}
public class ItemActivatedHandler(IServiceFactory serviceFactory,
IPublisher publisher) :
INotificationHandler<ActivatedEventArgs<ItemConfiguration>>
{
public async Task Handle(ActivatedEventArgs<ItemConfiguration> args,
CancellationToken cancellationToken = default)
{
}
}
+2 -2
View File
@@ -16,7 +16,7 @@ public class ContainerViewModelHandler(IDbContextFactory<ContainerDbContext> dbC
{
if (args.Options is ContainerViewModelConfiguration configuration)
{
ExpressionStarter<Data.Item> predicate = PredicateBuilder.New<Data.Item>(true);
ExpressionStarter<ItemEntry> predicate = PredicateBuilder.New<ItemEntry>(true);
if (configuration.Filter == "All")
{
@@ -36,7 +36,7 @@ public class ContainerViewModelHandler(IDbContextFactory<ContainerDbContext> dbC
var items = await Task.Run(async () =>
{
using ContainerDbContext context = dbContextFactory.CreateDbContext();
return await context.Set<Data.Item>().Where(predicate).Select(x => new
return await context.Set<ItemEntry>().Where(predicate).Select(x => new
{
x.Id,
x.Name
+10 -4
View File
@@ -1,5 +1,6 @@
using Bitvault.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Toolkit.Foundation;
namespace Bitvault;
@@ -14,16 +15,21 @@ public class CreateItemHandler(IDbContextFactory<ContainerDbContext> dbContextFa
{
try
{
using ContainerDbContext context = dbContextFactory.CreateDbContext();
EntityEntry<ItemEntry>? result = null;
await Task.Run(async () =>
{
using ContainerDbContext context = dbContextFactory.CreateDbContext();
await context.AddAsync(new Data.Item { Name = configuration.Name }, cancellationToken);
result = await context.AddAsync(new ItemEntry { Name = configuration.Name }, cancellationToken);
await context.SaveChangesAsync(cancellationToken);
}, cancellationToken);
await publisher.Publish(Activated.As(configuration));
return true;
if (result is not null)
{
await publisher.Publish(Activated.As(new Item { Id = result.Entity.Id }), cancellationToken);
return true;
}
}
catch
{
+1 -17
View File
@@ -1,23 +1,7 @@
namespace Bitvault;
public record Item<TValue>(TValue? Value = default);
public record Item
{
public Item(int id)
{
Id = id;
}
public Item()
{
}
public static Item<TValue> As<TValue>(TValue value) => new(value);
public static Item<TValue> As<TValue>() where TValue : new() => new(new TValue());
public int Id { get; }
public int Id { get; init; }
}
+23
View File
@@ -0,0 +1,23 @@
using Microsoft.Extensions.DependencyInjection;
using Toolkit.Foundation;
namespace Bitvault;
public class ItemActivatedHandler(IServiceProvider serviceProvider,
IProxyService<IPublisher> proxyPublisher) :
INotificationHandler<ActivatedEventArgs<Item>>
{
public async Task Handle(ActivatedEventArgs<Item> args,
CancellationToken cancellationToken = default)
{
IServiceScope serviceScope = serviceProvider.CreateScope();
IServiceFactory serviceFactory = serviceScope.ServiceProvider.GetRequiredService<IServiceFactory>();
if (serviceFactory.Create<ItemNavigationViewModel>(2, "efesf", "Description " + 1) is ItemNavigationViewModel viewModel)
{
// somehow, we need to get back out of the scope back to the compoment level, this currently doesnt work, and we need a better and cleaner way
await proxyPublisher.Proxy.Publish(new CreateEventArgs<ItemNavigationViewModel>(viewModel),
nameof(ContainerViewModel), cancellationToken);
}
}
}