Skip to content

Commit 4dc4a87

Browse files
committed
Migrate from MEF to Autofac
1 parent 59976af commit 4dc4a87

27 files changed

+112
-118
lines changed

src/DotNetPad/Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
44
</PropertyGroup>
55
<ItemGroup>
6+
<PackageVersion Include="Autofac" Version="8.4.0" />
67
<PackageVersion Include="AvalonEdit" Version="6.3.1.120" />
78
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Features" Version="4.14.0" />
89
<PackageVersion Include="Microsoft.CodeAnalysis.VisualBasic.Features" Version="4.14.0" />
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Autofac;
2+
using System.Waf.Applications;
3+
using Waf.DotNetPad.Applications.Controllers;
4+
using Waf.DotNetPad.Applications.Services;
5+
using Waf.DotNetPad.Applications.ViewModels;
6+
7+
namespace Waf.DotNetPad.Applications;
8+
9+
public class ApplicationsModule : Module
10+
{
11+
protected override void Load(ContainerBuilder builder)
12+
{
13+
builder.RegisterType<FileController>().AsSelf().SingleInstance();
14+
builder.RegisterType<ModuleController>().As<IModuleController>().SingleInstance();
15+
builder.RegisterType<WorkspaceController>().As<IWorkspaceService>().AsSelf().SingleInstance();
16+
17+
builder.RegisterType<CodeEditorService>().As<ICodeEditorService>().AsSelf().SingleInstance();
18+
builder.RegisterType<CSharpSampleService>().AsSelf().SingleInstance();
19+
builder.RegisterType<FileService>().As<IFileService>().As<IDocumentService>().AsSelf().SingleInstance();
20+
builder.RegisterType<ShellService>().As<IShellService>().AsSelf().SingleInstance();
21+
builder.RegisterType<VisualBasicSampleService>().AsSelf().SingleInstance();
22+
23+
builder.RegisterType<CodeEditorViewModel>().AsSelf();
24+
builder.RegisterType<ErrorListViewModel>().AsSelf().SingleInstance();
25+
builder.RegisterType<InfoViewModel>().AsSelf();
26+
builder.RegisterType<OutputViewModel>().AsSelf().SingleInstance();
27+
builder.RegisterType<SaveChangesViewModel>().AsSelf();
28+
builder.RegisterType<ShellViewModel>().AsSelf().SingleInstance();
29+
}
30+
}

src/DotNetPad/DotNetPad.Applications/Controllers/FileController.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System.ComponentModel.Composition;
2-
using System.Diagnostics;
3-
using System.Globalization;
4-
using System.Text;
1+
using System.Text;
52
using System.Waf.Applications;
63
using System.Waf.Applications.Services;
74
using Waf.DotNetPad.Applications.CodeAnalysis;
@@ -13,7 +10,6 @@
1310
namespace Waf.DotNetPad.Applications.Controllers;
1411

