using System; using System.Collections.Concurrent; using LiteDB; using PCL.Core.App.IoC; namespace PCL.Core.App.Database; [LifecycleService(LifecycleState.Loading)] public class DatabaseService() : GeneralService("database", "数据库管理") { private static readonly ConcurrentDictionary _Instances = new(); /// public override void Stop() { foreach (var instance in _Instances.Values) { instance.Dispose(); } _Instances.Clear(); } /// /// Get the database connenction from specified connection path.
/// If not exists, a new connection will be created and cached. ///
/// Getted connenction instance. /// Throw if connection path is invalid. public static LiteDatabase GetConnection(string connectionPath) { if (string.IsNullOrWhiteSpace(connectionPath)) { throw new ArgumentException("Connection string canot be null or whitespace.", nameof(connectionPath)); } return _Instances.GetOrAdd(connectionPath, cp => new LiteDatabase(cp)); } }