private void MergeRelationModels(DomainModel dm, RelationModelChange rc)
        {
            var toSave = dm.Relations[rc.Old.Name];

            foreach (var pc in rc.PropertyChanges)
            {
                switch (pc.Change)
                {
                case ChangeType.Created:
                    toSave.Properties.Add(pc.New);
                    break;

                case ChangeType.Deleted:
                    toSave.Properties.Remove(pc.Old);
                    break;

                case ChangeType.Modified:
                    toSave.Properties[pc.New.Name] = pc.New;
                    break;
                }
            }

            foreach (var ruleChange in rc.RuleChanges)
            {
                switch (ruleChange.Change)
                {
                case ChangeType.Created:
                    toSave.Rules.Add(ruleChange.New);
                    break;

                case ChangeType.Deleted:
                    toSave.Rules.Remove(ruleChange.Old);
                    break;
                }
            }
        }
        public DomainModelChanges CompareWithExisting(DomainModel dm)
        {
            //ensure relations
            foreach (var em in dm.Entities)
            {
                foreach (var rel in em.Relations)
                {
                    if (!dm.Relations.Contains(rel.Name))
                    {
                        dm.Relations.Add(rel);
                    }
                }
            }

            DomainModelChanges result = new DomainModelChanges();
            var entityChanges         = new List <EntityModelChange>();

            foreach (var ex in Domain.Entities)
            {
                if (!dm.Entities.Contains(ex.Name))
                {
                    entityChanges.Add(new EntityModelChange(ChangeType.Deleted, ex, null));
                }
                else
                {
                    EntityModelChange change = null;
                    var em = dm.Entities[ex.Name];
                    foreach (var listener in _changeListeners)
                    {
                        listener.BeforeSave(em);
                    }
                    if (TryGetChanges <EntityModelChange>(ex, em, out change))
                    {
                        entityChanges.Add(change);
                    }
                }
            }

            foreach (var en in dm.Entities)
            {
                if (!Domain.Entities.Contains(en.Name))
                {
                    foreach (var listener in _changeListeners)
                    {
                        listener.BeforeSave(en);
                    }

                    var ec          = new EntityModelChange(ChangeType.Created, null, en);
                    var propChanges = new List <ModelChange <PropertyModel> >();
                    foreach (var pm in en.Properties)
                    {
                        propChanges.Add(new ModelChange <PropertyModel>(ChangeType.Created, null, pm));
                    }
                    ec.PropertyChanges = propChanges;
                    entityChanges.Add(ec);
                }
            }
            result.EntityChanges = entityChanges;

            List <RelationModelChange> relChanges = new List <RelationModelChange>();

            foreach (var ex in Domain.Relations)
            {
                if (!dm.Relations.Contains(ex.Name))
                {
                    relChanges.Add(new RelationModelChange(ChangeType.Deleted, ex, null));
                }
                else
                {
                    //TODO: listeners
                    RelationModelChange change = null;
                    if (TryGetChanges <RelationModelChange>(ex, dm.Relations[ex.Name], out change))
                    {
                        relChanges.Add(change);
                    }
                }
            }

            foreach (var rm in dm.Relations)
            {
                if (!Domain.Relations.Contains(rm.Name))
                {
                    var rc          = new RelationModelChange(ChangeType.Created, null, rm);
                    var propChanges = new List <ModelChange <PropertyModel> >();
                    foreach (var pm in rm.Properties)
                    {
                        propChanges.Add(new ModelChange <PropertyModel>(ChangeType.Created, null, pm));
                    }
                    rc.PropertyChanges = propChanges;
                    relChanges.Add(rc);
                }
            }

            result.RelationChanges = relChanges;

            return(result);
        }