Example #1
0
        public virtual void Open(string storeFile, bool autoCreate = false)
        {
            Guard.NotNullOrEmpty(() => storeFile, storeFile);

            using (tracer.StartActivity(Resources.PatternManager_TraceOpeningStore, storeFile))
                using (new MouseCursor(System.Windows.Input.Cursors.Wait))
                {
                    if (this.IsOpen)
                    {
                        this.Close();
                    }

                    this.StoreFile = storeFile;
                    //tracer.TraceData(TraceEventType.Verbose, Resources.PatternManager_OpeningStoreEventId, storeFile);

                    if (autoCreate && (!File.Exists(storeFile) || new FileInfo(storeFile).Length == 0))
                    {
                        storeFile = CreateEmptyStateFile(storeFile);
                    }

                    //// Do NOT dispose the DSL state or it becomes null in the model elements!!
                    if (VerifyDslVersion(storeFile))
                    {
                        var result = new Dsl.SerializationResult();

                        var dslStore = new Dsl.Store(this.serviceProvider, typeof(Dsl.CoreDomainModel), typeof(ProductStateStoreDomainModel));

                        using (var tx = dslStore.TransactionManager.BeginTransaction(Resources.PatternManager_OpenTransactionDescription, true))
                        {
                            using (new StorePropertyBag(dslStore, ProductState.IsSerializingKey, true))
                            {
                                // Flag the state as being deserialized. This is
                                // required by the ProductState class to ignore
                                // element added events until deserialization is
                                // completed.

                                var store = ProductStateStoreSerializationHelper.Instance.LoadModel(result, dslStore, storeFile, null, null, null);

                                if (!result.Failed)
                                {
                                    this.productStore = store;
                                    tx.Commit();
                                }
                            }
                        }

                        if (!result.Failed)
                        {
                            this.IsOpen = true;

                            this.Store.TransactionCommited += this.OnTransactionCommitedDoSave;
                            this.Store.ElementInstantiated += this.OnElementInstantiated;
                            this.Store.ElementLoaded       += this.OnElementLoaded;
                            this.Store.ElementCreated      += this.OnElementCreated;
                            this.Store.ElementDeleted      += this.OnElementDeleted;
                            this.Store.ElementDeleting     += this.OnElementDeleting;

                            this.RaiseIsOpenChanged();

                            tracer.Info(Resources.PatternManager_OpenedStore, this.StoreFile);
                            //tracer.TraceData(TraceEventType.Verbose, Resources.PatternManager_OpenedStoreEventId, storeFile);
                        }
                        else
                        {
                            this.messageService.ShowError(
                                Properties.Resources.PatternManager_FailedToOpenStore);

                            tracer.Error(Properties.Resources.PatternManager_DeSerializationFailed, this.StoreFile);
                            result.ForEach(msg => tracer.Warn(msg.ToString()));
                        }
                    }
                }
        }