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