Skip to content

Commit

Permalink
chore: refactor code implementing dependency injection
Browse files Browse the repository at this point in the history
- Performed large scale code refactoring to update dependency injection implementation from the 'Jab' library to the 'Injectio' library(extending Microsoft.Extensions.DependencyInjection).

Signed-off-by: Russell Camo <[email protected]>
  • Loading branch information
russkyc committed Aug 28, 2023
1 parent 0b8c431 commit 4dbc89d
Show file tree
Hide file tree
Showing 22 changed files with 123 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

namespace GroomWise.Application.Enums;

public enum NavigationPage
public enum AppViews
{
Login,
Main
Main,
Dashboard
}
1 change: 1 addition & 0 deletions GroomWise.Application/GroomWise.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@


<ItemGroup>
<PackageReference Include="Injectio" Version="2.6.2" />
<PackageReference Include="Lombok.NET" Version="2.1.3" />
<PackageReference Include="Mapster" Version="7.3.0" />
<PackageReference Include="MvvmGen.PureCodeGeneration" Version="1.2.1">
Expand Down
21 changes: 17 additions & 4 deletions GroomWise.Application/ViewModels/AppViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,41 @@
// without written, signed consent from the author is strictly prohibited.

using GroomWise.Application.Enums;
using GroomWise.Domain.Interfaces;
using GroomWise.Infrastructure.Authentication.Enums;
using GroomWise.Infrastructure.Authentication.Interfaces;
using GroomWise.Infrastructure.Navigation;
using GroomWise.Infrastructure.Navigation.Interfaces;
using Injectio.Attributes;
using MvvmGen;
using MvvmGen.ViewModels;

namespace GroomWise.Application.ViewModels;

