Skip to content

Commit

Permalink
Migrate own mediator class to MediatR
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinotorowski committed Sep 15, 2021
1 parent 4fdc7a7 commit 2dfc024
Show file tree
Hide file tree
Showing 81 changed files with 1,339 additions and 759 deletions.
133 changes: 120 additions & 13 deletions src/Otor.MsixHero.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@
// https://github.com/marcinotorowski/msix-hero/blob/develop/LICENSE.md

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using MediatR;
using MediatR.Pipeline;
using Notifications.Wpf.Core;
using Notifications.Wpf.Core.Controls;
using Otor.MsixHero.App.Controls;
Expand Down Expand Up @@ -73,11 +81,15 @@
using Otor.MsixHero.Infrastructure.Updates;
using Otor.MsixHero.Lib.Infrastructure.Progress;
using Otor.MsixHero.Lib.Proxy;
using Prism.Events;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using Prism.Unity;
using Unity;
using Unity.Lifetime;

namespace Otor.MsixHero.App
{
Expand All @@ -100,7 +112,7 @@ static App()

LogManager.Initialize(logLevel);
}

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterSingleton<IInteractionService, InteractionService>();
Expand All @@ -125,18 +137,22 @@ protected override void RegisterTypes(IContainerRegistry containerRegistry)
containerRegistry.RegisterSingleton<IMsixHeroApplication, MsixHeroApplication>();
containerRegistry.RegisterSingleton<IRunningAppsDetector, RunningAppsDetector>();
containerRegistry.RegisterSingleton<IAppxManifestCreator, AppxManifestCreator>();
containerRegistry.RegisterSingleton<IEventAggregator, EventAggregator>();
containerRegistry.RegisterSingleton<IInterProcessCommunicationManager, InterProcessCommunicationManager>();
containerRegistry.Register<IDependencyMapper, DependencyMapper>();
containerRegistry.Register<IThirdPartyAppProvider, ThirdPartyAppProvider>();
containerRegistry.Register<IServiceRecommendationAdvisor, ServiceRecommendationAdvisor>();
containerRegistry.RegisterSingleton<PrismServices>();

containerRegistry.RegisterSingleton<IMediator>(containerProvider => new Mediator(containerProvider.Resolve));

containerRegistry.RegisterDialog<PackageExpertDialogView, PackageExpertDialogViewModel>(NavigationPaths.DialogPaths.PackageExpert);

if (Environment.GetCommandLineArgs().Length < 2)
{
containerRegistry.RegisterDialogWindow<AcrylicDialogWindow>();
}

this.Container.GetContainer().RegisterMediatorHandlers(typeof(App).Assembly);
}

protected override void OnExit(ExitEventArgs e)
Expand All @@ -155,7 +171,6 @@ protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
moduleCatalog.AddModule(new ModuleInfo(typeof(VolumeManagementModule), ModuleNames.VolumeManagement, InitializationMode.OnDemand));
moduleCatalog.AddModule(new ModuleInfo(typeof(DashboardModule), ModuleNames.Dashboard, InitializationMode.OnDemand));
moduleCatalog.AddModule(new ModuleInfo(typeof(WhatsNewModule), ModuleNames.WhatsNew, InitializationMode.OnDemand));

moduleCatalog.AddModule(new ModuleInfo(typeof(SigningModule), ModuleNames.Dialogs.Signing, InitializationMode.OnDemand));
moduleCatalog.AddModule(new ModuleInfo(typeof(AppInstallerModule), ModuleNames.Dialogs.AppInstaller, InitializationMode.OnDemand));
moduleCatalog.AddModule(new ModuleInfo(typeof(PackagingModule), ModuleNames.Dialogs.Packaging, InitializationMode.OnDemand));
Expand All @@ -166,7 +181,7 @@ protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
moduleCatalog.AddModule(new ModuleInfo(typeof(AppAttachModule), ModuleNames.Dialogs.AppAttach, InitializationMode.OnDemand));
moduleCatalog.AddModule(new ModuleInfo(typeof(SettingsModule), ModuleNames.Dialogs.Settings, InitializationMode.OnDemand));
moduleCatalog.AddModule(new ModuleInfo(typeof(HelpModule), ModuleNames.Dialogs.Help, InitializationMode.OnDemand));

base.ConfigureModuleCatalog(moduleCatalog);
}

Expand All @@ -177,7 +192,7 @@ private void InitializeMainWindow()

