依赖注入顺序修改

This commit is contained in:
hsc
2026-06-11 10:42:00 +08:00
parent 420ca0ffd6
commit 9c661200b9
6 changed files with 27 additions and 32 deletions

View File

@@ -52,11 +52,22 @@ namespace MainModule.ViewModels
// 每个 AutomatedTestingViewModel 实例创建独立的容器作用域
_scope = container.CreateScope();
_globalInfo =container.Resolve<GlobalInfo>();
// 在该作用域内解析 ScopedContext —— 当前作用域唯一
_scopedContext = _scope.Resolve<ScopedContext>();
_stepRunning = _scope.Resolve<StepRunning>();
_systemConfig = _scope.Resolve<SystemConfig>();
// 关键:从同一个 _scope 解析 5 个子 VMDI 会把同一个 ScopedContext 注入它们
//加载Json数据
if (ConfigService.IsExit(_globalInfo.CurrentOpeningScope))
{
string filePath = System.IO.Path.Combine(_systemConfig.SystemPath, $"{_globalInfo.CurrentOpeningScope}.json");
if (System.IO.File.Exists(filePath))
{
string json = System.IO.File.ReadAllText(filePath);
Newtonsoft.Json.JsonConvert.PopulateObject(json, _systemConfig);
}
}
//容器解析顺序不要改变!!!
_scopedContext = _scope.Resolve<ScopedContext>();
_deviceManager = _scope.Resolve<DeviceManager>();
_stepRunning = _scope.Resolve<StepRunning>();
// 从同一个 _scope 解析 5 个子 VMDI 会把同一个 ScopedContext 注入它们
CommandTreeVM = _scope.Resolve<CommandTreeViewModel>();
StepsManagerVM = _scope.Resolve<StepsManagerViewModel>();
SingleStepEditVM = _scope.Resolve<SingleStepEditViewModel>();
@@ -111,17 +122,6 @@ namespace MainModule.ViewModels
if (navigationContext.Parameters.ContainsKey("Name"))
{
TestStatus = navigationContext.Parameters.GetValue<string>("Name");
if (ConfigService.IsExit(TestStatus))
{
string filePath = System.IO.Path.Combine(_systemConfig.SystemPath, $"{TestStatus}.json");
if (System.IO.File.Exists(filePath))
{
string json = System.IO.File.ReadAllText(filePath);
// 🔥 关键:把 json 数据直接灌入当前实例,对象引用没有任何改变
Newtonsoft.Json.JsonConvert.PopulateObject(json, _systemConfig);
}
}
_globalInfo.ContextDic.Add(TestStatus, _scopedContext);
_globalInfo.StepRunningDic.Add(TestStatus, _stepRunning);
_globalInfo.ScopeDic.Add(TestStatus, _scope);
@@ -146,7 +146,6 @@ namespace MainModule.ViewModels
_scopedContext.Program.StepCollection = program.StepCollection;
_scopedContext.Program.ErrorStepCollection = program.ErrorStepCollection;
_scopedContext.CurrentFilePath = filePath;
_deviceManager = _scope.Resolve<DeviceManager>();
}
}