[Inject(typeof(IAuthenticationService))]
[Inject(typeof(IDialogFactory))]
[Inject(typeof(INavigationService))]
[Inject(typeof(DashboardViewModel))]
[ViewModel]
[ViewModelGenerateInterface]
[RegisterSingleton]
public partial class AppViewModel
{
[Property]
private ViewModelBase _pageContext;

partial void OnInitialize()
{
PageContext = DashboardViewModel;
}

[Command]
private async Task Logout()
{
await Task.Run(async () =>
{
var dialogResult = DialogFactory.Create(
"GroomWise",
"Are you sure you want to log out?"
"Are you sure you want to log out?",
NavigationService
);
if (dialogResult == true)
Expand All @@ -35,7 +48,7 @@ await Task.Run(async () =>
await Task.Delay(500);
if (result.Equals(AuthenticationStatus.NotAuthenticated))
{
NavigationService.Instance?.Navigate(NavigationPage.Login);
NavigationService.Navigate(AppViews.Login);
}
}
});
Expand Down
13 changes: 8 additions & 5 deletions GroomWise.Application/ViewModels/DashboardViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// Copyright (C) 2023 Russell Camo (Russkyc).- All Rights Reserved
//
//
// Unauthorized copying or redistribution of all files, in source and binary forms via any medium
// without written, signed consent from the author is strictly prohibited.

using Injectio.Attributes;
using MvvmGen;

namespace GroomWise.Application.ViewModels;

public class DashboardViewModel
{
}
[ViewModel]
[ViewModelGenerateInterface]
[RegisterSingleton]
public partial class DashboardViewModel { }
7 changes: 5 additions & 2 deletions GroomWise.Application/ViewModels/LoginViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
using GroomWise.Domain.Enums;
using GroomWise.Infrastructure.Authentication.Enums;
using GroomWise.Infrastructure.Authentication.Interfaces;
using GroomWise.Infrastructure.Navigation;
using GroomWise.Infrastructure.Navigation.Interfaces;
using Injectio.Attributes;
using MvvmGen;
using Swordfish.NET.Collections;

namespace GroomWise.Application.ViewModels;

[ViewModel]
[ViewModelGenerateInterface]
[Inject(typeof(INavigationService))]
[Inject(typeof(IAuthenticationService))]
[RegisterSingleton]
public partial class LoginViewModel
{
[Property]
Expand Down Expand Up @@ -86,7 +89,7 @@ await Task.Run(async () =>
await Task.Delay(1000);
Notifications.RemoveLast();
NavigationService.Instance?.Navigate(NavigationPage.Main);
NavigationService.Navigate(AppViews.Main);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
// Unauthorized copying or redistribution of all files, in source and binary forms via any medium
// without written, signed consent from the author is strictly prohibited.

using GroomWise.Domain.Entities;
using GroomWise.Infrastructure.Authentication.Enums;
using GroomWise.Infrastructure.Authentication.Interfaces;
using GroomWise.Infrastructure.Authentication.Mappers;
using GroomWise.Infrastructure.Database;
using GroomWise.Infrastructure.Encryption.Interfaces;
using GroomWise.Infrastructure.Session.Entities;
using Russkyc.DependencyInjection.Attributes;
using Russkyc.DependencyInjection.Enums;
using Injectio.Attributes;
using Role = GroomWise.Domain.Enums.Role;

namespace GroomWise.Infrastructure.Authentication;

[Service(Scope.Singleton, Registration.AsSelfAndInterfaces)]
[RegisterSingleton<IAuthenticationService, AuthenticationService>]
public class AuthenticationService : IAuthenticationService
{
private object _lock = new();
Expand Down
3 changes: 2 additions & 1 deletion GroomWise.Infrastructure/Database/GroomWiseDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
// without written, signed consent from the author is strictly prohibited.

using GroomWise.Domain.Entities;
using Injectio.Attributes;
using Russkyc.DependencyInjection.Attributes;
using Russkyc.DependencyInjection.Enums;

namespace GroomWise.Infrastructure.Database;

[Service(Scope.Singleton)]
[RegisterSingleton]
public class GroomWiseDbContext : DbContext
{
public GroomWiseDbContext()
Expand Down
3 changes: 2 additions & 1 deletion GroomWise.Infrastructure/Encryption/EncryptionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@

using System.Reflection;
using GroomWise.Infrastructure.Encryption.Interfaces;
using Injectio.Attributes;
using NETCore.Encrypt;
using NETCore.Encrypt.Extensions;
using Russkyc.DependencyInjection.Attributes;
using Russkyc.DependencyInjection.Enums;

namespace GroomWise.Infrastructure.Encryption;

[Service(Scope.Singleton, Registration.AsSelfAndInterfaces)]
[RegisterSingleton<IEncryptionService, EncryptionService>]
public class EncryptionService : IEncryptionService
{
public EncryptionService()
Expand Down
1 change: 1 addition & 0 deletions GroomWise.Infrastructure/GroomWise.Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<PackageReference Include="CredentialManagement" Version="1.0.2" />
<PackageReference Include="Injectio" Version="2.6.2" />
<PackageReference Include="LiteDB" Version="5.0.17" />
<PackageReference Include="Lombok.NET" Version="2.1.3" />
<PackageReference Include="Mapster" Version="7.3.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright (C) 2023 Russell Camo (Russkyc).- All Rights Reserved
//
//
// Unauthorized copying or redistribution of all files, in source and binary forms via any medium
// without written, signed consent from the author is strictly prohibited.

namespace GroomWise.Domain.Interfaces;
namespace GroomWise.Infrastructure.Navigation.Interfaces;

public interface IDialogFactory
{
bool? Create(string messageBoxText, string caption);
}
bool? Create(string messageBoxText, string caption, INavigationService navigationService);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ namespace GroomWise.Infrastructure.Navigation.Interfaces;

public interface INavigationService
{
static INavigationService Instance { get; }
IWindow CurrentWindow { get; }
IPage CurrentPage { get; }
void Add(Enum key, IWindow instance);
void Add(Enum key, IPage instance);
void Navigate(Enum key);
void Initialize(SynchronizationContext? context, IWindow? mainWindow);
}
46 changes: 27 additions & 19 deletions GroomWise.Infrastructure/Navigation/NavigationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,45 @@
// without written, signed consent from the author is strictly prohibited.

using GroomWise.Infrastructure.Navigation.Interfaces;
using Injectio.Attributes;

namespace GroomWise.Infrastructure.Navigation;

[RegisterSingleton<INavigationService, NavigationService>]
public class NavigationService : INavigationService
{
private readonly SynchronizationContext? _synchronizationContext;
private readonly Dictionary<Enum, object?>? _views;
private static INavigationService? _instance;
private SynchronizationContext? _synchronizationContext;
private Dictionary<Enum, object?>? _views;
private static IWindow? _currentWindow;
private static IPage _currentPage;

Check warning on line 17 in GroomWise.Infrastructure/Navigation/NavigationService.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Non-nullable field '_currentPage' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 17 in GroomWise.Infrastructure/Navigation/NavigationService.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Non-nullable field '_currentPage' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
private static readonly object Lock = new();

public static INavigationService? Instance
public IWindow CurrentWindow
{
get
{
lock (Lock)
{
return _instance;
return _currentWindow!;
}
}
}

public IWindow CurrentWindow
public IPage CurrentPage
{
get
{
lock (Lock)
{
return _currentWindow!;
return _currentPage;
}
}
}

public NavigationService(SynchronizationContext? context, IWindow? mainWindow)
{
if (context is not null)
private set
{
_synchronizationContext = context;
_views = new();
_currentWindow = mainWindow;
lock (Lock)
{
_currentPage = value;
}
}
}

Expand All @@ -62,22 +61,31 @@ public void Navigate(Enum key)
_synchronizationContext?.Send(
_ =>
{
if (_views?[key] is IWindow window)
var view = _views?[key];
if (view is IWindow window)
{
window.Show();
_currentWindow?.Hide();
_currentWindow = window;
return;
}
if (view is IPage page)
{
CurrentPage = page;
}
},
null
);
}

public static void Initialize(SynchronizationContext context, IWindow currentWindow)
public void Initialize(SynchronizationContext? context, IWindow? mainWindow)
{
lock (Lock)
if (context is not null)
{
_instance = new NavigationService(context, currentWindow);
_synchronizationContext = context;
_views = new();
_currentWindow = mainWindow;
}
}
}
18 changes: 11 additions & 7 deletions GroomWise.WPF/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
// without written, signed consent from the author is strictly prohibited.

using System.Threading;
using System.Windows.Navigation;
using GroomWise.Application.Enums;
using GroomWise.Containers;
using GroomWise.Infrastructure.Navigation.Interfaces;
using GroomWise.Views;
using GroomWise.Views.Dialogs;
using NavigationService = GroomWise.Infrastructure.Navigation.NavigationService;
using Microsoft.Extensions.DependencyInjection;

namespace GroomWise;

Expand All @@ -25,18 +23,24 @@ public partial class App
public App()
{
InitializeComponent();
var container = new ServiceProvider();

var services = new ServiceCollection()
.AddGroomWiseInfrastructure()
.AddGroomWiseApplication()
.AddGroomWise();
var container = services.BuildServiceProvider();

var main = container.GetService<MainView>();
var login = container.GetService<LoginView>();
var navigation = container.GetService<INavigationService>();

Current.MainWindow = login;

Current.Dispatcher.BeginInvoke(() =>
{
NavigationService.Initialize(SynchronizationContext.Current!, login);
NavigationService.Instance?.Add(NavigationPage.Login, login);
NavigationService.Instance?.Add(NavigationPage.Main, main);
navigation?.Initialize(SynchronizationContext.Current!, login!);
navigation?.Add(AppViews.Login, login!);
navigation?.Add(AppViews.Main, main!);
});
MainWindow?.Show();
}
Expand Down
35 changes: 0 additions & 35 deletions GroomWise.WPF/Containers/ServiceProvider.cs

This file was deleted.

Loading

0 comments on commit 4dbc89d

Please sign in to comment.