diff --git a/Toolkit.Foundation/ComponentBuilder.cs b/Toolkit.Foundation/ComponentBuilder.cs index 21e1cbf..b3dc932 100644 --- a/Toolkit.Foundation/ComponentBuilder.cs +++ b/Toolkit.Foundation/ComponentBuilder.cs @@ -32,9 +32,8 @@ public class ComponentBuilder : services.AddScoped(); services.AddTransient(); - services.AddTransient(); + services.AddScoped(); - services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/Toolkit.Foundation/HandlerProvider.cs b/Toolkit.Foundation/HandlerProvider.cs index caadcde..cadc785 100644 --- a/Toolkit.Foundation/HandlerProvider.cs +++ b/Toolkit.Foundation/HandlerProvider.cs @@ -7,6 +7,7 @@ public class HandlerProvider(SubscriptionCollection subscriptions) : object? key = null) { string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{type}"; + var d = subscriptions; if (subscriptions.TryGetValue(subscriptionKey, out List? subscribers)) { foreach (WeakReference weakRef in subscribers.ToArray()) diff --git a/Toolkit.Foundation/IDisposerRequired.cs b/Toolkit.Foundation/IDisposerRequired.cs new file mode 100644 index 0000000..a5a9cc7 --- /dev/null +++ b/Toolkit.Foundation/IDisposerRequired.cs @@ -0,0 +1,6 @@ +namespace Toolkit.Foundation; + +public interface IDisposerRequired +{ + IDisposer Disposer { get; } +} diff --git a/Toolkit.Foundation/IHandlerProvider.cs b/Toolkit.Foundation/IHandlerProvider.cs index f155b6c..a62ea93 100644 --- a/Toolkit.Foundation/IHandlerProvider.cs +++ b/Toolkit.Foundation/IHandlerProvider.cs @@ -1,9 +1,8 @@  -namespace Toolkit.Foundation +namespace Toolkit.Foundation; + +public interface IHandlerProvider { - public interface IHandlerProvider - { - IEnumerable Get(Type type, - object? key = null); - } + IEnumerable Get(Type type, + object? key = null); } \ No newline at end of file diff --git a/Toolkit.Foundation/IMediatorRequired.cs b/Toolkit.Foundation/IMediatorRequired.cs new file mode 100644 index 0000000..c6c138c --- /dev/null +++ b/Toolkit.Foundation/IMediatorRequired.cs @@ -0,0 +1,6 @@ +namespace Toolkit.Foundation; + +public interface IMediatorRequired +{ + IMediator Mediator { get; } +} diff --git a/Toolkit.Foundation/IPublisherRequired.cs b/Toolkit.Foundation/IPublisherRequired.cs new file mode 100644 index 0000000..beb8728 --- /dev/null +++ b/Toolkit.Foundation/IPublisherRequired.cs @@ -0,0 +1,6 @@ +namespace Toolkit.Foundation; + +public interface IPublisherRequired +{ + IPublisher Publisher { get; } +} diff --git a/Toolkit.Foundation/IServiceFactoryRequired.cs b/Toolkit.Foundation/IServiceFactoryRequired.cs new file mode 100644 index 0000000..8a77d43 --- /dev/null +++ b/Toolkit.Foundation/IServiceFactoryRequired.cs @@ -0,0 +1,6 @@ +namespace Toolkit.Foundation; + +public interface IServiceFactoryRequired +{ + IServiceFactory Factory { get; } +} diff --git a/Toolkit.Foundation/IServiceProviderRequired.cs b/Toolkit.Foundation/IServiceProviderRequired.cs new file mode 100644 index 0000000..edb0f13 --- /dev/null +++ b/Toolkit.Foundation/IServiceProviderRequired.cs @@ -0,0 +1,6 @@ +namespace Toolkit.Foundation; + +public interface IServiceProviderRequired +{ + IServiceProvider Provider { get; } +} diff --git a/Toolkit.Foundation/ISubscriptionRequired.cs b/Toolkit.Foundation/ISubscriptionRequired.cs new file mode 100644 index 0000000..250433e --- /dev/null +++ b/Toolkit.Foundation/ISubscriptionRequired.cs @@ -0,0 +1,6 @@ +namespace Toolkit.Foundation; + +public interface ISubscriptionRequired +{ + ISubscription Subscription { get; } +} diff --git a/Toolkit.Foundation/Mediator.cs b/Toolkit.Foundation/Mediator.cs index 12c625e..3bebf3b 100644 --- a/Toolkit.Foundation/Mediator.cs +++ b/Toolkit.Foundation/Mediator.cs @@ -40,6 +40,7 @@ public class Mediator(IHandlerProvider handlerProvider, handlerList = []; handlers.Add(handlerType, handlerList); } + handlerList.Add(handler); } } @@ -48,7 +49,7 @@ public class Mediator(IHandlerProvider handlerProvider, { foreach (object? handler in handlerEntry.Value) { - if (handler?.GetType().GetMethod("Handle") is MethodInfo handleMethod) + if (handler?.GetType().GetMethod("Handle", [messageType, typeof(CancellationToken)]) is MethodInfo handleMethod) { return await (Task)handleMethod.Invoke(handler, new object[] { message, cancellationToken })!; diff --git a/Toolkit.Foundation/NavigateHandler.cs b/Toolkit.Foundation/NavigateHandler.cs index 7a06f42..7706346 100644 --- a/Toolkit.Foundation/NavigateHandler.cs +++ b/Toolkit.Foundation/NavigateHandler.cs @@ -3,16 +3,15 @@ namespace Toolkit.Foundation; public class NavigateHandler(NamedComponent scope, - IComponentScopeProvider componentScopeProvider, - IServiceProvider provider) : + IComponentScopeProvider componentScopeProvider) : INotificationHandler { public Task Handle(NavigateEventArgs args) { INavigationScope? navigationScope; - if (args.Scope == "self") + if (args.Scope == "self" && args.Sender is IServiceProviderRequired requireServiceProvider) { - navigationScope = provider.GetRequiredService(); + navigationScope = requireServiceProvider.Provider.GetRequiredService(); } else { diff --git a/Toolkit.Foundation/NavigationScope.cs b/Toolkit.Foundation/NavigationScope.cs index 7a3430d..2a27790 100644 --- a/Toolkit.Foundation/NavigationScope.cs +++ b/Toolkit.Foundation/NavigationScope.cs @@ -10,8 +10,11 @@ public class NavigationScope(IPublisher publisher, IContentTemplateDescriptorProvider contentTemplateDescriptorProvider) : INavigationScope { - public void Navigate(string route, object? sender = null, object? context = null, - EventHandler? navigated = null, object[]? parameters = null) + public void Navigate(string route, + object? sender = null, + object? context = null, + EventHandler? navigated = null, + object[]? parameters = null) { string[] segments = route.Split('/'); int segmentCount = segments.Length; @@ -41,26 +44,25 @@ public class NavigationScope(IPublisher publisher, if (provider.GetRequiredKeyedService(descriptor.TemplateType, segment) is object view) { - if ((parameters is { Length: > 0 } + if (context is not null) + { + if (navigationContextProvider.TryGet(context, out object? scopedContext)) + { + context = scopedContext; + } + } + else + { + context = view; + } + + if (context is not null) + { + if ((parameters is { Length: > 0 } ? factory.Create(descriptor.ContentType, parameters) : provider.GetRequiredKeyedService(descriptor.ContentType, segment)) is object viewModel) - { - if (context is not null) { - if (navigationContextProvider.TryGet(context, out object? scopedContext)) - { - context = scopedContext; - } - } - else - { - context = view; - } - - if (context is not null) - { - if (navigationProvider.Get(context is Type type ? type : context.GetType()) - is INavigation navigation) + if (navigationProvider.Get(context is Type type ? type : context.GetType()) is INavigation navigation) { Type navigateType = typeof(NavigateEventArgs<>).MakeGenericType(navigation.Type); if (Activator.CreateInstance(navigateType, [context, view, viewModel, sender, parameters]) is object navigate) diff --git a/Toolkit.Foundation/ObservableCollectionViewModel.cs b/Toolkit.Foundation/ObservableCollectionViewModel.cs index 7706ab7..383cbf1 100644 --- a/Toolkit.Foundation/ObservableCollectionViewModel.cs +++ b/Toolkit.Foundation/ObservableCollectionViewModel.cs @@ -19,6 +19,11 @@ public partial class ObservableCollectionViewModel : IList, IReadOnlyList, INotifyCollectionChanged, + IServiceProviderRequired, + IServiceFactoryRequired, + IMediatorRequired, + IPublisherRequired, + IDisposerRequired, INotificationHandler>, INotificationHandler>, INotificationHandler>, diff --git a/Toolkit.Foundation/ObservableViewModel.cs b/Toolkit.Foundation/ObservableViewModel.cs index 35aefce..adaa76d 100644 --- a/Toolkit.Foundation/ObservableViewModel.cs +++ b/Toolkit.Foundation/ObservableViewModel.cs @@ -10,7 +10,12 @@ public partial class ObservableViewModel : IDeactivating, IDeactivated, IDeactivatable, - IDisposable + IDisposable, + IServiceProviderRequired, + IServiceFactoryRequired, + IMediatorRequired, + IPublisherRequired, + IDisposerRequired { [ObservableProperty] private bool isInitialized; diff --git a/Toolkit.Foundation/Validation.cs b/Toolkit.Foundation/Validation.cs new file mode 100644 index 0000000..d3c35ed --- /dev/null +++ b/Toolkit.Foundation/Validation.cs @@ -0,0 +1,10 @@ +namespace Toolkit.Foundation; + +public record Validation +{ + public static ValidationEventArgs As(TValue value) => + new(value); + + public static ValidationEventArgs As() where TValue : new() => + new(new TValue()); +} \ No newline at end of file diff --git a/Toolkit.Foundation/ValidationEventArgs.cs b/Toolkit.Foundation/ValidationEventArgs.cs new file mode 100644 index 0000000..e225895 --- /dev/null +++ b/Toolkit.Foundation/ValidationEventArgs.cs @@ -0,0 +1,3 @@ +namespace Toolkit.Foundation; + +public record ValidationEventArgs(TValue Value);