public override void ElementAdded(ElementAddedEventArgs e)
        {
            if (e.ModelElement != null)
                if (e.ModelElement.Store.TransactionManager.CurrentTransaction != null)
                    if (e.ModelElement.Store.TransactionManager.CurrentTransaction.IsSerializing)
                        return;

            if (e.ModelElement == null)
                return;

            if (ImmutabilityExtensionMethods.GetLocks(e.ModelElement) != Locks.None)
                return;

            ModelContext modelContext = e.ModelElement as ModelContext;
            if (modelContext != null)
            {
                if (modelContext.ViewContext == null)
                {
                    ViewContext viewContext = new ViewContext(modelContext.Store);
                    viewContext.DomainModelTreeView = new DomainModelTreeView(modelContext.Store);
                    viewContext.DiagramView = new DiagramView(modelContext.Store);
                    modelContext.ViewContext = viewContext;
                    modelContext.MetaModel.View.ViewContexts.Add(viewContext);
                }

                if (modelContext.DiagramClasses.Count == 0)
                {
                    DesignerDiagramClass ddC = new DesignerDiagramClass(modelContext.Store);
                    //ddC.Name = "DesignerDiagram";
                    ddC.Name = NameHelper.GetUniqueName(modelContext.Store, DesignerDiagramClass.DomainClassId);
                    ddC.Title = "Designer";
                    modelContext.DiagramClasses.Add(ddC);

                    DiagramClassView vm = new DiagramClassView(modelContext.Store);
                    vm.IsExpanded = true;
                    vm.DiagramClass = ddC;
                    modelContext.ViewContext.DiagramView.DiagramClassViews.Add(vm);
                }

                DomainClass domainClass = modelContext.Store.ElementFactory.CreateElement(DomainClass.DomainClassId) as DomainClass;
                domainClass.IsDomainModel = true;
                Microsoft.VisualStudio.Modeling.ElementOperations elementOperations = new Microsoft.VisualStudio.Modeling.ElementOperations(modelContext.Store as IServiceProvider, modelContext.Store.DefaultPartition);
                Microsoft.VisualStudio.Modeling.ElementGroup elementGroup = new Microsoft.VisualStudio.Modeling.ElementGroup(modelContext.Store.DefaultPartition);
                elementGroup.Add(domainClass);
                elementGroup.MarkAsRoot(domainClass);
                elementOperations.MergeElementGroup(modelContext, elementGroup);
                domainClass.Name = NameHelper.GetUniqueName(modelContext.Store, DomainClass.DomainClassId);

                SerializedDomainModel child = new SerializedDomainModel(domainClass.Store);
                child.DomainClass = domainClass;
                child.SerializationName = domainClass.SerializationName;

                if (modelContext.SerializationModel == null)
                {
                    modelContext.SerializationModel = new SerializationModel(modelContext.Store);
                }

                modelContext.SerializationModel.SerializedDomainModel = child;
                SerializationHelper.AddSerializationDomainProperties(domainClass.Store, domainClass);
            }
        }
        private static void FixUpSerializedDomainModel(ModelContext model)
        {
            foreach (DomainClass domainClass in model.Classes)
                if (domainClass.IsDomainModel)
                {
                    SerializedDomainModel child = new SerializedDomainModel(domainClass.Store);
                    child.DomainClass = domainClass;
                    child.SerializationName = domainClass.SerializationName;

                    model.SerializationModel.SerializedDomainModel = child;
                    SerializationHelper.AddSerializationDomainProperties(domainClass.Store, domainClass);
                }
                else
                {
                    SerializationDomainClassAddRule.OnDomainClassAdded(domainClass);
                    domainClass.SerializedDomainClass.SerializationName = domainClass.SerializationName;
                }

            foreach (DomainRelationship relationship in model.Relationships)
            {
                if (relationship is EmbeddingRelationship)
                {
                    SerializationDomainRelationshipAddRule.OnEmbeddingRelationshipAdded(relationship as EmbeddingRelationship);
                    (relationship as EmbeddingRelationship).SerializedEmbeddingRelationship.SerializationName = relationship.SerializationName;
                }
                else
                {
                    SerializationDomainRelationshipAddRule.OnReferenceRelationshipAdded(relationship as ReferenceRelationship);
                    (relationship as ReferenceRelationship).SerializedReferenceRelationship.SerializationName = relationship.SerializationName;
                }
            }

            foreach (DomainClass domainClass in model.Classes)
            {
                SerializationHelper.UpdateSerializationDomainProperties(domainClass.Store, domainClass.SerializedDomainClass, domainClass);
                SerializationHelper.UpdateSerializationDomainRoles(domainClass.Store, domainClass.SerializedDomainClass, domainClass);

                foreach (DomainProperty p in domainClass.Properties)
                    p.SerializedDomainProperty.IsSerializationNameTracking = TrackingEnum.True;
            }

            foreach (DomainRelationship relationship in model.Relationships)
            {
                if (relationship is EmbeddingRelationship)
                {
                    SerializationHelper.UpdateSerializationDomainProperties(relationship.Store, (relationship as EmbeddingRelationship).SerializedEmbeddingRelationship, relationship);
                }
                else
                {
                    SerializationHelper.UpdateSerializationDomainProperties(relationship.Store, (relationship as ReferenceRelationship).SerializedReferenceRelationship, relationship);
                }

                foreach (DomainProperty p in relationship.Properties)
                    p.SerializedDomainProperty.IsSerializationNameTracking = TrackingEnum.True;
            }

            // correct IsSerializationNameTracking values

            foreach (DomainClass domainClass in model.Classes)
            {
                if (domainClass.SerializationName != domainClass.Name)
                    domainClass.IsSerializationNameTracking = TrackingEnum.False;

                domainClass.SerializedDomainClass.IsSerializationNameTracking = domainClass.IsSerializationNameTracking;

                foreach (DomainProperty p in domainClass.Properties)
                {
                    if (p.Name != p.SerializationName)
                        p.IsSerializationNameTracking = TrackingEnum.False;

                    p.SerializedDomainProperty.IsSerializationNameTracking = p.IsSerializationNameTracking;

                    if (p.Name == "DomainFilePath")
                        p.SerializedDomainProperty.OmitProperty = true;
                }
            }

            foreach (DomainRelationship relationship in model.Relationships)
            {
                if (relationship is EmbeddingRelationship)
                {
                    EmbeddingRelationship emb = relationship as EmbeddingRelationship;
                    if (emb.SerializationName != emb.Name)
                        emb.IsSerializationNameTracking = TrackingEnum.False;

                    emb.SerializedEmbeddingRelationship.IsSerializationNameTracking = emb.IsSerializationNameTracking;
                }
                else
                {
                    ReferenceRelationship refRel = relationship as ReferenceRelationship;
                    if (refRel.SerializationName != refRel.Name)
                        refRel.IsSerializationNameTracking = TrackingEnum.False;

                    refRel.SerializedReferenceRelationship.IsSerializationNameTracking = refRel.IsSerializationNameTracking;
                }

                foreach (DomainProperty p in relationship.Properties)
                {
                    if (p.Name != p.SerializationName)
                        p.IsSerializationNameTracking = TrackingEnum.True;

                    p.SerializedDomainProperty.IsSerializationNameTracking = p.IsSerializationNameTracking;
                }
            }
        }