Files
mixtape/zero.Core/Configuration/ZeroOptions.cs
T

80 lines
2.0 KiB
C#
Raw Normal View History

2021-11-22 14:29:22 +01:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System.Collections.Concurrent;
namespace zero.Configuration;
2021-11-19 14:59:24 +01:00
public class ZeroOptions : IZeroOptions
{
2021-11-24 15:49:25 +01:00
/// <inheritdoc />
public bool Initialized => !String.IsNullOrEmpty(For<RavenOptions>()?.Database);
/// <inheritdoc />
public string Version { get; set; }
/// <inheritdoc />
public string ZeroPath { get; set; }
/// <inheritdoc />
public TimeSpan TokenExpiration { get; set; }
2021-12-06 13:29:15 +01:00
/// <inheritdoc />
public List<ApplicationRegistration> Applications { get; set; } = new();
2021-11-24 15:49:25 +01:00
2021-11-29 00:38:55 +01:00
internal IServiceProvider ServiceProvider { get; set; }
2021-11-22 14:29:22 +01:00
protected ConcurrentDictionary<Type, object> OptionsCache { get; private set; } = new();
2021-11-19 14:59:24 +01:00
/// <inheritdoc />
2021-11-22 14:29:22 +01:00
public TOptions For<TOptions>() where TOptions : class
{
Type type = typeof(TOptions);
if (!OptionsCache.TryGetValue(type, out object value))
{
IOptions<TOptions> options = ServiceProvider.GetService<IOptions<TOptions>>();
value = options.Value;
OptionsCache.TryAdd(type, value);
}
return value as TOptions;
}
2021-11-19 14:59:24 +01:00
}
public interface IZeroOptions
{
2021-11-24 13:56:08 +01:00
/// <summary>
/// Path to the backoffice. Defaults to /zero
/// </summary>
string ZeroPath { get; set; }
2021-11-19 14:59:24 +01:00
/// <summary>
/// The currently active version
/// This should not be set manually, as it is used for setup and migrations and incremented automatically
/// </summary>
2021-11-24 13:56:08 +01:00
string Version { get; set; }
2021-11-19 14:59:24 +01:00
/// <summary>
2021-11-22 14:29:22 +01:00
/// Whether this zero instance is initialized (setup is completed)
2021-11-19 14:59:24 +01:00
/// </summary>
2021-11-22 14:29:22 +01:00
bool Initialized { get; }
2021-11-19 14:59:24 +01:00
2021-11-24 15:49:25 +01:00
/// <summary>
/// Expiration of a generated change token for an entity
/// </summary>
TimeSpan TokenExpiration { get; set; }
2021-12-06 13:29:15 +01:00
/// <summary>
/// Contains all registered applications
/// </summary>
List<ApplicationRegistration> Applications { get; set; }
2021-11-19 14:59:24 +01:00
/// <summary>
2021-11-22 14:29:22 +01:00
/// Get typed options (proxy to IOptions<TOptions>)
2021-11-19 14:59:24 +01:00
/// </summary>
2021-11-22 14:29:22 +01:00
TOptions For<TOptions>() where TOptions : class;
2021-11-19 14:59:24 +01:00
}