Skip to content

Commit

Permalink
IoC is working
Browse files Browse the repository at this point in the history
  • Loading branch information
celeron533 committed Jul 18, 2024
1 parent 892f5c6 commit e5a4725
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 28 deletions.
27 changes: 16 additions & 11 deletions DicomGrep/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,17 @@ public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{

var services = new ServiceCollection();
ConfigServices(services);
ServiceProvider = services.BuildServiceProvider();

var mainWindow = ServiceProvider.GetRequiredService<MainWindow>();
mainWindow.Show();
Services = ConfigureServices();

base.OnStartup(e);
}

private void ConfigServices(IServiceCollection services)
/// <summary>
/// Configures the services for the application.
/// </summary>
private static IServiceProvider ConfigureServices()
{

var services = new ServiceCollection();
services.AddSingleton<IConfigurationService, ConfigurationService>();
services.AddSingleton<IDialogService, DialogService>();
services.AddSingleton<IDicomDictionaryLookupService, DicomDictionaryLookupService>();
Expand All @@ -45,9 +42,17 @@ private void ConfigServices(IServiceCollection services)
services.AddSingleton<ISearchService, SearchService>();
services.AddSingleton<IDictionaryService, DictionaryService>();

services.AddTransient<MainWindow>();
return services.BuildServiceProvider();
}

public IServiceProvider ServiceProvider { get; private set; }
/// <summary>
/// Gets the <see cref="IServiceProvider"/> instance to resolve application services.
/// </summary>
public IServiceProvider Services { get; private set; }

/// <summary>
/// Gets the current <see cref="App"/> instance in use
/// </summary>
public new static App Current => (App)Application.Current;
}
}
3 changes: 2 additions & 1 deletion DicomGrep/ViewModels/DicomDictionaryLookupViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public string DefaultFilterString
set => SetProperty(ref _defaultFilterString, value);
}

public DicomDictionaryLookupViewModel(IDictionaryService dictionaryService) : base()
public DicomDictionaryLookupViewModel() : base()
{
var dictionaryService = GetService<IDictionaryService>();
if (DicomDictionaryEntries == null || DicomDictionaryEntries.Count == 0)
{
DicomDictionaryEntries = new ObservableCollection<DicomDictionaryEntry>(
Expand Down
27 changes: 12 additions & 15 deletions DicomGrep/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,21 +285,18 @@ public ResultDicomFile SelectedMatchFile
set => SetProperty(ref _selectedMatchFile, value);
}


public MainViewModel(IConfigurationService _configurationService, IFolderPickupService _folderPickupService,
ISopClassLookupService _sopClassLookupService, IDicomDictionaryLookupService _dicomDictionaryLookupService,
IExportService _exportService, ITagValueDetailService _tagValueDetailService,
IFileOperationService _fileOperationService, ISearchService _searchService, IDictionaryService _dictionaryService)
{
configurationService = _configurationService;
folderPickupService = _folderPickupService;
sopClassLookupService = _sopClassLookupService;
dicomDictionaryLookupService = _dicomDictionaryLookupService;
exportService = _exportService;
tagValueDetailService = _tagValueDetailService;
fileOperationService = _fileOperationService;
searchService = _searchService;
dictionaryService = _dictionaryService; ;
public MainViewModel()
{
// viewModel here cannot inject the dependencies as the constructor parameters
configurationService = GetService<IConfigurationService>();
folderPickupService = GetService<IFolderPickupService>();
sopClassLookupService = GetService<ISopClassLookupService>();
dicomDictionaryLookupService = GetService<IDicomDictionaryLookupService>();
exportService = GetService<IExportService>();
tagValueDetailService = GetService<ITagValueDetailService>();
fileOperationService = GetService<IFileOperationService>();
searchService = GetService<ISearchService>();
dictionaryService = GetService<IDictionaryService>();

// dummy data for designer preview
if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
Expand Down
3 changes: 2 additions & 1 deletion DicomGrep/ViewModels/SopClassLookupViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ public string DefaultFilterString
set => SetProperty(ref _defaultFilterString, value);
}

public SopClassLookupViewModel(IDictionaryService dictionaryService) : base()
public SopClassLookupViewModel() : base()
{
var dictionaryService = GetService<IDictionaryService>();
if (SOPClassUIDs == null || SOPClassUIDs.Count == 0)
{
SOPClassUIDs = new ObservableCollection<DicomUID>(dictionaryService.GetAllDicomUIDDefs().Where(u => u.Type == DicomUidType.SOPClass || u.Type == DicomUidType.MetaSOPClass));
Expand Down
6 changes: 6 additions & 0 deletions DicomGrep/ViewModels/ViewModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,11 @@ protected virtual void InvalidateRequerySuggested()
{
Application.Current?.Dispatcher.BeginInvoke((Action)CommandManager.InvalidateRequerySuggested);
}

// service helper
protected static T GetService<T>() where T : class
{
return (T)App.Current.Services.GetService(typeof(T));
}
}
}

0 comments on commit e5a4725

Please sign in to comment.