Files
mixtape/zero.Core/Database/ZeroStore.cs
T

93 lines
2.6 KiB
C#
Raw Normal View History

2021-10-28 12:52:18 +02:00
using Raven.Client.Documents.Session;
2021-10-28 12:11:07 +02:00
using System.Collections.Generic;
using zero.Core.Options;
2020-11-15 00:59:51 +01:00
namespace zero.Core.Database
{
2021-10-28 12:52:18 +02:00
public class ZeroStore : IZeroStore
2020-11-15 00:59:51 +01:00
{
2021-10-28 12:52:18 +02:00
public ZeroStore(IZeroDocumentStore raven, IZeroOptions options) : base()
2020-11-15 00:59:51 +01:00
{
Options = options;
2021-10-28 12:52:18 +02:00
Raven = raven;
//Database = null;
2020-11-15 00:59:51 +01:00
}
2020-11-16 01:07:07 +01:00
protected IZeroOptions Options { get; set; }
2021-10-28 12:11:07 +02:00
protected Dictionary<string, IZeroDocumentSession> ScopedSessions { get; set; } = new();
2020-11-16 01:07:07 +01:00
/// <inheritdoc />
public string ResolvedDatabase { get; set; }
/// <inheritdoc />
2021-10-28 12:52:18 +02:00
public IZeroDocumentStore Raven { get; private set; }
2021-10-28 12:11:07 +02:00
/// <inheritdoc />
public IZeroDocumentSession Session(string database = null, ZeroSessionResolution resolution = ZeroSessionResolution.Reuse, SessionOptions options = null)
{
database ??= ResolvedDatabase;
options ??= new SessionOptions() { Database = database };
if (resolution == ZeroSessionResolution.Create)
{
2021-10-28 12:52:18 +02:00
return Raven.OpenAsyncSession(options) as IZeroDocumentSession;
2021-10-28 12:11:07 +02:00
}
if (!ScopedSessions.TryGetValue(database, out IZeroDocumentSession session))
{
2021-10-28 12:52:18 +02:00
session = Raven.OpenAsyncSession(options) as IZeroDocumentSession;
2021-10-28 12:11:07 +02:00
ScopedSessions.TryAdd(database, session);
}
if (session.IsDisposed)
{
2021-10-28 12:52:18 +02:00
session = Raven.OpenAsyncSession(options) as IZeroDocumentSession;
2021-10-28 12:11:07 +02:00
ScopedSessions.Remove(database);
ScopedSessions.TryAdd(database, session);
}
return session;
}
/// <inheritdoc />
public IZeroDocumentSession Session(bool global, string database = null, ZeroSessionResolution resolution = ZeroSessionResolution.Reuse, SessionOptions options = null)
{
return Session(global ? Options.Raven.Database : database, resolution, options);
}
}
2021-10-28 12:11:07 +02:00
public enum ZeroSessionResolution
{
Reuse = 0,
Create = 1
}
2021-10-28 12:52:18 +02:00
public interface IZeroStore
{
2020-11-16 01:07:07 +01:00
/// <summary>
/// The database which has been resolved from the current application
/// </summary>
string ResolvedDatabase { get; set; }
2020-11-15 00:59:51 +01:00
/// <summary>
/// Get underlying raven document store
/// </summary>
2021-10-28 12:52:18 +02:00
IZeroDocumentStore Raven { get; }
2020-11-15 17:07:27 +01:00
2021-10-28 12:11:07 +02:00
/// <summary>
/// Use a specific session
/// </summary>
IZeroDocumentSession Session(string database = null, ZeroSessionResolution resolution = ZeroSessionResolution.Reuse, SessionOptions options = null);
/// <summary>
/// Use a session for the core database
/// </summary>
IZeroDocumentSession Session(bool global, string database = null, ZeroSessionResolution resolution = ZeroSessionResolution.Reuse, SessionOptions options = null);
2020-11-15 00:59:51 +01:00
}
}