ViewModelLocationProvider.Register<PackageExpertDialogView, PackageExpertDialogViewModel>();
regionManager.RegisterViewWithRegion(RegionNames.PackageExpert, typeof(PackageExpertControl));

var app = this.Container.Resolve<IMsixHeroApplication>();
var config = this.Container.Resolve<IConfigurationService>();
var releaseNotesHelper = new ReleaseNotesHelper(config);
Expand All @@ -192,7 +207,7 @@ private void InitializeMainWindow()
helper.GoToDefaultScreenAsync();
}
}

private void InitializePackageExpert()
{
ViewModelLocationProvider.Register<PackageExpertDialogView, PackageExpertDialogViewModel>();
Expand All @@ -201,20 +216,20 @@ private void InitializePackageExpert()
var par = new DialogParameters
{
{
"package",
"package",
Environment.GetCommandLineArgs()[1]
}
};

var dialogService = this.Container.Resolve<IDialogService>();
dialogService.Show(NavigationPaths.DialogPaths.PackageExpert, par, _ => {});
dialogService.Show(NavigationPaths.DialogPaths.PackageExpert, par, _ => { });
// regionManager.Regions[RegionNames.Root].RequestNavigate(new Uri(NavigationPaths.PackageManagement, UriKind.Relative), par);
}

protected override void Initialize()
{
base.Initialize();

var config = this.GetConfigurationSafe();
var tier = config?.UiConfiguration?.UxTier ?? UxTierLevel.Auto;
switch (tier)
Expand All @@ -228,7 +243,7 @@ protected override void Initialize()
TierController.SetSystemTier();
break;
}

if (Environment.GetCommandLineArgs().Length > 1)
{
this.InitializePackageExpert();
Expand All @@ -238,7 +253,7 @@ protected override void Initialize()
this.InitializeMainWindow();
}
}

private Configuration GetConfigurationSafe()
{
return ExceptionGuard.Guard(() => this.Container.Resolve<IConfigurationService>().GetCurrentConfiguration());
Expand All @@ -257,4 +272,96 @@ protected override Window CreateShell()
}
}
}
}

// ReSharper disable once InconsistentNaming
public static class IUnityContainerExtensions
{
public static IUnityContainer RegisterMediator(this IUnityContainer container, ITypeLifetimeManager lifetimeManager)
{
return container.RegisterType<IMediator, Mediator>(lifetimeManager)
.RegisterInstance<ServiceFactory>(type =>
{
var enumerableType = type
.GetInterfaces()
.Concat(new[] { type })
.FirstOrDefault(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>));
return enumerableType != null
? container.ResolveAll(enumerableType.GetGenericArguments()[0])
: container.IsRegistered(type)
? container.Resolve(type)
: null;
});
}

public static IUnityContainer RegisterMediatorHandlers(this IUnityContainer container, Assembly assembly)
{
return container.RegisterTypesImplementingType(assembly, typeof(IRequestHandler<,>))
.RegisterNamedTypesImplementingType(assembly, typeof(INotificationHandler<>));
}

internal static bool IsGenericTypeOf(this Type type, Type genericType)
{
return type.IsGenericType &&
type.GetGenericTypeDefinition() == genericType;
}

internal static void AddGenericTypes(this List<object> list, IUnityContainer container, Type genericType)
{
var genericHandlerRegistrations =
container.Registrations.Where(reg => reg.RegisteredType == genericType);

foreach (var handlerRegistration in genericHandlerRegistrations)
{
if (list.All(item => item.GetType() != handlerRegistration.MappedToType))
{
list.Add(container.Resolve(handlerRegistration.MappedToType));
}
}
}

/// <summary>
/// Register all implementations of a given type for provided assembly.
/// </summary>
public static IUnityContainer RegisterTypesImplementingType(this IUnityContainer container, Assembly assembly, Type type)
{
foreach (var implementation in assembly.GetTypes().Where(t => t.GetInterfaces().Any(implementation => IsSubclassOfRawGeneric(type, implementation))))
{
var interfaces = implementation.GetInterfaces();
foreach (var @interface in interfaces)
container.RegisterType(@interface, implementation);
}

return container;
}

