/// <summary> /// Gets this window's unity-mapped model and connection string /// </summary> /// <returns></returns> private bool ResolveDependencies(out string message) { message = ""; try { // CONFIGURE UNITY (DEPENDENCY INJECTION) // _dependencyResolver = new UnityResolver(); // THE POINT HERE IS TO INSTANTIATE THE MODEL (IMPLEMENTING 'IAppViewModel') FROM UNITY DI // ADD ANY RUNTIME ARGUMENTS TO THE CONSTRUCTOR OF THE INJECTED OBJECT // THE 'disp' CONSTRUCTOR IS NOT USED ANYMORE, BUT THIS OPERATIONAL CODE IS LEFT IN // PLACE FOR WHEN A CONSTUCTOR ARGUMENT IS NEEDED FOR THE INJECTED TYPE // ANY ARGUMENTS NOT USED ARE IGNORED Dictionary <string, object> modelConstructorArguments = new Dictionary <string, object>(); // key is the actual constructor parameter name, value is the value to pass in. modelConstructorArguments.Add("disp", this.Dispatcher); // GET THE UNITY MAPPING NAME USED TO REFERENCE THE IMPLEMENTATION OF THE MODEL FROM // THIS DLL'S CONFIG FILE - NOT THE EXECUTING APP.CONFIG // THE dll.config SETTING IS DESIGNATED BY "<WINDOW CLASS NAME> + '_model'" // <!-- Unity Mapping name for Model class used for view 'MainWindow.xaml' --> // <appSettings> // <add key="MainWindow_model" value="ImporterViewModel"/> // </appSettings> object modelUnityMappingName = null; ConfigurationManager.GetDllConfigAppSetting(this.GetType().Name + "_model", out modelUnityMappingName); if (string.IsNullOrEmpty((string)modelUnityMappingName)) { throw new Exception("Could not find dll configuration setting '" + this.Name + "_model' which is used to specify the Unity DI mapping for the view model object"); } // ASSIGN THE INJECTED MODEL TO THIS WINOW'S DataContext - this._appViewModel = UnityResolver.Instance.GetObjectByAlias <IAppViewModel>((string)modelUnityMappingName, modelConstructorArguments); if (this._appViewModel == null) { _logger.Error("Unable to create view model - check unity log and configuration"); } else { // ASSIGN THE MODEL TO THIS WINOW'S DataContext this._appViewModel.ModelEvent += OnViewModelEvent; if (this._appViewModel.ReadyForUse) { this.DataContext = this._appViewModel; this._appViewModel.Initialize(); } else { this._appViewModel.Created += new CreationComplete(delegate() { this.DataContext = this._appViewModel; this._appViewModel.Initialize(); }); } _logger.InfoFormat("DataContext assigned to {0}.", this._appViewModel.GetType().FullName); this._appViewModel.ModelEvent -= OnViewModelEvent; return(true); } } catch (System.Security.Authentication.AuthenticationException) { message = "Login Failure - see system administrator"; this._logger.ErrorFormat("Login Failure while running under {0}\\{1}", Environment.UserDomainName, Environment.UserName); this._appViewModel.Dispose(); } catch (Exception ex) { this._logger.ErrorFormat("Unity resolver error - {0}{1}", ex.Message, ex.InnerException == null ? "" : ", " + ex.InnerException.Message); message = "Error resolving dependencies. Please check log and make necessary adjustments to 'Unity.config' file"; } return(false); }
public AppWindowViewModel(IAppViewModel appViewModel) { _appViewModel = appViewModel; }