/// <summary>
        /// Use data contract XML for document serialization.
        /// </summary>
        /// <param name="cfg"></param>
        /// <returns></returns>
        public static EventStoreConfiguration WithDataContractDocumentSerializer(this EventStoreConfiguration cfg)
        {
            IDocumentSerializer serializer = new DataContractDocumentSerializer();

            Xyperico.Agres.EventStore.EventStoreConfigurationExtensions.SetDocumentSerializer(cfg, serializer);
            return(cfg);
        }
        public static void SetAppendOnlyStore(EventStoreConfiguration cfg, IAppendOnlyStore store)
        {
            if (cfg.ContainsKey(AppendOnlyStore_SettingsKey))
            throw new InvalidOperationException("You should not configure append-only back-end store for event store twice.");

              Condition.Requires(cfg, "cfg").IsNotNull();
              Condition.Requires(store, "store").IsNotNull();
              cfg.Set(AppendOnlyStore_SettingsKey, store);
              Logger.DebugFormat("Using {0} as storage engine for EventStore", store);
        }
        public static void SetEventPublisher(EventStoreConfiguration cfg, IEventPublisher publisher)
        {
            if (cfg.ContainsKey(DocumentSerializer_SettingsKey))
            {
                throw new InvalidOperationException("You should not configure event publisher for event store twice.");
            }

            Condition.Requires(cfg, "cfg").IsNotNull();
            Condition.Requires(publisher, "publisher").IsNotNull();
            cfg.Set(EventPublisher_SettingsKey, publisher);
        }
        public static void SetMessageSerializer(EventStoreConfiguration cfg, ISerializer serializer)
        {
            if (cfg.ContainsKey(MessageSerializer_SettingsKey))
            {
                throw new InvalidOperationException("You should not configure message serializer for event store twice.");
            }

            Condition.Requires(cfg, "cfg").IsNotNull();
            Condition.Requires(serializer, "serializer").IsNotNull();
            cfg.Set(MessageSerializer_SettingsKey, serializer);
            Logger.DebugFormat("Using {0} as serializer for messages in event store", serializer);
        }
        public static void SetAppendOnlyStore(EventStoreConfiguration cfg, IAppendOnlyStore store)
        {
            if (cfg.ContainsKey(AppendOnlyStore_SettingsKey))
            {
                throw new InvalidOperationException("You should not configure append-only back-end store for event store twice.");
            }

            Condition.Requires(cfg, "cfg").IsNotNull();
            Condition.Requires(store, "store").IsNotNull();
            cfg.Set(AppendOnlyStore_SettingsKey, store);
            Logger.DebugFormat("Using {0} as storage engine for EventStore", store);
        }
        /// <summary>
        /// Use file based storing of events and internal data structures.
        /// </summary>
        /// <param name="cfg"></param>
        /// <param name="baseDir"></param>
        /// <returns></returns>
        public static EventStoreConfiguration WithFileDocumentStore(this EventStoreConfiguration cfg, string baseDir)
        {
            if (cfg.ContainsKey(DocumentStoreFactory_SettingsKey))
            {
                throw new InvalidOperationException("You should not configure document store for event store twice.");
            }

            Logger.Debug("Using plain files for storing documents used in event store");
            Condition.Requires(cfg, "cfg").IsNotNull();
            Condition.Requires(baseDir, "baseDir").IsNotNull();

            IDocumentSerializer documentSerializer = cfg.Get <IDocumentSerializer>(DocumentSerializer_SettingsKey);

            if (documentSerializer == null)
            {
                throw new InvalidOperationException("No document serializer has been configured for event store.");
            }
            IDocumentStoreFactory docStoreFactory = new FileDocumentStoreFactory(baseDir, documentSerializer);

            cfg.Set(DocumentStoreFactory_SettingsKey, docStoreFactory);

            return(cfg);
        }
        /// <summary>
        /// No more configuration needed for event store - now configure something else or start event store.
        /// </summary>
        /// <param name="cfg"></param>
        /// <returns></returns>
        public static BaseConfiguration Done(this EventStoreConfiguration cfg)
        {
            IAppendOnlyStore aStore            = cfg.Get <IAppendOnlyStore>(AppendOnlyStore_SettingsKey);
            ISerializer      messageSerializer = cfg.Get <ISerializer>(MessageSerializer_SettingsKey);

            if (aStore == null)
            {
                throw new InvalidOperationException("Mising storage mechanism (IAppendOnlyStore) for event store.");
            }
            if (messageSerializer == null)
            {
                throw new InvalidOperationException("Missing event serializer for event store.");
            }

            EventStoreDB eStore = new EventStoreDB(aStore, messageSerializer);

            cfg.Set(EventStoreDB_SettingsKey, eStore);

            IObjectContainer container = Xyperico.Agres.Configuration.ObjectContainerConfigurationExtensions.GetObjectContainer(cfg);

            container.RegisterInstance <IEventStore>(eStore);

            return(new BaseConfiguration(cfg));
        }
        public static void SetDocumentSerializer(EventStoreConfiguration cfg, IDocumentSerializer serializer)
        {
            if (cfg.ContainsKey(DocumentSerializer_SettingsKey))
            throw new InvalidOperationException("You should not configure document serializer for event store twice.");

              Condition.Requires(cfg, "cfg").IsNotNull();
              Condition.Requires(serializer, "serializer").IsNotNull();
              cfg.Set(DocumentSerializer_SettingsKey, serializer);
        }
        public static void SetMessageSerializer(EventStoreConfiguration cfg, ISerializer serializer)
        {
            if (cfg.ContainsKey(MessageSerializer_SettingsKey))
            throw new InvalidOperationException("You should not configure message serializer for event store twice.");

              Condition.Requires(cfg, "cfg").IsNotNull();
              Condition.Requires(serializer, "serializer").IsNotNull();
              cfg.Set(MessageSerializer_SettingsKey, serializer);
              Logger.DebugFormat("Using {0} as serializer for messages in event store", serializer);
        }