diff --git a/Toolkit.Avalonia/ContentTemplate.cs b/Toolkit.Avalonia/ContentTemplate.cs index 86a7144..95b753c 100644 --- a/Toolkit.Avalonia/ContentTemplate.cs +++ b/Toolkit.Avalonia/ContentTemplate.cs @@ -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(); INavigationContext? viewModelContentBinder = provider.GetService(); diff --git a/Toolkit.Foundation/Changed.cs b/Toolkit.Foundation/Changed.cs index 82fd0df..e8a009b 100644 --- a/Toolkit.Foundation/Changed.cs +++ b/Toolkit.Foundation/Changed.cs @@ -1,3 +1,10 @@ namespace Toolkit.Foundation; -public record Changed(TValue? Value = default) : INotification; \ No newline at end of file +public record Changed(TValue? Value = default); + +public record Changed +{ + public static Changed As(TValue value) => new(value); + + public static Changed As() where TValue : new() => new(new TValue()); +} \ No newline at end of file diff --git a/Toolkit.Foundation/CommandValueViewModel.cs b/Toolkit.Foundation/CommandValueViewModel.cs index f2b7717..dd8bbb4 100644 --- a/Toolkit.Foundation/CommandValueViewModel.cs +++ b/Toolkit.Foundation/CommandValueViewModel.cs @@ -2,12 +2,13 @@ namespace Toolkit.Foundation; -public partial class CommandValueViewModel(IServiceProvider serviceProvider, - IServiceFactory serviceFactory, +public partial class CommandValueViewModel(IServiceProvider provider, + IServiceFactory factory, + IMediator mediator, IPublisher publisher, ISubscriber subscriber, IDisposer disposer) : - ValueViewModel(serviceProvider, serviceFactory, publisher, subscriber, disposer) + ValueViewModel(provider, factory, mediator, publisher, subscriber, disposer) { public IRelayCommand InvokeCommand => new AsyncRelayCommand(InvokeAsync); diff --git a/Toolkit.Foundation/CommandViewModel.cs b/Toolkit.Foundation/CommandViewModel.cs index 740f078..20e3596 100644 --- a/Toolkit.Foundation/CommandViewModel.cs +++ b/Toolkit.Foundation/CommandViewModel.cs @@ -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); diff --git a/Toolkit.Foundation/ComponentConfigurationViewModel.cs b/Toolkit.Foundation/ComponentConfigurationViewModel.cs index 4eafe1e..76c09da 100644 --- a/Toolkit.Foundation/ComponentConfigurationViewModel.cs +++ b/Toolkit.Foundation/ComponentConfigurationViewModel.cs @@ -8,14 +8,15 @@ public partial class ComponentConfigurationViewModel> 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(IServiceProvider serviceProvider, - IServiceFactory serviceFactory, +public partial class ComponentConfigurationViewModel(IServiceProvider provider, + IServiceFactory factory, + IMediator mediator, IPublisher publisher, ISubscriber subscriber, IDisposer disposer, @@ -37,7 +39,7 @@ public partial class ComponentConfigurationViewModel valueDelegate, object header, object description) : - ValueViewModel(serviceProvider, serviceFactory, publisher, subscriber, disposer), + ValueViewModel(provider, factory, mediator, publisher, subscriber, disposer), IComponentConfigurationViewModel, INotificationHandler> where TConfiguration : class @@ -68,8 +70,9 @@ public partial class ComponentConfigurationViewModel(IServiceProvider serviceProvider, - IServiceFactory serviceFactory, +public partial class ComponentConfigurationViewModel(IServiceProvider provider, + IServiceFactory factory, + IMediator mediator, IPublisher publisher, ISubscriber subscriber, IDisposer disposer, @@ -78,7 +81,7 @@ public partial class ComponentConfigurationViewModel valueDelegate, object header) : - ValueViewModel(serviceProvider, serviceFactory, publisher, subscriber, disposer), + ValueViewModel(provider, factory, mediator, publisher, subscriber, disposer), IComponentConfigurationViewModel, INotificationHandler> where TConfiguration : class diff --git a/Toolkit.Foundation/ComponentHost.cs b/Toolkit.Foundation/ComponentHost.cs index f660820..907a694 100644 --- a/Toolkit.Foundation/ComponentHost.cs +++ b/Toolkit.Foundation/ComponentHost.cs @@ -3,7 +3,7 @@ using Microsoft.Extensions.Hosting; namespace Toolkit.Foundation; -public sealed class ComponentHost(IServiceProvider services, +public class ComponentHost(IServiceProvider services, IEnumerable initializers, IEnumerable hostedServices) : IComponentHost diff --git a/Toolkit.Foundation/Create.cs b/Toolkit.Foundation/Create.cs index 45b2bb9..667aa9a 100644 --- a/Toolkit.Foundation/Create.cs +++ b/Toolkit.Foundation/Create.cs @@ -1,12 +1,13 @@  namespace Toolkit.Foundation; -public record Create(TValue Value) : - INotification; +public record Create(TValue Value); public record Create { - public static Create As(TValue value) => new(value); + public static Create As(TValue value) => + new(value); - public static Create As() where TValue : new() => new(new TValue()); + public static Create As() where TValue : new() => + new(new TValue()); } \ No newline at end of file diff --git a/Toolkit.Foundation/Enumerate.cs b/Toolkit.Foundation/Enumerate.cs index 14d385f..63aebe8 100644 --- a/Toolkit.Foundation/Enumerate.cs +++ b/Toolkit.Foundation/Enumerate.cs @@ -1,3 +1,3 @@ namespace Toolkit.Foundation; -public record Enumerate(object? Key = null) : INotification; \ No newline at end of file +public record Enumerate(object? Key = null); \ No newline at end of file diff --git a/Toolkit.Foundation/HandlerDelegate.cs b/Toolkit.Foundation/HandlerDelegate.cs index 73ce94b..455c50c 100644 --- a/Toolkit.Foundation/HandlerDelegate.cs +++ b/Toolkit.Foundation/HandlerDelegate.cs @@ -1,5 +1,4 @@ namespace Toolkit.Foundation; -public delegate Task HandlerDelegate(TMessage message, - CancellationToken cancellationToken) - where TMessage : IMessage; \ No newline at end of file +public delegate Task HandlerDelegate(TRequest request, + CancellationToken cancellationToken); \ No newline at end of file diff --git a/Toolkit.Foundation/HandlerWrapper.cs b/Toolkit.Foundation/HandlerWrapper.cs index f5ba764..9cc8ae0 100644 --- a/Toolkit.Foundation/HandlerWrapper.cs +++ b/Toolkit.Foundation/HandlerWrapper.cs @@ -1,25 +1,25 @@ namespace Toolkit.Foundation; -public class HandlerWrapper(IHandler handler, - IEnumerable> pipelineBehaviours) - where TMessage : class, IRequest +public class HandlerWrapper(IHandler handler, + IEnumerable> pipelineBehaviours) + where TRequest : class { - private readonly IEnumerable> pipelineBehaviours = + private readonly IEnumerable> pipelineBehaviours = pipelineBehaviours.Reverse(); - public async Task Handle(TMessage message, + public async Task Handle(TRequest request, CancellationToken cancellationToken) { - HandlerDelegate currentHandler = handler.Handle; - foreach (IPipelineBehavior behavior in pipelineBehaviours) + HandlerDelegate currentHandler = handler.Handle; + foreach (IPipelineBehaviour behaviour in pipelineBehaviours) { - HandlerDelegate previousHandler = currentHandler; + HandlerDelegate 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); } } \ No newline at end of file diff --git a/Toolkit.Foundation/IHandler.cs b/Toolkit.Foundation/IHandler.cs index 9bfb4ee..2263715 100644 --- a/Toolkit.Foundation/IHandler.cs +++ b/Toolkit.Foundation/IHandler.cs @@ -4,14 +4,10 @@ public interface IHandler; public interface IHandler : IHandler - where TRequest : - IRequest { Task Handle(TRequest args, CancellationToken cancellationToken); } public interface IHandler : - IHandler - where TRequest : - IRequest; \ No newline at end of file + IHandler; \ No newline at end of file diff --git a/Toolkit.Foundation/IMediator.cs b/Toolkit.Foundation/IMediator.cs index 74fcb71..e87d071 100644 --- a/Toolkit.Foundation/IMediator.cs +++ b/Toolkit.Foundation/IMediator.cs @@ -2,9 +2,10 @@ public interface IMediator { - Task SendAsync(IRequest request, - CancellationToken cancellationToken = default); + Task Handle(TRequest request, + CancellationToken cancellationToken = default) + where TRequest : notnull; - Task SendAsync(object message, CancellationToken + Task Handle(object request, CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/Toolkit.Foundation/INotification.cs b/Toolkit.Foundation/INotification.cs deleted file mode 100644 index 1bc2226..0000000 --- a/Toolkit.Foundation/INotification.cs +++ /dev/null @@ -1,4 +0,0 @@ -namespace Toolkit.Foundation; - -public interface INotification : - IMessage; \ No newline at end of file diff --git a/Toolkit.Foundation/INotificationHandler.cs b/Toolkit.Foundation/INotificationHandler.cs index abe34bd..3da0f29 100644 --- a/Toolkit.Foundation/INotificationHandler.cs +++ b/Toolkit.Foundation/INotificationHandler.cs @@ -1,10 +1,8 @@ namespace Toolkit.Foundation; -public interface INotificationHandler : +public interface INotificationHandler : IHandler - where TNotification : - INotification { - Task Handle(TNotification args, + Task Handle(TMessage args, CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/Toolkit.Foundation/IObservableViewModel.cs b/Toolkit.Foundation/IObservableViewModel.cs index 7d688f4..333fa16 100644 --- a/Toolkit.Foundation/IObservableViewModel.cs +++ b/Toolkit.Foundation/IObservableViewModel.cs @@ -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; } } \ No newline at end of file diff --git a/Toolkit.Foundation/IPipelineBehavior.cs b/Toolkit.Foundation/IPipelineBehavior.cs index 7bd8b32..9f4999e 100644 --- a/Toolkit.Foundation/IPipelineBehavior.cs +++ b/Toolkit.Foundation/IPipelineBehavior.cs @@ -1,17 +1,15 @@ namespace Toolkit.Foundation; -public interface IPipelineBehavior - where TMessage : IMessage +public interface IPipelineBehaviour { Task Handle(TMessage message, HandlerDelegate next, CancellationToken cancellationToken = default); } -public interface IPipelineBehavior - where TNotification : INotification +public interface IPipelineBehaviour { - Task Handle(TNotification notification, - NotificationHandlerDelegate next, + Task Handle(TMessage message, + NotificationHandlerDelegate next, CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/Toolkit.Foundation/IPublisher.cs b/Toolkit.Foundation/IPublisher.cs index 6289413..4f0feec 100644 --- a/Toolkit.Foundation/IPublisher.cs +++ b/Toolkit.Foundation/IPublisher.cs @@ -2,57 +2,44 @@ public interface IPublisher { - public Task Publish(object key, + public Task Publish(object key, CancellationToken cancellationToken = default) - where TNotification : - INotification, - new(); + where TMessage : new(); - public Task Publish(TNotification notification, + public Task Publish(TMessage message, CancellationToken cancellationToken = default) - where TNotification : - INotification; + where TMessage : notnull; - public Task Publish(TNotification notification, + public Task Publish(TMessage message, object key, CancellationToken cancellationToken = default) - where TNotification : - INotification; + where TMessage : notnull; - Task PublishUI(TNotification notification, + Task PublishUI(TMessage message, object key, CancellationToken cancellationToken = default) - where TNotification : - INotification; - - Task PublishUI(object key, + where TMessage : notnull; + Task PublishUI(object key, CancellationToken cancellationToken = default) - where TNotification : - INotification, - new(); + where TMessage : new(); - Task PublishUI(TNotification notification, + Task PublishUI(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, Task> marshal, object? key = null, CancellationToken cancellationToken = default); - Task PublishUIAsync(CancellationToken cancellationToken = default) - where TNotification : - INotification, - new(); + Task PublishUI(CancellationToken cancellationToken = default) + where TMessage : new(); - Task Publish(CancellationToken cancellationToken = default) - where TNotification : - INotification, - new(); + Task Publish(CancellationToken cancellationToken = default) + where TMessage : new(); - public Task Publish(object notification, CancellationToken cancellationToken = default); + public Task Publish(object message, CancellationToken cancellationToken = default); } diff --git a/Toolkit.Foundation/IServiceCollectionExtensions.cs b/Toolkit.Foundation/IServiceCollectionExtensions.cs index e067f42..20eeebb 100644 --- a/Toolkit.Foundation/IServiceCollectionExtensions.cs +++ b/Toolkit.Foundation/IServiceCollectionExtensions.cs @@ -178,7 +178,7 @@ public static class IServiceCollectionExtensions services.Add(new ServiceDescriptor(wrapperType, provider => provider.GetService()?.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()?.Create(wrapperType, provider.GetRequiredService(), - provider.GetServices(typeof(IPipelineBehavior<,>) + provider.GetServices(typeof(IPipelineBehaviour<,>) .MakeGenericType(requestType, responseType)))!, lifetime)); } } diff --git a/Toolkit.Foundation/Initialized.cs b/Toolkit.Foundation/Initialized.cs new file mode 100644 index 0000000..6302662 --- /dev/null +++ b/Toolkit.Foundation/Initialized.cs @@ -0,0 +1,10 @@ +namespace Toolkit.Foundation; + +public record Initialized(TValue Value); + +public record Initialized +{ + public static Initialized As(TValue value) => new(value); + + public static Initialized As() where TValue : new() => new(new TValue()); +} \ No newline at end of file diff --git a/Toolkit.Foundation/Insert.cs b/Toolkit.Foundation/Insert.cs index e97b8eb..a5ac4d7 100644 --- a/Toolkit.Foundation/Insert.cs +++ b/Toolkit.Foundation/Insert.cs @@ -1,3 +1,12 @@ namespace Toolkit.Foundation; -public record Insert(int Index, TValue Value) : INotification; +public record Insert(int Index, TValue Value); + +public record Insert +{ + public static Insert As(int index, TValue value) => + new(index, value); + + public static Insert As(int index) where TValue : new() => + new(index, new TValue()); +} diff --git a/Toolkit.Foundation/Mediator.cs b/Toolkit.Foundation/Mediator.cs index c710ed3..f6d649d 100644 --- a/Toolkit.Foundation/Mediator.cs +++ b/Toolkit.Foundation/Mediator.cs @@ -5,8 +5,9 @@ namespace Toolkit.Foundation; public class Mediator(IServiceProvider provider) : IMediator { - public Task SendAsync(IRequest request, + public Task Handle(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(default); } - public Task SendAsync(object message, + public Task Handle(object message, CancellationToken cancellationToken = default) { if (message.GetType().GetInterface(typeof(IRequest<>).Name) is Type requestType && diff --git a/Toolkit.Foundation/Move.cs b/Toolkit.Foundation/Move.cs index f3c66d9..67e4d77 100644 --- a/Toolkit.Foundation/Move.cs +++ b/Toolkit.Foundation/Move.cs @@ -1,3 +1,12 @@ namespace Toolkit.Foundation; -public record Move(int Index, TValue Value) : INotification; \ No newline at end of file +public record Move(int Index, TValue Value); + +public record Move +{ + public static Move As(int index, TValue value) => + new(index, value); + + public static Insert As(int index) where TValue : new() => + new(index, new TValue()); +} \ No newline at end of file diff --git a/Toolkit.Foundation/Navigate.cs b/Toolkit.Foundation/Navigate.cs index 30dce1c..9b889c2 100644 --- a/Toolkit.Foundation/Navigate.cs +++ b/Toolkit.Foundation/Navigate.cs @@ -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(object Context, object Template, object Content, object? Sender = null, - object[]? Parameters = null) : - INotification; \ No newline at end of file + object[]? Parameters = null); \ No newline at end of file diff --git a/Toolkit.Foundation/NavigateBack.cs b/Toolkit.Foundation/NavigateBack.cs index 25be518..224de35 100644 --- a/Toolkit.Foundation/NavigateBack.cs +++ b/Toolkit.Foundation/NavigateBack.cs @@ -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(object? Context) : - INotification; +public record NavigateBack(object? Context); diff --git a/Toolkit.Foundation/NavigationChanged.cs b/Toolkit.Foundation/NavigationChanged.cs index 6545fd0..b6df381 100644 --- a/Toolkit.Foundation/NavigationChanged.cs +++ b/Toolkit.Foundation/NavigationChanged.cs @@ -1,4 +1,3 @@ namespace Toolkit.Foundation; -public record NavigationChanged(TValue? Value) : - INotification; \ No newline at end of file +public record NavigationChanged(TValue? Value); \ No newline at end of file diff --git a/Toolkit.Foundation/NavigationScope.cs b/Toolkit.Foundation/NavigationScope.cs index 374cb41..5748090 100644 --- a/Toolkit.Foundation/NavigationScope.cs +++ b/Toolkit.Foundation/NavigationScope.cs @@ -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(), .. mappedParameters ?? Enumerable.Empty()]; - 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) { diff --git a/Toolkit.Foundation/NotificationHandlerDelegate.cs b/Toolkit.Foundation/NotificationHandlerDelegate.cs index 2237a3a..01c0dea 100644 --- a/Toolkit.Foundation/NotificationHandlerDelegate.cs +++ b/Toolkit.Foundation/NotificationHandlerDelegate.cs @@ -1,5 +1,4 @@ namespace Toolkit.Foundation; -public delegate Task NotificationHandlerDelegate(TNotification notification, - CancellationToken cancellationToken) - where TNotification : INotification; +public delegate Task NotificationHandlerDelegate(TMessage message, + CancellationToken cancellationToken); diff --git a/Toolkit.Foundation/NotificationHandlerWrapper.cs b/Toolkit.Foundation/NotificationHandlerWrapper.cs index 100165a..91a67c4 100644 --- a/Toolkit.Foundation/NotificationHandlerWrapper.cs +++ b/Toolkit.Foundation/NotificationHandlerWrapper.cs @@ -1,25 +1,24 @@ namespace Toolkit.Foundation; -public class NotificationHandlerWrapper(INotificationHandler handler, - IEnumerable> pipelineBehaviours) - where TNotification : INotification +public class NotificationHandlerWrapper(INotificationHandler handler, + IEnumerable> pipelineBehaviours) { - private readonly IEnumerable> pipelineBehaviours = + private readonly IEnumerable> pipelineBehaviours = pipelineBehaviours.Reverse(); - public async Task Handle(TNotification notification, + public async Task Handle(TMessage message, CancellationToken cancellationToken) { - NotificationHandlerDelegate currentHandler = handler.Handle; - foreach (IPipelineBehavior behavior in pipelineBehaviours) + NotificationHandlerDelegate currentHandler = handler.Handle; + foreach (IPipelineBehaviour behaviour in pipelineBehaviours) { - NotificationHandlerDelegate previousHandler = currentHandler; + NotificationHandlerDelegate 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); } } diff --git a/Toolkit.Foundation/ObservableCollectionViewModel.cs b/Toolkit.Foundation/ObservableCollectionViewModel.cs index 81c2712..7ef445d 100644 --- a/Toolkit.Foundation/ObservableCollectionViewModel.cs +++ b/Toolkit.Foundation/ObservableCollectionViewModel.cs @@ -30,14 +30,16 @@ public partial class ObservableCollectionViewModel : [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 : collection.CollectionChanged += OnCollectionChanged; } - public ObservableCollectionViewModel(IServiceProvider serviceProvider, - IServiceFactory serviceFactory, + public ObservableCollectionViewModel(IServiceProvider provider, + IServiceFactory factory, + IMediator mediator, IPublisher publisher, ISubscriber subscriber, IDisposer disposer, IEnumerable items) { - ServiceProvider = serviceProvider; - ServiceFactory = serviceFactory; + Provider = provider; + Factory = factory; + Mediator = mediator; Publisher = publisher; Disposer = disposer; @@ -72,6 +76,8 @@ public partial class ObservableCollectionViewModel : public IDisposer Disposer { get; private set; } + public IServiceFactory Factory { get; private set; } + bool IList.IsFixedSize => false; bool ICollection.IsReadOnly => false; @@ -80,12 +86,12 @@ public partial class ObservableCollectionViewModel : 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 : public TViewModel Add() { - TViewModel? item = ServiceFactory.Create(); + TViewModel? item = Factory.Create(); Add(item); return item; @@ -128,7 +134,7 @@ public partial class ObservableCollectionViewModel : public TViewModel Add(params object?[] parameters) where T : TViewModel { - T? item = ServiceFactory.Create(parameters); + T? item = Factory.Create(parameters); Add(item); return item; @@ -138,7 +144,7 @@ public partial class ObservableCollectionViewModel : where T : TViewModel { - T? item = ServiceFactory.Create(); + T? item = Factory.Create(); Add(item); return item; @@ -407,9 +413,10 @@ public partial class ObservableCollectionViewModel : 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(serviceProvider, serviceFactory, publisher, subscriber, disposer); \ No newline at end of file + ObservableCollectionViewModel(provider, factory, mediator, publisher, subscriber, disposer); \ No newline at end of file diff --git a/Toolkit.Foundation/ObservableViewModel.cs b/Toolkit.Foundation/ObservableViewModel.cs index 2f73260..87f0ec8 100644 --- a/Toolkit.Foundation/ObservableViewModel.cs +++ b/Toolkit.Foundation/ObservableViewModel.cs @@ -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; diff --git a/Toolkit.Foundation/Publisher.cs b/Toolkit.Foundation/Publisher.cs index 12622bc..172608c 100644 --- a/Toolkit.Foundation/Publisher.cs +++ b/Toolkit.Foundation/Publisher.cs @@ -8,32 +8,31 @@ public class Publisher(ISubscriptionManager subscriptionManager, IDispatcher dispatcher) : IPublisher { - public Task Publish(object key, + public Task Publish(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 notification, + public Task Publish(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 notification, + public Task Publish(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, Task> marshal, object? key = null, CancellationToken cancellationToken = default) { - Type notificationType = notification.GetType(); + Type notificationType = message.GetType(); List 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(CancellationToken cancellationToken = default) - where TNotification : - INotification, new() => Publish(new TNotification(), - async args => await args(), + public Task Publish(CancellationToken cancellationToken = default) + where TMessage : new() => + Publish(new TMessage(), async args => await args(), null, cancellationToken); - public Task PublishUI(object key, + public Task PublishUI(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 notification, + public Task PublishUI(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 notification, + public Task PublishUI(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(CancellationToken cancellationToken = default) - where TNotification : - INotification, new() => Publish(new TNotification(), - args => dispatcher.InvokeAsync(async () => await args()), + public Task PublishUI(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); } diff --git a/Toolkit.Foundation/Remove.cs b/Toolkit.Foundation/Remove.cs index 54d190d..4b63bd7 100644 --- a/Toolkit.Foundation/Remove.cs +++ b/Toolkit.Foundation/Remove.cs @@ -1,5 +1,13 @@  namespace Toolkit.Foundation; -public record Remove(TValue Value) : - INotification; \ No newline at end of file +public record Remove(TValue Value); + +public record Remove +{ + public static Remove As(TValue value) => + new(value); + + public static Remove As() where TValue : new() => + new(new TValue()); +} \ No newline at end of file diff --git a/Toolkit.Foundation/Replace.cs b/Toolkit.Foundation/Replace.cs index 6441149..919c49e 100644 --- a/Toolkit.Foundation/Replace.cs +++ b/Toolkit.Foundation/Replace.cs @@ -1,6 +1,13 @@  namespace Toolkit.Foundation; -public record Replace(int Index, TValue Value, object? Target = null) : - INotification; +public record Replace(int Index, TValue Value); +public record Replace +{ + public static Replace As(int index, TValue value) => + new(index, value); + + public static Replace As(int index) where TValue : new() => + new(index, new TValue()); +} \ No newline at end of file diff --git a/Toolkit.Foundation/Request.cs b/Toolkit.Foundation/Request.cs index a5d5fb7..c7f50b9 100644 --- a/Toolkit.Foundation/Request.cs +++ b/Toolkit.Foundation/Request.cs @@ -1,12 +1,8 @@ namespace Toolkit.Foundation; -public record Request : - INotification -{ - -} +public record Request; public class Request { - public static Request Create() => new(); + public static Request As() => new(); } \ No newline at end of file diff --git a/Toolkit.Foundation/Response.cs b/Toolkit.Foundation/Response.cs index f1a7248..8197b75 100644 --- a/Toolkit.Foundation/Response.cs +++ b/Toolkit.Foundation/Response.cs @@ -1,4 +1,3 @@ namespace Toolkit.Foundation; -public record Response(TValue Value) : - INotification; \ No newline at end of file +public record Response(TValue Value); \ No newline at end of file diff --git a/Toolkit.Foundation/ServiceScopeFactory.cs b/Toolkit.Foundation/ServiceScopeFactory.cs index e8b114e..c86aec1 100644 --- a/Toolkit.Foundation/ServiceScopeFactory.cs +++ b/Toolkit.Foundation/ServiceScopeFactory.cs @@ -11,9 +11,9 @@ public class ServiceScopeFactory(IServiceScopeFactory serviceScopeFact { if (serviceScopeFactory.CreateScope() is IServiceScope serviceScope) { - if (serviceScope.ServiceProvider.GetService() is IServiceFactory serviceFactory) + if (serviceScope.ServiceProvider.GetService() is IServiceFactory factory) { - if (serviceFactory.Create(parameters) is TService service) + if (factory.Create(parameters) is TService service) { cache.Add(service, serviceScope); return service; diff --git a/Toolkit.Foundation/Started.cs b/Toolkit.Foundation/Started.cs index 9180311..3df6262 100644 --- a/Toolkit.Foundation/Started.cs +++ b/Toolkit.Foundation/Started.cs @@ -1,4 +1,3 @@ namespace Toolkit.Foundation; -public record Started : - INotification; \ No newline at end of file +public record Started; \ No newline at end of file diff --git a/Toolkit.Foundation/ValueViewModel.cs b/Toolkit.Foundation/ValueViewModel.cs index da03c02..a43ba96 100644 --- a/Toolkit.Foundation/ValueViewModel.cs +++ b/Toolkit.Foundation/ValueViewModel.cs @@ -2,12 +2,13 @@ namespace Toolkit.Foundation; -public partial class ValueViewModel(IServiceProvider serviceProvider, - IServiceFactory serviceFactory, +public partial class ValueViewModel(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; diff --git a/Toolkit.UI.Controls.Avalonia/Themes/ThemeResources.axaml.cs b/Toolkit.UI.Controls.Avalonia/Themes/ThemeResources.axaml.cs index 0f3df7f..6989d3e 100644 --- a/Toolkit.UI.Controls.Avalonia/Themes/ThemeResources.axaml.cs +++ b/Toolkit.UI.Controls.Avalonia/Themes/ThemeResources.axaml.cs @@ -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); } }