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
+1
View File
@@ -90,6 +90,7 @@ public partial class App : Application
services.AddTemplate<ItemHeaderViewModel, ItemHeaderView>(); services.AddTemplate<ItemHeaderViewModel, ItemHeaderView>();
services.AddHandler<CreateItemHandler>(); services.AddHandler<CreateItemHandler>();
services.AddHandler<ItemActivatedHandler>();
}); });
})!); })!);
@@ -1,10 +1,12 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Bitvault.Data; namespace Bitvault.Data;
public record Document [Table("Blobs")]
public record BlobEntry
{ {
public byte[]? Blob { get; set; } public byte[]? Data { get; set; }
public DocumentType Type { get; set; } public DocumentType Type { get; set; }
+6 -6
View File
@@ -5,20 +5,20 @@ namespace Bitvault.Data;
public class ContainerDbContext(DbContextOptions<ContainerDbContext> options) : public class ContainerDbContext(DbContextOptions<ContainerDbContext> options) :
DbContext(options) DbContext(options)
{ {
public DbSet<Document> Documents { get; set; } public DbSet<BlobEntry> Blobs { get; set; }
public DbSet<Item> Items { get; set; } public DbSet<ItemEntry> Items { get; set; }
public DbSet<Tag> Tags { get; set; } public DbSet<TagEntry> Tags { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
{ {
modelBuilder.Entity<Item>() modelBuilder.Entity<ItemEntry>()
.HasMany(x => x.Tags) .HasMany(x => x.Tags)
.WithOne(); .WithOne();
modelBuilder.Entity<Item>() modelBuilder.Entity<ItemEntry>()
.HasMany(x => x.Documents) .HasMany(x => x.Blobs)
.WithOne(); .WithOne();
} }
} }
-20
View File
@@ -1,20 +0,0 @@
using Bitvault.Data;
using System.ComponentModel.DataAnnotations;
namespace Bitvault.Data;
public record Item
{
[Key]
public int Id { get; set; }
public string? Name { get; set; }
public string? Description { get; set; }
public int State { get; set; } = 0;
public ICollection<Tag>? Tags { get; }
public ICollection<Document>? Documents { get; }
}
+21
View File
@@ -0,0 +1,21 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Bitvault.Data;
[Table("Items")]
public record ItemEntry
{
[Key]
public int Id { get; set; }
public string? Name { get; set; }
public string? Description { get; set; }
public int State { get; set; } = 0;
public ICollection<TagEntry>? Tags { get; }
public ICollection<BlobEntry>? Blobs { get; }
}
@@ -1,8 +1,10 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Bitvault.Data; namespace Bitvault.Data;
public class Tag [Table("Tags")]
public class TagEntry
{ {
[Key] [Key]
public int Id { get; set; } public int Id { get; set; }
-11
View File
@@ -32,14 +32,3 @@ 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) 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") if (configuration.Filter == "All")
{ {
@@ -36,7 +36,7 @@ public class ContainerViewModelHandler(IDbContextFactory<ContainerDbContext> dbC
var items = await Task.Run(async () => var items = await Task.Run(async () =>
{ {
using ContainerDbContext context = dbContextFactory.CreateDbContext(); 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.Id,
x.Name x.Name
+9 -3
View File
@@ -1,5 +1,6 @@
using Bitvault.Data; using Bitvault.Data;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Toolkit.Foundation; using Toolkit.Foundation;
namespace Bitvault; namespace Bitvault;
@@ -14,17 +15,22 @@ public class CreateItemHandler(IDbContextFactory<ContainerDbContext> dbContextFa
{ {
try try
{ {
using ContainerDbContext context = dbContextFactory.CreateDbContext();
EntityEntry<ItemEntry>? result = null;
await Task.Run(async () => await Task.Run(async () =>
{ {
using ContainerDbContext context = dbContextFactory.CreateDbContext(); result = await context.AddAsync(new ItemEntry { Name = configuration.Name }, cancellationToken);
await context.AddAsync(new Data.Item { Name = configuration.Name }, cancellationToken);
await context.SaveChangesAsync(cancellationToken); await context.SaveChangesAsync(cancellationToken);
}, cancellationToken); }, cancellationToken);
await publisher.Publish(Activated.As(configuration)); if (result is not null)
{
await publisher.Publish(Activated.As(new Item { Id = result.Entity.Id }), cancellationToken);
return true; return true;
} }
}
catch catch
{ {
+1 -17
View File
@@ -1,23 +1,7 @@
namespace Bitvault; namespace Bitvault;
public record Item<TValue>(TValue? Value = default);
public record Item public record Item
{ {
public Item(int id) public int Id { get; init; }
{
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; }
} }
+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);
}
}
}