{"record":{"id":"640428e734d080c6","repo":"stride3d/stride","slug":"the-editorviewmodel-class-can-be-instanced-only-once","errorCode":null,"errorMessage":"The EditorViewModel class can be instanced only once.","messagePattern":"The EditorViewModel class can be instanced only once\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/editor/Stride.Core.Assets.Editor/ViewModel/EditorViewModel.cs","lineNumber":51,"sourceCode":"            serviceProvider.Get<IEditorDialogService>();\n\n            ClearMRUCommand = new AnonymousCommand(serviceProvider, () => ClearRecentFiles());\n            OpenSettingsWindowCommand = new AnonymousCommand(serviceProvider, OpenSettingsWindow);\n            OpenWebPageCommand = new AnonymousTaskCommand<string>(serviceProvider, OpenWebPage);\n#if DEBUG\n            DebugCommand = new AnonymousCommand(serviceProvider, DebugFunction);\n#endif\n\n            MRU = mru;\n            MRU.MostRecentlyUsedFiles.CollectionChanged += MostRecentlyUsedFiles_CollectionChanged;\n\n            serviceProvider.Get<IEditorDialogService>().RegisterDefaultTemplateProviders();\n\n            EditorName = editorName;\n            EditorVersionMajor = editorVersionMajor;\n            UpdateRecentFiles();\n            if (Instance != null)\n                throw new InvalidOperationException(\"The EditorViewModel class can be instanced only once.\");\n\n            Status = new StatusViewModel(ServiceProvider);\n            Status.PushStatus(\"Ready\");\n\n            Instance = this;\n        }\n\n        private void MostRecentlyUsedFiles_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)\n        {\n            UpdateRecentFiles();\n        }\n\n        /// <summary>\n        /// Gets the current instance of <see cref=\"EditorViewModel\"/>.\n        /// </summary>\n        public static EditorViewModel Instance { get; private set; }\n\n        /// <summary>","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/editor/Stride.Core.Assets.Editor/ViewModel/EditorViewModel.cs#L33-L69","documentation":"EditorViewModel enforces a singleton pattern per editor instance: a static Instance field tracks the one permitted instance. The constructor throws InvalidOperationException if Instance is already set, because two editor view models sharing static state would corrupt global editor state.","triggerScenarios":"Constructing EditorViewModel a second time in the same AppDomain/process, e.g. re-running editor initialization code or creating the view model in both a bootstrapper and a window.","commonSituations":"Unit tests constructing EditorViewModel repeatedly without resetting Instance; accidental double-initialization during app startup or editor reboot logic.","solutions":["Reuse the existing EditorViewModel.Instance instead of constructing a new one","Ensure the previous instance is torn down and Instance cleared before constructing again","Restructure code so EditorViewModel is created exactly once at startup"],"exampleFix":"// before\nvar editor = new EditorViewModel(serviceProvider, name, versionMajor);\n// after\nvar editor = EditorViewModel.Instance ?? new EditorViewModel(serviceProvider, name, versionMajor);","handlingStrategy":"validation","validationCode":"if (EditorViewModel.Instance != null) return EditorViewModel.Instance;","typeGuard":"bool CanCreateEditor() => EditorViewModel.Instance == null;","tryCatchPattern":"try { editor = new EditorViewModel(sp, name, ver); } catch (InvalidOperationException) { editor = EditorViewModel.Instance; }","preventionTips":["Create EditorViewModel exactly once at startup","Reuse the static Instance","Reset Instance in test teardown"],"tags":["singleton","viewmodel","initialization"],"backgroundTag":"invalid-state-transition","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}