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