// ========================================================================== // Store.cs // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex Group // All rights reserved. // ========================================================================== using System; using System.Threading.Tasks; using Squidex.Infrastructure.EventSourcing; namespace Squidex.Infrastructure.States { public sealed class Store : IStore { private readonly Action invalidate; private readonly IServiceProvider services; private readonly IStreamNameResolver streamNameResolver; private readonly IEventStore eventStore; private readonly IEventDataFormatter eventDataFormatter; public Store( IEventStore eventStore, IEventDataFormatter eventDataFormatter, IServiceProvider services, IStreamNameResolver streamNameResolver, Action invalidate = null) { this.eventStore = eventStore; this.eventDataFormatter = eventDataFormatter; this.invalidate = invalidate; this.services = services; this.streamNameResolver = streamNameResolver; } public IPersistence WithEventSourcing(string key, Func, Task> applyEvent) { return CreatePersistence(key, null, applyEvent); } public IPersistence WithSnapshots(string key, Func applySnapshot) { return CreatePersistence(key, applySnapshot, null); } public IPersistence WithSnapshotsAndEventSourcing(string key, Func applySnapshot, Func, Task> applyEvent) { return CreatePersistence(key, applySnapshot, applyEvent); } private IPersistence CreatePersistence(string key, Func applySnapshot, Func, Task> applyEvent) { Guard.NotNullOrEmpty(key, nameof(key)); var snapshotStore = (ISnapshotStore)services.GetService(typeof(ISnapshotStore)); return new Persistence(key, invalidate, eventStore, eventDataFormatter, snapshotStore, streamNameResolver, applySnapshot, applyEvent); } } }