This commit is contained in:
TheXamlGuy
2024-04-26 22:27:31 +01:00
parent d799eab511
commit 9f90ef693d
39 changed files with 236 additions and 211 deletions
+1 -1
View File
@@ -14,7 +14,7 @@ public class ContentTemplate :
{ {
if (item is IObservableViewModel observableViewModel) if (item is IObservableViewModel observableViewModel)
{ {
if (observableViewModel.ServiceProvider is IServiceProvider provider) if (observableViewModel.Provider is IServiceProvider provider)
{ {
IContentTemplateDescriptorProvider? contentTemplateProvider = provider.GetService<IContentTemplateDescriptorProvider>(); IContentTemplateDescriptorProvider? contentTemplateProvider = provider.GetService<IContentTemplateDescriptorProvider>();
INavigationContext? viewModelContentBinder = provider.GetService<INavigationContext>(); INavigationContext? viewModelContentBinder = provider.GetService<INavigationContext>();
+8 -1
View File
@@ -1,3 +1,10 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public record Changed<TValue>(TValue? Value = default) : INotification; public record Changed<TValue>(TValue? Value = default);
public record Changed
{
public static Changed<TValue> As<TValue>(TValue value) => new(value);
public static Changed<TValue> As<TValue>() where TValue : new() => new(new TValue());
}
+4 -3
View File
@@ -2,12 +2,13 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public partial class CommandValueViewModel<TValue>(IServiceProvider serviceProvider, public partial class CommandValueViewModel<TValue>(IServiceProvider provider,
IServiceFactory serviceFactory, IServiceFactory factory,
IMediator mediator,
IPublisher publisher, IPublisher publisher,
ISubscriber subscriber, ISubscriber subscriber,
IDisposer disposer) : IDisposer disposer) :
ValueViewModel<TValue>(serviceProvider, serviceFactory, publisher, subscriber, disposer) ValueViewModel<TValue>(provider, factory, mediator, publisher, subscriber, disposer)
{ {
public IRelayCommand InvokeCommand => public IRelayCommand InvokeCommand =>
new AsyncRelayCommand(InvokeAsync); new AsyncRelayCommand(InvokeAsync);
+4 -3
View File
@@ -2,12 +2,13 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public partial class CommandViewModel(IServiceProvider serviceProvider, public partial class CommandViewModel(IServiceProvider provider,
IServiceFactory serviceFactory, IServiceFactory factory,
IMediator mediator,
IPublisher publisher, IPublisher publisher,
ISubscriber subscriber, ISubscriber subscriber,
IDisposer disposer) : IDisposer disposer) :
ObservableViewModel(serviceProvider, serviceFactory, publisher, subscriber, disposer) ObservableViewModel(provider, factory, mediator, publisher, subscriber, disposer)
{ {
public IRelayCommand InvokeCommand => public IRelayCommand InvokeCommand =>
new AsyncRelayCommand(InvokeAsync); new AsyncRelayCommand(InvokeAsync);
@@ -8,14 +8,15 @@ public partial class ComponentConfigurationViewModel<TConfiguration, TValue, THe
INotificationHandler<Changed<TConfiguration>> INotificationHandler<Changed<TConfiguration>>
where TConfiguration : class where TConfiguration : class
{ {
public ComponentConfigurationViewModel(IServiceProvider serviceProvider, public ComponentConfigurationViewModel(IServiceProvider provider,
IServiceFactory serviceFactory, IServiceFactory factory,
IMediator mediator,
IPublisher publisher, IPublisher publisher,
ISubscriber subscriber, ISubscriber subscriber,
IDisposer disposer, IDisposer disposer,
THeader header, THeader header,
TDescription description, TDescription description,
TAction action) : base(serviceProvider, serviceFactory, publisher, subscriber, disposer) TAction action) : base(provider, factory, mediator, publisher, subscriber, disposer)
{ {
} }
@@ -27,8 +28,9 @@ public partial class ComponentConfigurationViewModel<TConfiguration, TValue, THe
} }
} }
public partial class ComponentConfigurationViewModel<TConfiguration, TValue, TAction>(IServiceProvider serviceProvider, public partial class ComponentConfigurationViewModel<TConfiguration, TValue, TAction>(IServiceProvider provider,
IServiceFactory serviceFactory, IServiceFactory factory,
IMediator mediator,
IPublisher publisher, IPublisher publisher,
ISubscriber subscriber, ISubscriber subscriber,
IDisposer disposer, IDisposer disposer,
@@ -37,7 +39,7 @@ public partial class ComponentConfigurationViewModel<TConfiguration, TValue, TAc
Func<TConfiguration, TValue> valueDelegate, Func<TConfiguration, TValue> valueDelegate,
object header, object header,
object description) : object description) :
ValueViewModel<TValue>(serviceProvider, serviceFactory, publisher, subscriber, disposer), ValueViewModel<TValue>(provider, factory, mediator, publisher, subscriber, disposer),
IComponentConfigurationViewModel, IComponentConfigurationViewModel,
INotificationHandler<Changed<TConfiguration>> INotificationHandler<Changed<TConfiguration>>
where TConfiguration : class where TConfiguration : class
@@ -68,8 +70,9 @@ public partial class ComponentConfigurationViewModel<TConfiguration, TValue, TAc
} }
} }
public partial class ComponentConfigurationViewModel<TConfiguration, TValue, TDescription, TAction>(IServiceProvider serviceProvider, public partial class ComponentConfigurationViewModel<TConfiguration, TValue, TDescription, TAction>(IServiceProvider provider,
IServiceFactory serviceFactory, IServiceFactory factory,
IMediator mediator,
IPublisher publisher, IPublisher publisher,
ISubscriber subscriber, ISubscriber subscriber,
IDisposer disposer, IDisposer disposer,
@@ -78,7 +81,7 @@ public partial class ComponentConfigurationViewModel<TConfiguration, TValue, TDe
TConfiguration configuration, TConfiguration configuration,
Func<TConfiguration, TValue> valueDelegate, Func<TConfiguration, TValue> valueDelegate,
object header) : object header) :
ValueViewModel<TValue>(serviceProvider, serviceFactory, publisher, subscriber, disposer), ValueViewModel<TValue>(provider, factory, mediator, publisher, subscriber, disposer),
IComponentConfigurationViewModel, IComponentConfigurationViewModel,
INotificationHandler<Changed<TConfiguration>> INotificationHandler<Changed<TConfiguration>>
where TConfiguration : class where TConfiguration : class
+1 -1
View File
@@ -3,7 +3,7 @@ using Microsoft.Extensions.Hosting;
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public sealed class ComponentHost(IServiceProvider services, public class ComponentHost(IServiceProvider services,
IEnumerable<IInitializer> initializers, IEnumerable<IInitializer> initializers,
IEnumerable<IHostedService> hostedServices) : IEnumerable<IHostedService> hostedServices) :
IComponentHost IComponentHost
+5 -4
View File
@@ -1,12 +1,13 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public record Create<TValue>(TValue Value) : public record Create<TValue>(TValue Value);
INotification;
public record Create public record Create
{ {
public static Create<TValue> As<TValue>(TValue value) => new(value); public static Create<TValue> As<TValue>(TValue value) =>
new(value);
public static Create<TValue> As<TValue>() where TValue : new() => new(new TValue()); public static Create<TValue> As<TValue>() where TValue : new() =>
new(new TValue());
} }
+1 -1
View File
@@ -1,3 +1,3 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public record Enumerate<TValue>(object? Key = null) : INotification; public record Enumerate<TValue>(object? Key = null);
+2 -3
View File
@@ -1,5 +1,4 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public delegate Task<TResponse> HandlerDelegate<TMessage, TResponse>(TMessage message, public delegate Task<TResponse> HandlerDelegate<TRequest, TResponse>(TRequest request,
CancellationToken cancellationToken) CancellationToken cancellationToken);
where TMessage : IMessage;
+10 -10
View File
@@ -1,25 +1,25 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public class HandlerWrapper<TMessage, TReply>(IHandler<TMessage, TReply> handler, public class HandlerWrapper<TRequest, TResponse>(IHandler<TRequest, TResponse> handler,
IEnumerable<IPipelineBehavior<TMessage, TReply>> pipelineBehaviours) IEnumerable<IPipelineBehaviour<TRequest, TResponse>> pipelineBehaviours)
where TMessage : class, IRequest<TReply> where TRequest : class
{ {
private readonly IEnumerable<IPipelineBehavior<TMessage, TReply>> pipelineBehaviours = private readonly IEnumerable<IPipelineBehaviour<TRequest, TResponse>> pipelineBehaviours =
pipelineBehaviours.Reverse(); pipelineBehaviours.Reverse();
public async Task<TReply> Handle(TMessage message, public async Task<TResponse> Handle(TRequest request,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
HandlerDelegate<TMessage, TReply> currentHandler = handler.Handle; HandlerDelegate<TRequest, TResponse> currentHandler = handler.Handle;
foreach (IPipelineBehavior<TMessage, TReply> behavior in pipelineBehaviours) foreach (IPipelineBehaviour<TRequest, TResponse> behaviour in pipelineBehaviours)
{ {
HandlerDelegate<TMessage, TReply> previousHandler = currentHandler; HandlerDelegate<TRequest, TResponse> previousHandler = currentHandler;
currentHandler = async (args, token) => currentHandler = async (args, token) =>
{ {
return await behavior.Handle(args, previousHandler, token); return await behaviour.Handle(args, previousHandler, token);
}; };
} }
return await currentHandler(message, cancellationToken); return await currentHandler(request, cancellationToken);
} }
} }
+1 -5
View File
@@ -4,14 +4,10 @@ public interface IHandler;
public interface IHandler<in TRequest, TResponse> : public interface IHandler<in TRequest, TResponse> :
IHandler IHandler
where TRequest :
IRequest<TResponse>
{ {
Task<TResponse> Handle(TRequest args, Task<TResponse> Handle(TRequest args,
CancellationToken cancellationToken); CancellationToken cancellationToken);
} }
public interface IHandler<in TRequest> : public interface IHandler<in TRequest> :
IHandler<TRequest, Unit> IHandler<TRequest, Unit>;
where TRequest :
IRequest<Unit>;
+4 -3
View File
@@ -2,9 +2,10 @@
public interface IMediator public interface IMediator
{ {
Task<TResponse?> SendAsync<TResponse>(IRequest<TResponse> request, Task<TResponse?> Handle<TRequest, TResponse>(TRequest request,
CancellationToken cancellationToken = default); CancellationToken cancellationToken = default)
where TRequest : notnull;
Task<object?> SendAsync(object message, CancellationToken Task<object?> Handle(object request, CancellationToken
cancellationToken = default); cancellationToken = default);
} }
-4
View File
@@ -1,4 +0,0 @@
namespace Toolkit.Foundation;
public interface INotification :
IMessage;
+2 -4
View File
@@ -1,10 +1,8 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public interface INotificationHandler<in TNotification> : public interface INotificationHandler<in TMessage> :
IHandler IHandler
where TNotification :
INotification
{ {
Task Handle(TNotification args, Task Handle(TMessage args,
CancellationToken cancellationToken = default); CancellationToken cancellationToken = default);
} }
+2 -2
View File
@@ -7,7 +7,7 @@ public interface IObservableViewModel :
public IPublisher Publisher { get; } public IPublisher Publisher { get; }
public IServiceFactory ServiceFactory { get; } public IServiceFactory Factory { get; }
public IServiceProvider ServiceProvider { get; } public IServiceProvider Provider { get; }
} }
+4 -6
View File
@@ -1,17 +1,15 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public interface IPipelineBehavior<TMessage, TResponse> public interface IPipelineBehaviour<TMessage, TResponse>
where TMessage : IMessage
{ {
Task<TResponse> Handle(TMessage message, Task<TResponse> Handle(TMessage message,
HandlerDelegate<TMessage, TResponse> next, HandlerDelegate<TMessage, TResponse> next,
CancellationToken cancellationToken = default); CancellationToken cancellationToken = default);
} }
public interface IPipelineBehavior<TNotification> public interface IPipelineBehaviour<TMessage>
where TNotification : INotification
{ {
Task Handle(TNotification notification, Task Handle(TMessage message,
NotificationHandlerDelegate<TNotification> next, NotificationHandlerDelegate<TMessage> next,
CancellationToken cancellationToken = default); CancellationToken cancellationToken = default);
} }
+19 -32
View File
@@ -2,57 +2,44 @@
public interface IPublisher public interface IPublisher
{ {
public Task Publish<TNotification>(object key, public Task Publish<TMessage>(object key,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
where TNotification : where TMessage : new();
INotification,
new();
public Task Publish<TNotification>(TNotification notification, public Task Publish<TMessage>(TMessage message,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
where TNotification : where TMessage : notnull;
INotification;
public Task Publish<TNotification>(TNotification notification, public Task Publish<TMessage>(TMessage message,
object key, object key,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
where TNotification : where TMessage : notnull;
INotification;
Task PublishUI<TNotification>(TNotification notification, Task PublishUI<TMessage>(TMessage message,
object key, object key,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
where TNotification : where TMessage : notnull;
INotification; Task PublishUI<TMessage>(object key,
Task PublishUI<TNotification>(object key,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
where TNotification : where TMessage : new();
INotification,
new();
Task PublishUI<TNotification>(TNotification notification, Task PublishUI<TMessage>(TMessage message,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
where TNotification : where TMessage : notnull;
INotification;
Task PublishUI(object notification, Task PublishUI(object message,
CancellationToken cancellationToken = default); CancellationToken cancellationToken = default);
Task Publish(object notification, Task Publish(object message,
Func<Func<Task>, Task> marshal, Func<Func<Task>, Task> marshal,
object? key = null, object? key = null,
CancellationToken cancellationToken = default); CancellationToken cancellationToken = default);
Task PublishUIAsync<TNotification>(CancellationToken cancellationToken = default) Task PublishUI<TMessage>(CancellationToken cancellationToken = default)
where TNotification : where TMessage : new();
INotification,
new();
Task Publish<TNotification>(CancellationToken cancellationToken = default) Task Publish<TMessage>(CancellationToken cancellationToken = default)
where TNotification : where TMessage : new();
INotification,
new();
public Task Publish(object notification, CancellationToken cancellationToken = default); public Task Publish(object message, CancellationToken cancellationToken = default);
} }
@@ -178,7 +178,7 @@ public static class IServiceCollectionExtensions
services.Add(new ServiceDescriptor(wrapperType, provider => services.Add(new ServiceDescriptor(wrapperType, provider =>
provider.GetService<IServiceFactory>()?.Create(wrapperType, provider.GetService<IServiceFactory>()?.Create(wrapperType,
provider.GetRequiredService(typeof(INotificationHandler<>).MakeGenericType(notificationType)), provider.GetRequiredService(typeof(INotificationHandler<>).MakeGenericType(notificationType)),
provider.GetServices(typeof(IPipelineBehavior<>) provider.GetServices(typeof(IPipelineBehaviour<>)
.MakeGenericType(notificationType)))!, lifetime)); .MakeGenericType(notificationType)))!, lifetime));
} }
@@ -197,7 +197,7 @@ public static class IServiceCollectionExtensions
services.Add(new ServiceDescriptor(wrapperType, provider => services.Add(new ServiceDescriptor(wrapperType, provider =>
provider.GetService<IServiceFactory>()?.Create(wrapperType, provider.GetService<IServiceFactory>()?.Create(wrapperType,
provider.GetRequiredService<THandler>(), provider.GetRequiredService<THandler>(),
provider.GetServices(typeof(IPipelineBehavior<,>) provider.GetServices(typeof(IPipelineBehaviour<,>)
.MakeGenericType(requestType, responseType)))!, lifetime)); .MakeGenericType(requestType, responseType)))!, lifetime));
} }
} }
+10
View File
@@ -0,0 +1,10 @@
namespace Toolkit.Foundation;
public record Initialized<TValue>(TValue Value);
public record Initialized
{
public static Initialized<TValue> As<TValue>(TValue value) => new(value);
public static Initialized<TValue> As<TValue>() where TValue : new() => new(new TValue());
}
+10 -1
View File
@@ -1,3 +1,12 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public record Insert<TValue>(int Index, TValue Value) : INotification; public record Insert<TValue>(int Index, TValue Value);
public record Insert
{
public static Insert<TValue> As<TValue>(int index, TValue value) =>
new(index, value);
public static Insert<TValue> As<TValue>(int index) where TValue : new() =>
new(index, new TValue());
}
+3 -2
View File
@@ -5,8 +5,9 @@ namespace Toolkit.Foundation;
public class Mediator(IServiceProvider provider) : public class Mediator(IServiceProvider provider) :
IMediator IMediator
{ {
public Task<TResponse?> SendAsync<TResponse>(IRequest<TResponse> request, public Task<TResponse?> Handle<TRequest, TResponse>(TRequest request,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
where TRequest : notnull
{ {
Type handlerType = typeof(HandlerWrapper<,>).MakeGenericType(request.GetType(), Type handlerType = typeof(HandlerWrapper<,>).MakeGenericType(request.GetType(),
typeof(TResponse)); typeof(TResponse));
@@ -23,7 +24,7 @@ public class Mediator(IServiceProvider provider) :
return Task.FromResult<TResponse?>(default); return Task.FromResult<TResponse?>(default);
} }
public Task<object?> SendAsync(object message, public Task<object?> Handle(object message,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
if (message.GetType().GetInterface(typeof(IRequest<>).Name) is Type requestType && if (message.GetType().GetInterface(typeof(IRequest<>).Name) is Type requestType &&
+10 -1
View File
@@ -1,3 +1,12 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public record Move<TValue>(int Index, TValue Value) : INotification; public record Move<TValue>(int Index, TValue Value);
public record Move
{
public static Move<TValue> As<TValue>(int index, TValue value) =>
new(index, value);
public static Insert<TValue> As<TValue>(int index) where TValue : new() =>
new(index, new TValue());
}
+2 -4
View File
@@ -5,12 +5,10 @@ public record Navigate(string Route,
string? Scope = null, string? Scope = null,
object? Sender = null, object? Sender = null,
EventHandler? Navigated = null, EventHandler? Navigated = null,
object[]? Parameters = null) : object[]? Parameters = null);
INotification;
public record Navigate<TNavigation>(object Context, public record Navigate<TNavigation>(object Context,
object Template, object Template,
object Content, object Content,
object? Sender = null, object? Sender = null,
object[]? Parameters = null) : object[]? Parameters = null);
INotification;
+2 -4
View File
@@ -1,7 +1,5 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public record NavigateBack(object? Context = null, string? Scope = null) : public record NavigateBack(object? Context = null, string? Scope = null);
INotification;
public record NavigateBack<TNavigation>(object? Context) : public record NavigateBack<TNavigation>(object? Context);
INotification;
+1 -2
View File
@@ -1,4 +1,3 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public record NavigationChanged<TValue>(TValue? Value) : public record NavigationChanged<TValue>(TValue? Value);
INotification;
+5 -5
View File
@@ -3,8 +3,8 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public class NavigationScope(IPublisher publisher, public class NavigationScope(IPublisher publisher,
IServiceProvider serviceProvider, IServiceProvider provider,
IServiceFactory serviceFactory, IServiceFactory factory,
INavigationProvider navigationProvider, INavigationProvider navigationProvider,
INavigationContextProvider navigationContextProvider, INavigationContextProvider navigationContextProvider,
IContentTemplateDescriptorProvider contentTemplateDescriptorProvider) : IContentTemplateDescriptorProvider contentTemplateDescriptorProvider) :
@@ -39,11 +39,11 @@ public class NavigationScope(IPublisher publisher,
Enumerable.Empty<object?>(), Enumerable.Empty<object?>(),
.. mappedParameters ?? Enumerable.Empty<object?>()]; .. mappedParameters ?? Enumerable.Empty<object?>()];
if (serviceProvider.GetRequiredKeyedService(descriptor.TemplateType, segment) is object view) if (provider.GetRequiredKeyedService(descriptor.TemplateType, segment) is object view)
{ {
if ((parameters is { Length: > 0 } if ((parameters is { Length: > 0 }
? serviceFactory.Create(descriptor.ContentType, parameters) ? factory.Create(descriptor.ContentType, parameters)
: serviceProvider.GetRequiredKeyedService(descriptor.ContentType, segment)) is object viewModel) : provider.GetRequiredKeyedService(descriptor.ContentType, segment)) is object viewModel)
{ {
if (context is not null) if (context is not null)
{ {
@@ -1,5 +1,4 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public delegate Task NotificationHandlerDelegate<TNotification>(TNotification notification, public delegate Task NotificationHandlerDelegate<TMessage>(TMessage message,
CancellationToken cancellationToken) CancellationToken cancellationToken);
where TNotification : INotification;
@@ -1,25 +1,24 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public class NotificationHandlerWrapper<TNotification>(INotificationHandler<TNotification> handler, public class NotificationHandlerWrapper<TMessage>(INotificationHandler<TMessage> handler,
IEnumerable<IPipelineBehavior<TNotification>> pipelineBehaviours) IEnumerable<IPipelineBehaviour<TMessage>> pipelineBehaviours)
where TNotification : INotification
{ {
private readonly IEnumerable<IPipelineBehavior<TNotification>> pipelineBehaviours = private readonly IEnumerable<IPipelineBehaviour<TMessage>> pipelineBehaviours =
pipelineBehaviours.Reverse(); pipelineBehaviours.Reverse();
public async Task Handle(TNotification notification, public async Task Handle(TMessage message,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
NotificationHandlerDelegate<TNotification> currentHandler = handler.Handle; NotificationHandlerDelegate<TMessage> currentHandler = handler.Handle;
foreach (IPipelineBehavior<TNotification> behavior in pipelineBehaviours) foreach (IPipelineBehaviour<TMessage> behaviour in pipelineBehaviours)
{ {
NotificationHandlerDelegate<TNotification> previousHandler = currentHandler; NotificationHandlerDelegate<TMessage> previousHandler = currentHandler;
currentHandler = async (args, token) => currentHandler = async (args, token) =>
{ {
await behavior.Handle(args, previousHandler, token); await behaviour.Handle(args, previousHandler, token);
}; };
} }
await currentHandler(notification, cancellationToken); await currentHandler(message, cancellationToken);
} }
} }
@@ -30,14 +30,16 @@ public partial class ObservableCollectionViewModel<TViewModel> :
[ObservableProperty] [ObservableProperty]
private bool isInitialized; private bool isInitialized;
public ObservableCollectionViewModel(IServiceProvider serviceProvider, public ObservableCollectionViewModel(IServiceProvider provider,
IServiceFactory serviceFactory, IServiceFactory factory,
IMediator mediator,
IPublisher publisher, IPublisher publisher,
ISubscriber subscriber, ISubscriber subscriber,
IDisposer disposer) IDisposer disposer)
{ {
ServiceProvider = serviceProvider; Provider = provider;
ServiceFactory = serviceFactory; Factory = factory;
Mediator = mediator;
Publisher = publisher; Publisher = publisher;
Disposer = disposer; Disposer = disposer;
@@ -46,15 +48,17 @@ public partial class ObservableCollectionViewModel<TViewModel> :
collection.CollectionChanged += OnCollectionChanged; collection.CollectionChanged += OnCollectionChanged;
} }
public ObservableCollectionViewModel(IServiceProvider serviceProvider, public ObservableCollectionViewModel(IServiceProvider provider,
IServiceFactory serviceFactory, IServiceFactory factory,
IMediator mediator,
IPublisher publisher, IPublisher publisher,
ISubscriber subscriber, ISubscriber subscriber,
IDisposer disposer, IDisposer disposer,
IEnumerable<TViewModel> items) IEnumerable<TViewModel> items)
{ {
ServiceProvider = serviceProvider; Provider = provider;
ServiceFactory = serviceFactory; Factory = factory;
Mediator = mediator;
Publisher = publisher; Publisher = publisher;
Disposer = disposer; Disposer = disposer;
@@ -72,6 +76,8 @@ public partial class ObservableCollectionViewModel<TViewModel> :
public IDisposer Disposer { get; private set; } public IDisposer Disposer { get; private set; }
public IServiceFactory Factory { get; private set; }
bool IList.IsFixedSize => false; bool IList.IsFixedSize => false;
bool ICollection<TViewModel>.IsReadOnly => false; bool ICollection<TViewModel>.IsReadOnly => false;
@@ -80,12 +86,12 @@ public partial class ObservableCollectionViewModel<TViewModel> :
bool ICollection.IsSynchronized => false; bool ICollection.IsSynchronized => false;
public IMediator Mediator { get; }
public IServiceProvider Provider { get; private set; }
public IPublisher Publisher { get; private set; } public IPublisher Publisher { get; private set; }
public IServiceFactory ServiceFactory { get; private set; }
public IServiceProvider ServiceProvider { get; private set; }
object ICollection.SyncRoot => this; object ICollection.SyncRoot => this;
public TViewModel this[int index] public TViewModel this[int index]
@@ -119,7 +125,7 @@ public partial class ObservableCollectionViewModel<TViewModel> :
public TViewModel Add() public TViewModel Add()
{ {
TViewModel? item = ServiceFactory.Create<TViewModel>(); TViewModel? item = Factory.Create<TViewModel>();
Add(item); Add(item);
return item; return item;
@@ -128,7 +134,7 @@ public partial class ObservableCollectionViewModel<TViewModel> :
public TViewModel Add<T>(params object?[] parameters) public TViewModel Add<T>(params object?[] parameters)
where T : TViewModel where T : TViewModel
{ {
T? item = ServiceFactory.Create<T>(parameters); T? item = Factory.Create<T>(parameters);
Add(item); Add(item);
return item; return item;
@@ -138,7 +144,7 @@ public partial class ObservableCollectionViewModel<TViewModel> :
where T : where T :
TViewModel TViewModel
{ {
T? item = ServiceFactory.Create<T>(); T? item = Factory.Create<T>();
Add(item); Add(item);
return item; return item;
@@ -407,9 +413,10 @@ public partial class ObservableCollectionViewModel<TViewModel> :
CollectionChanged?.Invoke(this, args); CollectionChanged?.Invoke(this, args);
} }
public class ObservableCollectionViewModel(IServiceProvider serviceProvider, public class ObservableCollectionViewModel(IServiceProvider provider,
IServiceFactory serviceFactory, IServiceFactory factory,
IMediator mediator,
IPublisher publisher, IPublisher publisher,
ISubscriber subscriber, ISubscriber subscriber,
IDisposer disposer) : IDisposer disposer) :
ObservableCollectionViewModel<IDisposable>(serviceProvider, serviceFactory, publisher, subscriber, disposer); ObservableCollectionViewModel<IDisposable>(provider, factory, mediator, publisher, subscriber, disposer);
+12 -8
View File
@@ -14,14 +14,16 @@ public partial class ObservableViewModel :
[ObservableProperty] [ObservableProperty]
private bool isInitialized; private bool isInitialized;
public ObservableViewModel(IServiceProvider serviceProvider, public ObservableViewModel(IServiceProvider provider,
IServiceFactory serviceFactory, IServiceFactory factory,
IMediator mediator,
IPublisher publisher, IPublisher publisher,
ISubscriber subscriber, ISubscriber subscriber,
IDisposer disposer) IDisposer disposer)
{ {
ServiceProvider = serviceProvider; Provider = provider;
ServiceFactory = serviceFactory; Factory = factory;
Mediator = mediator;
Publisher = publisher; Publisher = publisher;
Disposer = disposer; Disposer = disposer;
@@ -32,12 +34,14 @@ public partial class ObservableViewModel :
public IDisposer Disposer { get; } public IDisposer Disposer { get; }
public IServiceFactory Factory { get; }
public IMediator Mediator { get; }
public IServiceProvider Provider { get; }
public IPublisher Publisher { get; } public IPublisher Publisher { get; }
public IServiceFactory ServiceFactory { get; }
public IServiceProvider ServiceProvider { get; }
public virtual Task Activated() => public virtual Task Activated() =>
Task.CompletedTask; Task.CompletedTask;
+32 -38
View File
@@ -8,32 +8,31 @@ public class Publisher(ISubscriptionManager subscriptionManager,
IDispatcher dispatcher) : IDispatcher dispatcher) :
IPublisher IPublisher
{ {
public Task Publish<TNotification>(object key, public Task Publish<TMessage>(object key,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
where TNotification : where TMessage : new() =>
INotification, Publish(new TMessage(), async args => await args(),
new() => Publish(new TNotification(), async args => await args(),
key, cancellationToken); key, cancellationToken);
public Task Publish<TNotification>(TNotification notification, public Task Publish<TMessage>(TMessage message,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
where TNotification : where TMessage : notnull =>
INotification => Publish(notification, async args => await args(), Publish(message, async args => await args(),
null, cancellationToken); null, cancellationToken);
public Task Publish<TNotification>(TNotification notification, public Task Publish<TMessage>(TMessage message,
object key, object key,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
where TNotification : where TMessage : notnull =>
INotification => Publish(notification, Publish(message, async args => await args(),
async args => await args(), key, cancellationToken); key, cancellationToken);
public async Task Publish(object notification, public async Task Publish(object message,
Func<Func<Task>, Task> marshal, Func<Func<Task>, Task> marshal,
object? key = null, object? key = null,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
Type notificationType = notification.GetType(); Type notificationType = message.GetType();
List<object?> handlers = provider.GetServices(typeof(NotificationHandlerWrapper<>) List<object?> handlers = provider.GetServices(typeof(NotificationHandlerWrapper<>)
.MakeGenericType(notificationType)).ToList(); .MakeGenericType(notificationType)).ToList();
@@ -55,52 +54,47 @@ public class Publisher(ISubscriptionManager subscriptionManager,
if (handleMethod is not null) if (handleMethod is not null)
{ {
await marshal(() => (Task)handleMethod.Invoke(handler, new object[] await marshal(() => (Task)handleMethod.Invoke(handler, new object[]
{ notification, cancellationToken })!); { message, cancellationToken })!);
} }
} }
} }
} }
public Task Publish(object notification, public Task Publish(object message,
CancellationToken cancellationToken = default) => Publish(notification, CancellationToken cancellationToken = default) => Publish(message,
async args => await args(), async args => await args(),
null, cancellationToken); null, cancellationToken);
public Task Publish<TNotification>(CancellationToken cancellationToken = default) public Task Publish<TMessage>(CancellationToken cancellationToken = default)
where TNotification : where TMessage : new() =>
INotification, new() => Publish(new TNotification(), Publish(new TMessage(), async args => await args(),
async args => await args(),
null, cancellationToken); null, cancellationToken);
public Task PublishUI<TNotification>(object key, public Task PublishUI<TMessage>(object key,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
where TNotification : where TMessage : new() =>
INotification, new() => Publish(new TNotification(), Publish(new TMessage(), args => dispatcher.InvokeAsync(async () => await args()),
args => dispatcher.InvokeAsync(async () => await args()),
key, cancellationToken); key, cancellationToken);
public Task PublishUI<TNotification>(TNotification notification, public Task PublishUI<TMessage>(TMessage message,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
where TNotification : where TMessage : notnull =>
INotification => Publish(notification, Publish(message, args => dispatcher.InvokeAsync(async () => await args()),
args => dispatcher.InvokeAsync(async () => await args()),
null, cancellationToken); null, cancellationToken);
public Task PublishUI<TNotification>(TNotification notification, public Task PublishUI<TMessage>(TMessage message,
object key, object key,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
where TNotification : where TMessage : notnull =>
INotification => Publish(notification, Publish(message, args => dispatcher.InvokeAsync(async () => await args()),
args => dispatcher.InvokeAsync(async () => await args()),
key, cancellationToken); key, cancellationToken);
public Task PublishUIAsync<TNotification>(CancellationToken cancellationToken = default) public Task PublishUI<TMessage>(CancellationToken cancellationToken = default)
where TNotification : where TMessage : new() =>
INotification, new() => Publish(new TNotification(), Publish(new TMessage(), args => dispatcher.InvokeAsync(async () => await args()),
args => dispatcher.InvokeAsync(async () => await args()),
null, cancellationToken); null, cancellationToken);
public Task PublishUI(object notification, public Task PublishUI(object message,
CancellationToken cancellationToken = default) => Publish(notification, args => CancellationToken cancellationToken = default) => Publish(message, args =>
dispatcher.InvokeAsync(async () => await args()), dispatcher.InvokeAsync(async () => await args()),
null, cancellationToken); null, cancellationToken);
} }
+10 -2
View File
@@ -1,5 +1,13 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public record Remove<TValue>(TValue Value) : public record Remove<TValue>(TValue Value);
INotification;
public record Remove
{
public static Remove<TValue> As<TValue>(TValue value) =>
new(value);
public static Remove<TValue> As<TValue>() where TValue : new() =>
new(new TValue());
}
+9 -2
View File
@@ -1,6 +1,13 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public record Replace<TValue>(int Index, TValue Value, object? Target = null) : public record Replace<TValue>(int Index, TValue Value);
INotification;
public record Replace
{
public static Replace<TValue> As<TValue>(int index, TValue value) =>
new(index, value);
public static Replace<TValue> As<TValue>(int index) where TValue : new() =>
new(index, new TValue());
}
+2 -6
View File
@@ -1,12 +1,8 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public record Request<TValue> : public record Request<TValue>;
INotification
{
}
public class Request public class Request
{ {
public static Request<TValue> Create<TValue>() => new(); public static Request<TValue> As<TValue>() => new();
} }
+1 -2
View File
@@ -1,4 +1,3 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public record Response<TValue>(TValue Value) : public record Response<TValue>(TValue Value);
INotification;
+2 -2
View File
@@ -11,9 +11,9 @@ public class ServiceScopeFactory<TService>(IServiceScopeFactory serviceScopeFact
{ {
if (serviceScopeFactory.CreateScope() is IServiceScope serviceScope) if (serviceScopeFactory.CreateScope() is IServiceScope serviceScope)
{ {
if (serviceScope.ServiceProvider.GetService<IServiceFactory>() is IServiceFactory serviceFactory) if (serviceScope.ServiceProvider.GetService<IServiceFactory>() is IServiceFactory factory)
{ {
if (serviceFactory.Create<TService>(parameters) is TService service) if (factory.Create<TService>(parameters) is TService service)
{ {
cache.Add(service, serviceScope); cache.Add(service, serviceScope);
return service; return service;
+1 -2
View File
@@ -1,4 +1,3 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public record Started : public record Started;
INotification;
+4 -3
View File
@@ -2,12 +2,13 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public partial class ValueViewModel<TValue>(IServiceProvider serviceProvider, public partial class ValueViewModel<TValue>(IServiceProvider provider,
IServiceFactory serviceFactory, IServiceFactory factory,
IMediator mediator,
IPublisher publisher, IPublisher publisher,
ISubscriber subscriber, ISubscriber subscriber,
IDisposer disposer) : IDisposer disposer) :
ObservableViewModel(serviceProvider, serviceFactory, publisher, subscriber, disposer) ObservableViewModel(provider, factory, mediator, publisher, subscriber, disposer)
{ {
[ObservableProperty] [ObservableProperty]
private TValue? value; private TValue? value;
@@ -6,8 +6,8 @@ namespace Toolkit.UI.Controls.Avalonia;
public class ThemeResources : public class ThemeResources :
Styles Styles
{ {
public ThemeResources(IServiceProvider? serviceProvider = null) public ThemeResources(IServiceProvider? provider = null)
{ {
AvaloniaXamlLoader.Load(serviceProvider, this); AvaloniaXamlLoader.Load(provider, this);
} }
} }