/// <summary>
/// Register all implementations of a given type for provided assembly.
/// </summary>
public static IUnityContainer RegisterNamedTypesImplementingType(this IUnityContainer container, Assembly assembly, Type type)
{
foreach (var implementation in assembly.GetTypes().Where(t => t.GetInterfaces().Any(implementation => IsSubclassOfRawGeneric(type, implementation))))
{
var interfaces = implementation.GetInterfaces();
foreach (var @interface in interfaces)
container.RegisterType(@interface, implementation, implementation.FullName);
}

return container;
}

private static bool IsSubclassOfRawGeneric(Type generic, Type toCheck)
{
while (toCheck != null && toCheck != typeof(object))
{
var currentType = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
if (generic == currentType)
return true;

toCheck = toCheck.BaseType;
}

return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
xmlns:open="clr-namespace:Otor.MsixHero.App.Controls.PackageExpert.Views.Open"
xmlns:start="clr-namespace:Otor.MsixHero.App.Controls.PackageExpert.Views.Start"
xmlns:viewModels="clr-namespace:Otor.MsixHero.App.Controls.PackageExpert.ViewModels"
xmlns:commands="clr-namespace:Otor.MsixHero.App.Hero"
xmlns:commands="clr-namespace:Otor.MsixHero.App.Hero.Commands"
xmlns:helpers="clr-namespace:Otor.MsixHero.App.Helpers"
xmlns:more="clr-namespace:Otor.MsixHero.App.Controls.PackageExpert.Views.More"
d:DataContext="{d:DesignInstance viewModels:PackageExpertViewModel}"
Expand Down Expand Up @@ -106,7 +106,7 @@
</Popup>

<Button
Command="{x:Static commands:MsixHeroCommands.RemovePackage}"
Command="{x:Static commands:MsixHeroRoutedUICommands.RemovePackage}"
Style="{StaticResource ActionButton}">
<controls:LabelWithIcon
Style="{StaticResource ActionLabelWithIcon}"
Expand All @@ -115,7 +115,7 @@
</Button>

<Button
Command="{x:Static commands:MsixHeroCommands.CheckUpdates}"
Command="{x:Static commands:MsixHeroRoutedUICommands.CheckUpdates}"
Style="{StaticResource ActionButton}">
<controls:LabelWithIcon
Style="{StaticResource ActionLabelWithIcon}"
Expand Down
10 changes: 5 additions & 5 deletions src/Otor.MsixHero.App/Controls/PackageExpert/Views/Body.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
xmlns:view="clr-namespace:Otor.MsixHero.App.Controls.PsfContent.View"
xmlns:helpers="clr-namespace:Otor.MsixHero.App.Helpers"
xmlns:converters="clr-namespace:Otor.MsixHero.App.Converters"
xmlns:hero="clr-namespace:Otor.MsixHero.App.Hero"
xmlns:hero="clr-namespace:Otor.MsixHero.App.Hero.Commands"
xmlns:volume="clr-namespace:Otor.MsixHero.App.Controls.Volume"
xmlns:elements="clr-namespace:Otor.MsixHero.App.Modules.PackageManagement.PackageList.ViewModels"
xmlns:interop="clr-namespace:Otor.MsixHero.App.Helpers.Interop"
Expand Down Expand Up @@ -549,7 +549,7 @@
ItemsSource="{Binding Files.Nodes}">
<commands:RoutedCommandHandlers.Commands>
<commands:RoutedCommandHandler
RoutedCommand="{x:Static hero:MsixHeroCommands.OpenFile}"
RoutedCommand="{x:Static hero:MsixHeroRoutedUICommands.OpenFile}"
Command="{Binding Files.View}" />
</commands:RoutedCommandHandlers.Commands>
<ListView.Style>
Expand All @@ -566,7 +566,7 @@
</ListView.Style>
<ListView.ContextMenu>
<ContextMenu>
<MenuItem Header="View/edit" Command="{x:Static hero:MsixHeroCommands.OpenFile}">
<MenuItem Header="View/edit" Command="{x:Static hero:MsixHeroRoutedUICommands.OpenFile}">
<MenuItem.Icon>
<Path Style="{StaticResource MediumIcon}" Data="{StaticResource VectorOpen}" />
</MenuItem.Icon>
Expand Down Expand Up @@ -842,7 +842,7 @@

<TextBlock
DockPanel.Dock="Top" Margin="0 10">
<Hyperlink Command="{x:Static hero:MsixHeroCommands.DependencyViewer}">
<Hyperlink Command="{x:Static hero:MsixHeroRoutedUICommands.DependencyViewer}">
<Run Text="Open dependencies graph..." />
</Hyperlink>
</TextBlock>
Expand Down Expand Up @@ -966,7 +966,7 @@
TotalSize="{Binding Disk.Capacity}" />

<TextBlock HorizontalAlignment="Left" Margin="0 10 0 0">
<Hyperlink Command="{x:Static hero:MsixHeroCommands.ChangeVolume}">
<Hyperlink Command="{x:Static hero:MsixHeroRoutedUICommands.ChangeVolume}">
<Run Text="Change install directory..." />
</Hyperlink>
</TextBlock>
Expand Down
20 changes: 10 additions & 10 deletions src/Otor.MsixHero.App/Controls/PackageExpert/Views/More/More.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:controls="clr-namespace:Otor.MsixHero.App.Controls"
xmlns:helpers="clr-namespace:Otor.MsixHero.App.Helpers"
xmlns:commands="clr-namespace:Otor.MsixHero.App.Hero"
xmlns:commands="clr-namespace:Otor.MsixHero.App.Hero.Commands"
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
xmlns:commands1="clr-namespace:Otor.MsixHero.App.Modules.PackageManagement.Commands"
xmlns:pkgMgmtCommands="clr-namespace:Otor.MsixHero.App.Modules.PackageManagement.Commands"
mc:Ignorable="d" >

<StackPanel>
Expand All @@ -21,7 +21,7 @@
Visibility="Collapsed"
Style="{StaticResource ActionButton}"
CommandTarget="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Popup}}"
Command="{x:Static commands:MsixHeroCommands.PackageExpert}">
Command="{x:Static commands:MsixHeroRoutedUICommands.PackageExpert}">
<controls:LabelWithIcon Icon16x16="{StaticResource SmallInNewWindow}" Content="Open in a new window" />
<b:Interaction.Behaviors>
<helpers:ButtonClosePopup />
Expand All @@ -32,7 +32,7 @@
<Button
Style="{StaticResource ActionButton}"
CommandTarget="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Popup}}"
Command="{x:Static commands:MsixHeroCommands.DependencyViewer}">
Command="{x:Static commands:MsixHeroRoutedUICommands.DependencyViewer}">
<controls:LabelWithIcon
Icon16x16="{StaticResource SmallDependencies}"
Content="Dependency analyzer..." />
Expand All @@ -44,7 +44,7 @@
<Button
Style="{StaticResource ActionButton}"
CommandTarget="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Popup}}"
Command="{x:Static commands:MsixHeroCommands.ModificationPackage}">
Command="{x:Static commands:MsixHeroRoutedUICommands.ModificationPackage}">
<controls:LabelWithIcon
Icon16x16="{StaticResource SmallMicrosoftWindows}"
Content="Modification package editor..." />
Expand All @@ -56,7 +56,7 @@
<Button
Style="{StaticResource ActionButton}"
CommandTarget="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Popup}}"
Command="{x:Static commands:MsixHeroCommands.AppInstaller}">
Command="{x:Static commands:MsixHeroRoutedUICommands.AppInstaller}">
<controls:LabelWithIcon
Icon16x16="{StaticResource SmallAppInstaller}"
Content="App installer editor..." />
Expand All @@ -67,9 +67,9 @@

<Button
Style="{StaticResource ActionButton}"
CommandParameter="{x:Static commands1:DialogTarget.Selection}"
CommandParameter="{x:Static pkgMgmtCommands:DialogTarget.Selection}"
CommandTarget="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Popup}}"
Command="{x:Static commands:MsixHeroCommands.Winget}">
Command="{x:Static commands:MsixHeroRoutedUICommands.Winget}">

<controls:LabelWithIcon
Icon16x16="{StaticResource SmallTerminal}"
Expand Down Expand Up @@ -106,9 +106,9 @@

<Button
Style="{StaticResource ActionButton}"
CommandParameter="{x:Static commands1:DialogTarget.Selection}"
CommandParameter="{x:Static pkgMgmtCommands:DialogTarget.Selection}"
CommandTarget="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Popup}}"
Command="{x:Static commands:MsixHeroCommands.ChangeVolume}">
Command="{x:Static commands:MsixHeroRoutedUICommands.ChangeVolume}">
<controls:LabelWithIcon Content="Change volume..." Icon16x16="{StaticResource SmallForward}" />
<b:Interaction.Behaviors>
<helpers:ButtonClosePopup />
Expand Down
Loading

0 comments on commit 2dfc024

Please sign in to comment.