private void Serialize(Stream stream, IEnumerable <IModelEntity> entities, IEnumerable <IModelRelationship> relationships, HashSet <Identity> deletedEntities = null, HashSet <Identity> deletedRelationShips = null)
        {
            ISession session = null;

            if (Session.Current != null)
            {
                Session.Current.SetMode(SessionMode.Serializing);
            }
            else
            {
                IHyperstore          store = _domain.Store;
                SessionConfiguration sessionConfiguration = new SessionConfiguration();
                sessionConfiguration.Mode = SessionMode.Serializing;
                session = store.BeginSession(sessionConfiguration);
            }

            try
            {
                _monikers = new Dictionary <Identity, MonikerEntry>();
                SerializeEntities(entities, deletedEntities);
                SerializeRelationships(relationships, deletedRelationShips);
                _writer.Save(stream, _monikers.Values);
            }
            finally
            {
                if (session != null)
                {
                    session.AcceptChanges();
                    session.Dispose();
                }
                _monikers        = null;
                _monikerSequence = 0;
            }
        }