From 5a98b67dcfa375f2f450d8b469b2659e3942261c Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Sun, 9 Jun 2024 22:41:58 +0100 Subject: [PATCH] Some refactoring --- Toolkit.Foundation/Activate.cs | 6 +- Toolkit.Foundation/ActivateEventArgs.cs | 2 +- Toolkit.Foundation/Activated.cs | 4 +- Toolkit.Foundation/ActivatedEventArgs.cs | 2 +- Toolkit.Foundation/Cancel.cs | 6 +- Toolkit.Foundation/CancelEventArgs.cs | 2 +- Toolkit.Foundation/Changed.cs | 4 +- Toolkit.Foundation/ChangedEventArgs.cs | 2 +- .../ComponentConfigurationViewModel.cs | 4 +- .../ConfigurationChangedHandler.cs | 2 +- Toolkit.Foundation/Create.cs | 8 +-- Toolkit.Foundation/CreateEventArgs.cs | 3 +- Toolkit.Foundation/Created.cs | 8 +-- Toolkit.Foundation/CreatedEventArgs.cs | 2 +- Toolkit.Foundation/Deactivated.cs | 4 +- Toolkit.Foundation/DeactivatedEventArgs.cs | 2 +- Toolkit.Foundation/Delete.cs | 8 +-- Toolkit.Foundation/DeleteEventArgs.cs | 2 +- Toolkit.Foundation/IMediator.cs | 6 +- Toolkit.Foundation/Mediator.cs | 56 ++++++++---------- Toolkit.Foundation/Move.cs | 6 +- Toolkit.Foundation/MoveEventArgs.cs | 2 +- Toolkit.Foundation/MoveToEventArgs.cs | 2 +- Toolkit.Foundation/NavigationScope.cs | 33 ++++++++--- Toolkit.Foundation/Notify.cs | 6 +- Toolkit.Foundation/NotifyEventArgs.cs | 2 +- Toolkit.Foundation/ObservableCollection.cs | 17 ++++-- Toolkit.Foundation/Remove.cs | 6 +- Toolkit.Foundation/RemoveEventArgs.cs | 4 +- Toolkit.Foundation/Replace.cs | 6 +- Toolkit.Foundation/ReplaceEventArgs.cs | 2 +- Toolkit.Foundation/RequestEventArgs.cs | 2 +- Toolkit.Foundation/Synchronize.cs | 3 +- Toolkit.Foundation/TupleExtensions.cs | 59 +++++++++++++++++++ 34 files changed, 177 insertions(+), 106 deletions(-) create mode 100644 Toolkit.Foundation/TupleExtensions.cs diff --git a/Toolkit.Foundation/Activate.cs b/Toolkit.Foundation/Activate.cs index 51d6d4c..15a20be 100644 --- a/Toolkit.Foundation/Activate.cs +++ b/Toolkit.Foundation/Activate.cs @@ -2,7 +2,7 @@ public record Activate { - public static ActivateEventArgs As(TValue value) => new(value); + public static ActivateEventArgs As(TSender sender) => new(sender); - public static ActivateEventArgs As() where TValue : new() => new(new TValue()); -} \ No newline at end of file + public static ActivateEventArgs As() where TSender : new() => new(new TSender()); +} diff --git a/Toolkit.Foundation/ActivateEventArgs.cs b/Toolkit.Foundation/ActivateEventArgs.cs index d4cd45e..dfe5d3e 100644 --- a/Toolkit.Foundation/ActivateEventArgs.cs +++ b/Toolkit.Foundation/ActivateEventArgs.cs @@ -1,3 +1,3 @@ namespace Toolkit.Foundation; -public record ActivateEventArgs(TValue? Value = default); \ No newline at end of file +public record ActivateEventArgs(TSender? Sender = default); \ No newline at end of file diff --git a/Toolkit.Foundation/Activated.cs b/Toolkit.Foundation/Activated.cs index b11a16e..adfb47e 100644 --- a/Toolkit.Foundation/Activated.cs +++ b/Toolkit.Foundation/Activated.cs @@ -2,7 +2,7 @@ public record Activated { - public static ActivatedEventArgs As(TValue value) => new(value); + public static ActivatedEventArgs As(TSender sender) => new(sender); - public static ActivatedEventArgs As() where TValue : new() => new(new TValue()); + public static ActivatedEventArgs As() where TSender : new() => new(new TSender()); } \ No newline at end of file diff --git a/Toolkit.Foundation/ActivatedEventArgs.cs b/Toolkit.Foundation/ActivatedEventArgs.cs index 3d2a526..99fcacf 100644 --- a/Toolkit.Foundation/ActivatedEventArgs.cs +++ b/Toolkit.Foundation/ActivatedEventArgs.cs @@ -1,3 +1,3 @@ namespace Toolkit.Foundation; -public record ActivatedEventArgs(TValue? Value = default); \ No newline at end of file +public record ActivatedEventArgs(TSender? Sender = default); \ No newline at end of file diff --git a/Toolkit.Foundation/Cancel.cs b/Toolkit.Foundation/Cancel.cs index 16c479a..1bfb291 100644 --- a/Toolkit.Foundation/Cancel.cs +++ b/Toolkit.Foundation/Cancel.cs @@ -2,9 +2,7 @@ public record Cancel { - public static CancelEventArgs As(TValue value) => - new(value); + public static CancelEventArgs As(TSender sender) => new(sender); - public static CancelEventArgs As() where TValue : new() => - new(new TValue()); + public static CancelEventArgs As() where TSender : new() => new(new TSender()); } \ No newline at end of file diff --git a/Toolkit.Foundation/CancelEventArgs.cs b/Toolkit.Foundation/CancelEventArgs.cs index 906ca41..46867e9 100644 --- a/Toolkit.Foundation/CancelEventArgs.cs +++ b/Toolkit.Foundation/CancelEventArgs.cs @@ -1,3 +1,3 @@ namespace Toolkit.Foundation; -public record CancelEventArgs(TValue Value); \ No newline at end of file +public record CancelEventArgs(TSender sender); \ No newline at end of file diff --git a/Toolkit.Foundation/Changed.cs b/Toolkit.Foundation/Changed.cs index be8dda9..c8f538b 100644 --- a/Toolkit.Foundation/Changed.cs +++ b/Toolkit.Foundation/Changed.cs @@ -2,7 +2,7 @@ public record Changed { - public static ChangedEventArgs As(TValue value) => new(value); + public static ChangedEventArgs As(TSender sender) => new(sender); - public static ChangedEventArgs As() where TValue : new() => new(new TValue()); + public static ChangedEventArgs As() where TSender : new() => new(new TSender()); } \ No newline at end of file diff --git a/Toolkit.Foundation/ChangedEventArgs.cs b/Toolkit.Foundation/ChangedEventArgs.cs index 0376401..5e6c1c7 100644 --- a/Toolkit.Foundation/ChangedEventArgs.cs +++ b/Toolkit.Foundation/ChangedEventArgs.cs @@ -1,3 +1,3 @@ namespace Toolkit.Foundation; -public record ChangedEventArgs(TValue? Value = default); \ No newline at end of file +public record ChangedEventArgs(TSender? Sender = default); \ No newline at end of file diff --git a/Toolkit.Foundation/ComponentConfigurationViewModel.cs b/Toolkit.Foundation/ComponentConfigurationViewModel.cs index 888d7c1..d9a810b 100644 --- a/Toolkit.Foundation/ComponentConfigurationViewModel.cs +++ b/Toolkit.Foundation/ComponentConfigurationViewModel.cs @@ -59,7 +59,7 @@ public partial class ComponentConfigurationViewModel args) { - if (args.Value is TConfiguration configuration) + if (args.Sender is TConfiguration configuration) { Value = valueDelegate.Invoke(configuration); } @@ -101,7 +101,7 @@ public partial class ComponentConfigurationViewModel args) { - if (args.Value is TConfiguration configuration) + if (args.Sender is TConfiguration configuration) { Value = valueDelegate.Invoke(configuration); } diff --git a/Toolkit.Foundation/ConfigurationChangedHandler.cs b/Toolkit.Foundation/ConfigurationChangedHandler.cs index 1244aa6..1c343bc 100644 --- a/Toolkit.Foundation/ConfigurationChangedHandler.cs +++ b/Toolkit.Foundation/ConfigurationChangedHandler.cs @@ -7,7 +7,7 @@ public class ConfigurationChangedHandler(ConfigurationVa { public Task Handle(ChangedEventArgs args) { - if (args.Value is TConfiguration configuration) + if (args.Sender is TConfiguration configuration) { if (configurationValue.TryUpdate(configuration, out TValue value)) { diff --git a/Toolkit.Foundation/Create.cs b/Toolkit.Foundation/Create.cs index 72b7fea..0973a5d 100644 --- a/Toolkit.Foundation/Create.cs +++ b/Toolkit.Foundation/Create.cs @@ -2,9 +2,9 @@ public record Create { - public static CreateEventArgs As(TValue value, params object[] Parameters) => - new(value); + public static CreateEventArgs As(TSender sender, params object[] parameters) => + new(sender, parameters); - public static CreateEventArgs As(params object[] Parameters) where TValue : new() => - new(new TValue()); + public static CreateEventArgs As(params object[] parameters) where TSender : new() => + new(new TSender(), parameters); } \ No newline at end of file diff --git a/Toolkit.Foundation/CreateEventArgs.cs b/Toolkit.Foundation/CreateEventArgs.cs index d83845a..170b59e 100644 --- a/Toolkit.Foundation/CreateEventArgs.cs +++ b/Toolkit.Foundation/CreateEventArgs.cs @@ -1,3 +1,4 @@ namespace Toolkit.Foundation; -public record CreateEventArgs(TValue Value, params object[] Parameters); \ No newline at end of file +public record CreateEventArgs(TSender? Sender = default, + params object[] Parameters); \ No newline at end of file diff --git a/Toolkit.Foundation/Created.cs b/Toolkit.Foundation/Created.cs index 42bc553..2a96ff6 100644 --- a/Toolkit.Foundation/Created.cs +++ b/Toolkit.Foundation/Created.cs @@ -2,9 +2,9 @@ public record Created { - public static CreatedEventArgs As(TValue value) => - new(value); + public static CreatedEventArgs As(TSender sender) => + new(sender); - public static CreatedEventArgs As() where TValue : new() => - new(new TValue()); + public static CreatedEventArgs As() where TSender : new() => + new(new TSender()); } \ No newline at end of file diff --git a/Toolkit.Foundation/CreatedEventArgs.cs b/Toolkit.Foundation/CreatedEventArgs.cs index 7a2c4cf..e7fa76b 100644 --- a/Toolkit.Foundation/CreatedEventArgs.cs +++ b/Toolkit.Foundation/CreatedEventArgs.cs @@ -1,3 +1,3 @@ namespace Toolkit.Foundation; -public record CreatedEventArgs(TValue Value); \ No newline at end of file +public record CreatedEventArgs(TSender? Sender = default); \ No newline at end of file diff --git a/Toolkit.Foundation/Deactivated.cs b/Toolkit.Foundation/Deactivated.cs index 3f975bc..aeb3e2c 100644 --- a/Toolkit.Foundation/Deactivated.cs +++ b/Toolkit.Foundation/Deactivated.cs @@ -2,7 +2,7 @@ public record Deactivated { - public static DeactivatedEventArgs As(TValue value) => new(value); + public static DeactivatedEventArgs As(TSender sender) => new(sender); - public static DeactivatedEventArgs As() where TValue : new() => new(new TValue()); + public static DeactivatedEventArgs As() where TSender : new() => new(new TSender()); } \ No newline at end of file diff --git a/Toolkit.Foundation/DeactivatedEventArgs.cs b/Toolkit.Foundation/DeactivatedEventArgs.cs index 917c38b..2efcbd9 100644 --- a/Toolkit.Foundation/DeactivatedEventArgs.cs +++ b/Toolkit.Foundation/DeactivatedEventArgs.cs @@ -1,3 +1,3 @@ namespace Toolkit.Foundation; -public record DeactivatedEventArgs(TValue? Value = default); \ No newline at end of file +public record DeactivatedEventArgs(TSender? Sender = default); \ No newline at end of file diff --git a/Toolkit.Foundation/Delete.cs b/Toolkit.Foundation/Delete.cs index a1a474c..5937231 100644 --- a/Toolkit.Foundation/Delete.cs +++ b/Toolkit.Foundation/Delete.cs @@ -2,9 +2,9 @@ public record Delete { - public static DeleteEventArgs As(TValue value) => - new(value); + public static DeleteEventArgs As(TSender sender) => + new(sender); - public static DeleteEventArgs As() where TValue : new() => - new(new TValue()); + public static DeleteEventArgs As() where TSender : new() => + new(new TSender()); } \ No newline at end of file diff --git a/Toolkit.Foundation/DeleteEventArgs.cs b/Toolkit.Foundation/DeleteEventArgs.cs index 6037a7e..00cce35 100644 --- a/Toolkit.Foundation/DeleteEventArgs.cs +++ b/Toolkit.Foundation/DeleteEventArgs.cs @@ -1,3 +1,3 @@ namespace Toolkit.Foundation; -public record DeleteEventArgs(TValue Value); \ No newline at end of file +public record DeleteEventArgs(TSender? Sender = default); \ No newline at end of file diff --git a/Toolkit.Foundation/IMediator.cs b/Toolkit.Foundation/IMediator.cs index 139a386..a47d276 100644 --- a/Toolkit.Foundation/IMediator.cs +++ b/Toolkit.Foundation/IMediator.cs @@ -2,7 +2,8 @@ public interface IMediator { - Task Handle(object message, + Task Handle(Type responseType, + object message, object? key = null, CancellationToken cancellationToken = default); @@ -11,7 +12,8 @@ public interface IMediator CancellationToken cancellationToken = default) where TMessage : notnull; - IAsyncEnumerable HandleManyAsync(object message, + IAsyncEnumerable HandleManyAsync(Type responseType, + object message, object? key = null, CancellationToken cancellationToken = default); diff --git a/Toolkit.Foundation/Mediator.cs b/Toolkit.Foundation/Mediator.cs index f1e8a45..14e4b2c 100644 --- a/Toolkit.Foundation/Mediator.cs +++ b/Toolkit.Foundation/Mediator.cs @@ -27,41 +27,42 @@ public class Mediator(IHandlerProvider handlerProvider, return default; } - public async Task Handle(object message, + public async Task Handle(Type responseType, + object message, object? key = null, CancellationToken cancellationToken = default) { Type messageType = message.GetType(); - if (messageType.GetInterface(message.GetType().Name) is Type requestType && - requestType.GetGenericArguments().Length == 1) + Type handlerWrapperType = typeof(HandlerWrapper<,>).MakeGenericType(message.GetType(), responseType); + + List handlers = GetHandlers(message, handlerWrapperType, key); + + foreach (object? handler in handlers) { - Type responseType = requestType.GetGenericArguments()[0]; - Type handlerWrapperType = typeof(HandlerWrapper<,>).MakeGenericType(message.GetType(), responseType); - - List handlers = GetHandlers(message, handlerWrapperType, key); - - foreach (object? handler in handlers) + MethodInfo? handleMethod = handler?.GetType().GetMethod("Handle", [messageType, typeof(CancellationToken)]); + if (handleMethod != null) { - MethodInfo? handleMethod = handler?.GetType().GetMethod("Handle", [messageType, typeof(CancellationToken)]); - if (handleMethod != null) - { - return await (Task)handleMethod.Invoke(handler, new object[] { message, cancellationToken })!; - } + dynamic task = handleMethod.Invoke(handler, new object[] { message, cancellationToken })!; + await task; + + return task.Result; } } return default; } - public async Task> HandleMany(object message, + public async Task> HandleMany(Type responseType, + object message, object? key = null, CancellationToken cancellationToken = default) { List responses = []; - await foreach (object? response in HandleManyAsync(message, key, cancellationToken)) + await foreach (object? response in HandleManyAsync(responseType, message, key, cancellationToken)) { responses.Add(response); } + return responses; } @@ -79,26 +80,21 @@ public class Mediator(IHandlerProvider handlerProvider, return responses; } - public async IAsyncEnumerable HandleManyAsync(object message, - object? key = null, + public async IAsyncEnumerable HandleManyAsync(Type responseType, + object message, + object? key = null, [EnumeratorCancellation] CancellationToken cancellationToken = default) { Type messageType = message.GetType(); + Type handlerWrapperType = typeof(HandlerWrapper<,>).MakeGenericType(message.GetType(), responseType); - if (messageType.GetInterface(message.GetType().Name) is Type requestType && - requestType.GetGenericArguments().Length == 1) + List handlers = GetHandlers(message, handlerWrapperType, key); + foreach (object? handler in handlers) { - Type responseType = requestType.GetGenericArguments()[0]; - Type handlerWrapperType = typeof(HandlerWrapper<,>).MakeGenericType(message.GetType(), responseType); - - List handlers = GetHandlers(message, handlerWrapperType, key); - foreach (object? handler in handlers) + MethodInfo? handleMethod = handler?.GetType().GetMethod("Handle", [messageType, typeof(CancellationToken)]); + if (handleMethod != null) { - MethodInfo? handleMethod = handler?.GetType().GetMethod("Handle", [messageType, typeof(CancellationToken)]); - if (handleMethod != null) - { - yield return await (Task)handleMethod.Invoke(handler, new object[] { message, cancellationToken })!; - } + yield return await (Task)handleMethod.Invoke(handler, new object[] { message, cancellationToken })!; } } } diff --git a/Toolkit.Foundation/Move.cs b/Toolkit.Foundation/Move.cs index 684a09e..f1aaf1d 100644 --- a/Toolkit.Foundation/Move.cs +++ b/Toolkit.Foundation/Move.cs @@ -2,9 +2,7 @@ public record Move { - public static MoveEventArgs As(int index, TValue value) => - new(index, value); + public static MoveEventArgs As(int index, TSender sender) => new(index, sender); - public static InsertEventArgs As(int index) where TValue : new() => - new(index, new TValue()); + public static InsertEventArgs As(int index) where TSender : new() => new(index, new TSender()); } \ No newline at end of file diff --git a/Toolkit.Foundation/MoveEventArgs.cs b/Toolkit.Foundation/MoveEventArgs.cs index 877c6cb..c8256d2 100644 --- a/Toolkit.Foundation/MoveEventArgs.cs +++ b/Toolkit.Foundation/MoveEventArgs.cs @@ -1,3 +1,3 @@ namespace Toolkit.Foundation; -public record MoveEventArgs(int Index, TValue Value); \ No newline at end of file +public record MoveEventArgs(int Index, TSender? Sender = default); \ No newline at end of file diff --git a/Toolkit.Foundation/MoveToEventArgs.cs b/Toolkit.Foundation/MoveToEventArgs.cs index 64b22a7..fc5c41c 100644 --- a/Toolkit.Foundation/MoveToEventArgs.cs +++ b/Toolkit.Foundation/MoveToEventArgs.cs @@ -1,3 +1,3 @@ namespace Toolkit.Foundation; -public record MoveToEventArgs(int OldIndex, int NewIndex); \ No newline at end of file +public record MoveToEventArgs(int OldIndex, int NewIndex); \ No newline at end of file diff --git a/Toolkit.Foundation/NavigationScope.cs b/Toolkit.Foundation/NavigationScope.cs index a7e01dc..2c1f9cf 100644 --- a/Toolkit.Foundation/NavigationScope.cs +++ b/Toolkit.Foundation/NavigationScope.cs @@ -3,6 +3,7 @@ namespace Toolkit.Foundation; public class NavigationScope(IPublisher publisher, + IMediator mediator, IServiceProvider provider, IServiceFactory factory, INavigationProvider navigationProvider, @@ -10,7 +11,7 @@ public class NavigationScope(IPublisher publisher, IContentTemplateDescriptorProvider contentTemplateDescriptorProvider) : INavigationScope { - public void Navigate(string route, + public async void Navigate(string route, object? sender = null, object? region = null, EventHandler? navigated = null, @@ -63,16 +64,34 @@ public class NavigationScope(IPublisher publisher, if (region is not null) { - if ((resolvedArguments is { Length: > 0 } - ? factory.Create(descriptor.ContentType, resolvedArguments) - : provider.GetRequiredKeyedService(descriptor.ContentType, segment)) is object viewModel) + Type createEventType = typeof(CreateEventArgs<>).MakeGenericType(descriptor.ContentType); + + object? content = null; + if (Activator.CreateInstance(createEventType, [null, resolvedArguments]) is object createEvent) + { + content = await mediator.Handle(descriptor.ContentType, createEvent, descriptor.Key); + } + + if (content is null) + { + if (resolvedArguments is { Length: > 0 }) + { + content = factory.Create(descriptor.ContentType, resolvedArguments); + } + else + { + content = provider.GetRequiredKeyedService(descriptor.ContentType, segment); + } + } + + if (content is not null) { if (navigationProvider.Get(region is Type type ? type : region.GetType()) is INavigation navigation) { - Type navigateType = typeof(NavigateEventArgs<>).MakeGenericType(navigation.Type); - if (Activator.CreateInstance(navigateType, [region, view, viewModel, sender, parameters]) is object navigate) + Type navigateEventType = typeof(NavigateEventArgs<>).MakeGenericType(navigation.Type); + if (Activator.CreateInstance(navigateEventType, [region, view, content, sender, parameters]) is object navigateEvent) { - publisher.Publish(navigate); + publisher.Publish(navigateEvent); if (currentSegmentIndex == segmentCount) { navigated?.Invoke(this, EventArgs.Empty); diff --git a/Toolkit.Foundation/Notify.cs b/Toolkit.Foundation/Notify.cs index bb99f22..367692c 100644 --- a/Toolkit.Foundation/Notify.cs +++ b/Toolkit.Foundation/Notify.cs @@ -2,9 +2,7 @@ public class Notify { - public static NotifyEventArgs As(TValue value) => - new(value); + public static NotifyEventArgs As(TSender sender) => new(sender); - public static NotifyEventArgs As() where TValue : new() => - new(new TValue()); + public static NotifyEventArgs As() where TSender : new() => new(new TSender()); } \ No newline at end of file diff --git a/Toolkit.Foundation/NotifyEventArgs.cs b/Toolkit.Foundation/NotifyEventArgs.cs index a8effcb..6803ca5 100644 --- a/Toolkit.Foundation/NotifyEventArgs.cs +++ b/Toolkit.Foundation/NotifyEventArgs.cs @@ -1,3 +1,3 @@ namespace Toolkit.Foundation; -public record NotifyEventArgs(TValue Value); \ No newline at end of file +public record NotifyEventArgs(TSender? Sender = default); \ No newline at end of file diff --git a/Toolkit.Foundation/ObservableCollection.cs b/Toolkit.Foundation/ObservableCollection.cs index d4bf388..93f196a 100644 --- a/Toolkit.Foundation/ObservableCollection.cs +++ b/Toolkit.Foundation/ObservableCollection.cs @@ -155,6 +155,12 @@ public partial class ObservableCollection : return item; } + public void Clear(Action> factory) + { + Clear(); + factory.Invoke(this); + } + public void Add(TItem item) { int index = collection.Count; @@ -274,7 +280,7 @@ public partial class ObservableCollection : { foreach (TItem item in this.ToList()) { - if (args.Value is not null && args.Value.Equals(item)) + if (args.Sender is not null && args.Sender.Equals(item)) { Remove(item); } @@ -292,7 +298,7 @@ public partial class ObservableCollection : { if (Activated) { - if (args.Value is TItem item) + if (args.Sender is TItem item) { Add(item); } @@ -340,7 +346,7 @@ public partial class ObservableCollection : { if (Activated) { - if (args.Value is TItem item) + if (args.Sender is TItem item) { Move(args.Index, item); } @@ -357,7 +363,7 @@ public partial class ObservableCollection : { if (Activated) { - if (args.Value is TItem item) + if (args.Sender is TItem item) { Replace(args.Index, item); } @@ -715,7 +721,8 @@ public partial class ObservableCollection : partial void OnValueChanged(TValue value) => OnValueChanged(); } -public class ObservableCollection : ObservableCollection +public class ObservableCollection : + ObservableCollection { public ObservableCollection(IServiceProvider provider, IServiceFactory factory, diff --git a/Toolkit.Foundation/Remove.cs b/Toolkit.Foundation/Remove.cs index 75a5d71..7ce4d60 100644 --- a/Toolkit.Foundation/Remove.cs +++ b/Toolkit.Foundation/Remove.cs @@ -2,9 +2,7 @@ public record Remove { - public static RemoveEventArgs As(TValue value) => - new(value); + public static RemoveEventArgs As(TSender sender) => new(sender); - public static RemoveEventArgs As() where TValue : new() => - new(new TValue()); + public static RemoveEventArgs As() where TSender : new() => new(new TSender()); } \ No newline at end of file diff --git a/Toolkit.Foundation/RemoveEventArgs.cs b/Toolkit.Foundation/RemoveEventArgs.cs index 6ba0b5b..5ba67ac 100644 --- a/Toolkit.Foundation/RemoveEventArgs.cs +++ b/Toolkit.Foundation/RemoveEventArgs.cs @@ -1,5 +1,3 @@ namespace Toolkit.Foundation; -public record RemoveEventArgs(TValue Value); - -public record RemoveAndInsertEventArgs(TValue Value); \ No newline at end of file +public record RemoveEventArgs(TSender? Sender = default); \ No newline at end of file diff --git a/Toolkit.Foundation/Replace.cs b/Toolkit.Foundation/Replace.cs index f47c36f..92372e7 100644 --- a/Toolkit.Foundation/Replace.cs +++ b/Toolkit.Foundation/Replace.cs @@ -2,9 +2,7 @@ public record Replace { - public static ReplaceEventArgs As(int index, TValue value) => - new(index, value); + public static ReplaceEventArgs As(int index, TSender sender) => new(index, sender); - public static ReplaceEventArgs As(int index) where TValue : new() => - new(index, new TValue()); + public static ReplaceEventArgs As(int index) where TSender : new() => new(index, new TSender()); } \ No newline at end of file diff --git a/Toolkit.Foundation/ReplaceEventArgs.cs b/Toolkit.Foundation/ReplaceEventArgs.cs index 8da69c9..91698f4 100644 --- a/Toolkit.Foundation/ReplaceEventArgs.cs +++ b/Toolkit.Foundation/ReplaceEventArgs.cs @@ -1,3 +1,3 @@ namespace Toolkit.Foundation; -public record ReplaceEventArgs(int Index, TValue Value); \ No newline at end of file +public record ReplaceEventArgs(int Index, TSender? Sender = default); \ No newline at end of file diff --git a/Toolkit.Foundation/RequestEventArgs.cs b/Toolkit.Foundation/RequestEventArgs.cs index 109508e..c8d8225 100644 --- a/Toolkit.Foundation/RequestEventArgs.cs +++ b/Toolkit.Foundation/RequestEventArgs.cs @@ -1,3 +1,3 @@ namespace Toolkit.Foundation; -public record RequestEventArgs(TValue Value); +public record RequestEventArgs(TValue? Value = default); diff --git a/Toolkit.Foundation/Synchronize.cs b/Toolkit.Foundation/Synchronize.cs index 93f7e6a..5de0d4d 100644 --- a/Toolkit.Foundation/Synchronize.cs +++ b/Toolkit.Foundation/Synchronize.cs @@ -2,8 +2,7 @@ public record Synchronize { - public static SynchronizeEventArgs As(TOptions options) - where TOptions : class => new(options); + public static SynchronizeEventArgs As(TOptions options) => new(options); public static SynchronizeEventArgs As() => new(); diff --git a/Toolkit.Foundation/TupleExtensions.cs b/Toolkit.Foundation/TupleExtensions.cs new file mode 100644 index 0000000..4aeb138 --- /dev/null +++ b/Toolkit.Foundation/TupleExtensions.cs @@ -0,0 +1,59 @@ +namespace Toolkit.Foundation; + +public static class TupleExtensions +{ + public static (T1, T2) CreateValueTuple(this object[] parameters) => ( + (T1)Convert.ChangeType(parameters[0], typeof(T1)), + (T2)Convert.ChangeType(parameters[1], typeof(T2)) + ); + + public static (T1, T2, T3) CreateValueTuple(this object[] parameters) => ( + (T1)Convert.ChangeType(parameters[0], typeof(T1)), + (T2)Convert.ChangeType(parameters[1], typeof(T2)), + (T3)Convert.ChangeType(parameters[2], typeof(T3)) + ); + + public static (T1, T2, T3, T4) CreateValueTuple(this object[] parameters) => ( + (T1)Convert.ChangeType(parameters[0], typeof(T1)), + (T2)Convert.ChangeType(parameters[1], typeof(T2)), + (T3)Convert.ChangeType(parameters[2], typeof(T3)), + (T4)Convert.ChangeType(parameters[3], typeof(T4)) + ); + public static (T1, T2, T3, T4, T5) CreateValueTuple(this object[] parameters) => ( + (T1)Convert.ChangeType(parameters[0], typeof(T1)), + (T2)Convert.ChangeType(parameters[1], typeof(T2)), + (T3)Convert.ChangeType(parameters[2], typeof(T3)), + (T4)Convert.ChangeType(parameters[3], typeof(T4)), + (T5)Convert.ChangeType(parameters[4], typeof(T5)) + ); + + public static (T1, T2, T3, T4, T5, T6) CreateValueTuple(this object[] parameters) => + ((T1)Convert.ChangeType(parameters[0], typeof(T1)), + (T2)Convert.ChangeType(parameters[1], typeof(T2)), + (T3)Convert.ChangeType(parameters[2], typeof(T3)), + (T4)Convert.ChangeType(parameters[3], typeof(T4)), + (T5)Convert.ChangeType(parameters[4], typeof(T5)), + (T6)Convert.ChangeType(parameters[5], typeof(T6)) + ); + + public static (T1, T2, T3, T4, T5, T6, T7) CreateValueTuple(this object[] parameters) => + ((T1)Convert.ChangeType(parameters[0], typeof(T1)), + (T2)Convert.ChangeType(parameters[1], typeof(T2)), + (T3)Convert.ChangeType(parameters[2], typeof(T3)), + (T4)Convert.ChangeType(parameters[3], typeof(T4)), + (T5)Convert.ChangeType(parameters[4], typeof(T5)), + (T6)Convert.ChangeType(parameters[5], typeof(T6)), + (T7)Convert.ChangeType(parameters[6], typeof(T7)) + ); + + public static (T1, T2, T3, T4, T5, T6, T7, T8) CreateValueTuple(this object[] parameters) => + ((T1)Convert.ChangeType(parameters[0], typeof(T1)), + (T2)Convert.ChangeType(parameters[1], typeof(T2)), + (T3)Convert.ChangeType(parameters[2], typeof(T3)), + (T4)Convert.ChangeType(parameters[3], typeof(T4)), + (T5)Convert.ChangeType(parameters[4], typeof(T5)), + (T6)Convert.ChangeType(parameters[5], typeof(T6)), + (T7)Convert.ChangeType(parameters[6], typeof(T7)), + (T8)Convert.ChangeType(parameters[7], typeof(T8)) + ); +}