DI StartUp fixes

This commit is contained in:
NickAppLab
2022-07-29 16:36:15 +05:00
parent 5d2c6a85d7
commit efa7de7617
2 changed files with 18 additions and 12 deletions

View File

@@ -1,8 +1,7 @@
<Application x:Class="StructureHelper.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:StructureHelper"
StartupUri="Windows/MainWindow/MainView.xaml">
xmlns:local="clr-namespace:StructureHelper">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>

View File

@@ -11,23 +11,30 @@ namespace StructureHelper
public partial class App : Application
{
public static IContainer Container { get; private set; }
public static ILifetimeScope Scope { get; private set; }
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var builder = new ContainerBuilder();
builder.RegisterType<PrimitiveRepository>().As<IPrimitiveRepository>();
builder.RegisterType<PrimitiveService>().As<IPrimitiveService>();
builder.RegisterType<MainModel>().AsSelf();
builder.RegisterType<MainViewModel>().AsSelf();
builder.RegisterType<PrimitiveRepository>().As<IPrimitiveRepository>().SingleInstance();
builder.RegisterType<PrimitiveService>().As<IPrimitiveService>().SingleInstance();
builder.RegisterType<MainModel>().AsSelf().SingleInstance();
builder.RegisterType<MainViewModel>().AsSelf().SingleInstance();
builder.RegisterType<MainView>().AsSelf();
Container = builder.Build();
using (var scope = Container.BeginLifetimeScope())
{
var window = scope.Resolve<MainView>();
window.ShowDialog();
}
Scope = Container.Resolve<ILifetimeScope>();
var window = Scope.Resolve<MainView>();
window.Show();
}
protected override void OnExit(ExitEventArgs e)
{
Scope.Dispose();
base.OnExit(e);
}
}
}