1512
/// <summary>Responsible for the file related commands.</summary>
16-
[Export]
1713
internal sealed class FileController
1814
{
1915
private static readonly string[] supportedFileExtensions = [".cs", ".vb"];
@@ -27,7 +23,7 @@ internal sealed class FileController
2723
private readonly IEnvironmentService environmentService;
2824
private readonly IClipboardService clipboardService;
2925
private readonly FileService fileService;
30-
private readonly ExportFactory<SaveChangesViewModel> saveChangesViewModelFactory;
26+
private readonly Func<SaveChangesViewModel> saveChangesViewModelFactory;
3127
private readonly DelegateCommand closeCommand;
3228
private readonly DelegateCommand closeAllCommand;
3329
private readonly DelegateCommand saveCommand;
@@ -36,9 +32,8 @@ internal sealed class FileController
3632
private IWeakEventProxy? activeDocumentPropertyChangedProxy;
3733
private int documentCounter;
3834

39-
[ImportingConstructor]
4035
public FileController(IMessageService messageService, IFileDialogService fileDialogService, IShellService shellService, IEnvironmentService environmentService,
41-
IClipboardService clipboardService, FileService fileService, ExportFactory<SaveChangesViewModel> saveChangesViewModelFactory)
36+
IClipboardService clipboardService, FileService fileService, Func<SaveChangesViewModel> saveChangesViewModelFactory)
4237
{
4338
this.messageService = messageService;
4439
this.fileDialogService = fileDialogService;
@@ -162,7 +157,7 @@ private bool PrepareToClose(IEnumerable<DocumentFile> documentsToClose)
162157
var modifiedDocuments = documentsToClose.Where(d => d.Modified).ToArray();
163158
if (!modifiedDocuments.Any()) return true;
164159

165-
var saveChangesViewModel = saveChangesViewModelFactory.CreateExport().Value;
160+
var saveChangesViewModel = saveChangesViewModelFactory();
166161
saveChangesViewModel.DocumentFiles = modifiedDocuments;
167162
bool? dialogResult = saveChangesViewModel.ShowDialog(shellService.ShellView);
168163
if (dialogResult == true)

src/DotNetPad/DotNetPad.Applications/Controllers/ModuleController.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.ComponentModel.Composition;
2-
using System.Waf.Applications;
1+
using System.Waf.Applications;
32
using System.Waf.Applications.Services;
43
using Waf.DotNetPad.Applications.DataModels;
54
using Waf.DotNetPad.Applications.Properties;
@@ -9,22 +8,20 @@
98

109
namespace Waf.DotNetPad.Applications.Controllers;
1110

12-
[Export(typeof(IModuleController))]
1311
internal sealed class ModuleController : IModuleController
1412
{
1513
private readonly Lazy<ShellService> shellService;
1614
private readonly ISettingsService settingsService;
1715
private readonly FileController fileController;
1816
private readonly WorkspaceController workspaceController;
1917
private readonly Lazy<ShellViewModel> shellViewModel;
20-
private readonly ExportFactory<CodeEditorViewModel> codeEditorViewModelFactory;
21-
private readonly ExportFactory<InfoViewModel> infoViewModelFactory;
18+
private readonly Func<CodeEditorViewModel> codeEditorViewModelFactory;
19+
private readonly Func<InfoViewModel> infoViewModelFactory;
2220
private readonly DelegateCommand infoCommand;
2321
private readonly ObservableList<DocumentDataModel> documentDataModels;
2422

25-
[ImportingConstructor]
2623
public ModuleController(Lazy<ShellService> shellService, ISettingsService settingsService, FileController fileController, WorkspaceController workspaceController, IFileService fileService,
27-
Lazy<ShellViewModel> shellViewModel, ExportFactory<CodeEditorViewModel> codeEditorViewModelFactory, ExportFactory<InfoViewModel> infoViewModelFactory)
24+
Lazy<ShellViewModel> shellViewModel, Func<CodeEditorViewModel> codeEditorViewModelFactory, Func<InfoViewModel> infoViewModelFactory)
2825
{
2926
this.shellService = shellService;
3027
this.settingsService = settingsService;
@@ -63,15 +60,15 @@ public void Shutdown() { }
6360

6461
private void ShowInfo()
6562
{
66-
var infoViewModel = infoViewModelFactory.CreateExport().Value;
63+
var infoViewModel = infoViewModelFactory();
6764
infoViewModel.ShowDialog(ShellService.ShellView);
6865
}
6966

7067
private DocumentDataModel CreateDocumentDataModel(DocumentFile documentFile) => new(documentFile, new(() => CreateDocumentViewModel(documentFile).View));
7168

7269
private CodeEditorViewModel CreateDocumentViewModel(DocumentFile document)
7370
{
74-
var viewModel = codeEditorViewModelFactory.CreateExport().Value;
71+
var viewModel = codeEditorViewModelFactory();
7572
viewModel.DocumentFile = document;
7673
return viewModel;
7774
}

src/DotNetPad/DotNetPad.Applications/Controllers/WorkspaceController.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Microsoft.CodeAnalysis;
22
using Microsoft.CodeAnalysis.Host.Mef;
3-
using System.ComponentModel.Composition;
43
using System.Composition.Hosting;
54
using System.Globalization;
65
using System.Waf.Applications;
@@ -12,7 +11,6 @@
1211

1312
namespace Waf.DotNetPad.Applications.Controllers;
1413

15-
[Export, Export(typeof(IWorkspaceService))]
1614
internal sealed class WorkspaceController : IWorkspaceService
1715
{
1816
private readonly TaskScheduler taskScheduler;
@@ -32,7 +30,6 @@ internal sealed class WorkspaceController : IWorkspaceService
3230
private CancellationTokenSource? runScriptCancellation;
3331
private DocumentFile? runningDocument;
3432

35-
[ImportingConstructor]
3633
public WorkspaceController(IDocumentService documentService, Lazy<ShellViewModel> shellViewModel, Lazy<ErrorListViewModel> errorListViewModel, Lazy<OutputViewModel> outputViewModel)
3734
{
3835
taskScheduler = TaskScheduler.FromCurrentSynchronizationContext();

src/DotNetPad/DotNetPad.Applications/DotNetPad.Applications.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<PackageReference Include="Autofac" />
910
<PackageReference Include="System.Waf.Wpf" />
1011
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Features" />
1112
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.Features" />

src/DotNetPad/DotNetPad.Applications/Services/CSharpSampleService.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
using System.ComponentModel.Composition;
1+
namespace Waf.DotNetPad.Applications.Services;
22

3-
namespace Waf.DotNetPad.Applications.Services;
4-
5-
[Export]
63
public class CSharpSampleService
74
{
85
public Lazy<string> OutVariables => new(() => GetSampleCode("OutVariables.cs"));

src/DotNetPad/DotNetPad.Applications/Services/CodeEditorService.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
using System.ComponentModel.Composition;
2-
using Waf.DotNetPad.Domain;
1+
using Waf.DotNetPad.Domain;
32

43
namespace Waf.DotNetPad.Applications.Services;
54

6-
[Export(typeof(ICodeEditorService)), Export]
75
public class CodeEditorService : ICodeEditorService
86
{
97
public event EventHandler<SetCaretEventArgs>? RequestSetCaret;

src/DotNetPad/DotNetPad.Applications/Services/FileService.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
1-
using System.ComponentModel.Composition;
2-
using System.Waf.Applications;
1+
using System.Waf.Applications;
32
using System.Windows.Input;
43
using Waf.DotNetPad.Domain;
54

65
namespace Waf.DotNetPad.Applications.Services;
76

8-
[Export(typeof(IDocumentService)), Export(typeof(IFileService)), Export]
97
internal sealed class FileService : Model, IFileService
108
{
119
private readonly ObservableList<DocumentFile> documentFiles;
1210
private DocumentFile? activeDocumentFile;
1311
private DocumentFile? lockedDocumentFile;
14-
private ICommand newCSharpCommand = null!;
15-
private ICommand newVisualBasicCommand = null!;
16-
private DelegateCommand newCSharpFromClipboardCommand = null!;
17-
private DelegateCommand newVisualBasicFromClipboardCommand = null!;
18-
private ICommand openCommand = null!;
19-
private ICommand closeCommand = null!;
20-
private ICommand closeAllCommand = null!;
21-
private ICommand saveCommand = null!;
22-
private ICommand saveAsCommand = null!;
23-
24-
[ImportingConstructor]
12+
private ICommand newCSharpCommand = DelegateCommand.DisabledCommand;
13+
private ICommand newVisualBasicCommand = DelegateCommand.DisabledCommand;
14+
private DelegateCommand newCSharpFromClipboardCommand = DelegateCommand.DisabledCommand;
15+
private DelegateCommand newVisualBasicFromClipboardCommand = DelegateCommand.DisabledCommand;
16+
private ICommand openCommand = DelegateCommand.DisabledCommand;
17+
private ICommand closeCommand = DelegateCommand.DisabledCommand;
18+
private ICommand closeAllCommand = DelegateCommand.DisabledCommand;
19+
private ICommand saveCommand = DelegateCommand.DisabledCommand;
20+
private ICommand saveAsCommand = DelegateCommand.DisabledCommand;
21+
2522
public FileService()
2623
{
2724
documentFiles = [];

src/DotNetPad/DotNetPad.Applications/Services/ShellService.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
using System.ComponentModel.Composition;
2-
using Waf.DotNetPad.Applications.Properties;
1+
using Waf.DotNetPad.Applications.Properties;
32
using Waf.DotNetPad.Applications.Views;
43

54
namespace Waf.DotNetPad.Applications.Services;
65

7-
[Export(typeof(IShellService)), Export]
86
internal sealed class ShellService : Model, IShellService
97
{
108
private readonly Lazy<IShellView> shellView;
@@ -13,7 +11,6 @@ internal sealed class ShellService : Model, IShellService
1311
private bool isClosingEventInitialized;
1412
private CancelEventHandler? closing;
1513

16-
[ImportingConstructor]
1714
public ShellService(Lazy<IShellView> shellView)
1815
{
1916
this.shellView = shellView;

0 commit comments

Comments
 (0)