Kaynağa Gözat

Added configuration providers, to use globalConfiguration even server-side

Kenric Nugteren 1 yıl önce
ebeveyn
işleme
55f6e7119d

+ 6 - 0
InABox.Core/Client/Client.cs

@@ -147,6 +147,12 @@ namespace InABox.Clients
             new Client<TEntity>().Delete(entity, auditNote);
         }
 
+        public static void Delete<TEntity>(TEntity entity, string auditNote, Action<TEntity, Exception?> callback)
+            where TEntity : Entity, IRemotable, IPersistent, new()
+        {
+            new Client<TEntity>().Delete(entity, auditNote, callback);
+        }
+
         public static void Delete<TEntity>(IEnumerable<TEntity> entities, string auditNote)
             where TEntity : Entity, IRemotable, IPersistent, new()
         {

+ 62 - 0
InABox.Core/Configuration/ConfigurationProvider.cs

@@ -0,0 +1,62 @@
+using InABox.Clients;
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace InABox.Core
+{
+    public interface IConfigurationProvider<T>
+        where T : Entity, IRemotable, IPersistent, new()
+    {
+        string UserID { get; }
+
+        T[] Load(Filter<T> filter);
+
+        CoreTable Query(Filter<T> filter, Columns<T> columns, SortOrder<T>? sort = null);
+
+        void Save(T entity);
+
+        void Save(IEnumerable<T> entities);
+
+        void Delete(T entity, Action<Exception?>? callback = null);
+    }
+    public class ClientConfigurationProvider<T> : IConfigurationProvider<T>
+        where T : Entity, IRemotable, IPersistent, new()
+    {
+        public static ClientConfigurationProvider<T> Provider = new ClientConfigurationProvider<T>();
+
+        public string UserID => ClientFactory.UserID;
+
+        public T[] Load(Filter<T> filter)
+        {
+            return Client.Query(filter, null).ToArray<T>();
+        }
+
+        public CoreTable Query(Filter<T> filter, Columns<T> columns, SortOrder<T>? sort)
+        {
+            return Client.Query(filter, columns, sort);
+        }
+
+        public void Save(T entity)
+        {
+            Client.Save(entity, "", (o, e) => { });
+        }
+
+        public void Save(IEnumerable<T> entities)
+        {
+            Client.Save(entities, "", (o, e) => { });
+        }
+
+        public void Delete(T entity, Action<Exception?>? callback = null)
+        {
+            if(callback != null)
+            {
+                Client.Delete(entity, "", (o, e) => { callback(e); });
+            }
+            else
+            {
+                Client.Delete(entity, "");
+            }
+        }
+    }
+}

+ 19 - 27
InABox.Core/Configuration/GlobalConfiguration.cs

@@ -1,7 +1,6 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
-using InABox.Clients;
 using InABox.Core;
 
 namespace InABox.Configuration
@@ -30,8 +29,15 @@ namespace InABox.Configuration
 
     public class GlobalConfiguration<T> : Configuration<T>, IGlobalConfiguration where T : IGlobalConfigurationSettings, new()
     {
-        public GlobalConfiguration(string section = "") : base(section)
+        private IConfigurationProvider<GlobalSettings> Provider;
+
+        public GlobalConfiguration(string section = "") : this(section, ClientConfigurationProvider<GlobalSettings>.Provider)
+        {
+        }
+
+        public GlobalConfiguration(string section, IConfigurationProvider<GlobalSettings> provider) : base(section)
         {
+            Provider = provider;
         }
 
         private GlobalSettings NewSettings(string? section = null)
@@ -42,12 +48,9 @@ namespace InABox.Configuration
         private GlobalSettings GetSettings()
         {
             GlobalSettings? result = null;
-            if (ClientFactory.ClientType != null)
-            {
-                var client = new Client<GlobalSettings>();
-                result = client.Load(new Filter<GlobalSettings>(x => x.Section).IsEqualTo(typeof(T).Name).And(x => x.Key).IsEqualTo(Section))
-                    .FirstOrDefault();
-            }
+            result = Provider.Load(
+                    new Filter<GlobalSettings>(x => x.Section).IsEqualTo(typeof(T).Name).And(x => x.Key).IsEqualTo(Section))
+                .FirstOrDefault();
             return result ?? NewSettings();
         }
 
@@ -89,11 +92,10 @@ namespace InABox.Configuration
             {
                 filter.And(x => x.Key).InList(sections.AsArray());
             }
-            var data = new Client<GlobalSettings>().Query(
+            var data = Provider.Query(
                 filter,
                 Columns.None<GlobalSettings>().Add(x => x.Key, x => x.Contents),
-                new SortOrder<GlobalSettings>(x => x.Key)
-            );
+                new SortOrder<GlobalSettings>(x => x.Key));
             foreach (var row in data.Rows)
             {
                 var tObj = Serialization.Deserialize<T>(row.Get<GlobalSettings, string>(c => c.Contents));
@@ -116,7 +118,7 @@ namespace InABox.Configuration
 
             setting.Contents = Serialization.Serialize(obj);
 
-            new Client<GlobalSettings>().Save(setting, "", (o, e) => { });
+            Provider.Save(setting);
         }
 
         public override void SaveAll(Dictionary<string, T> objs, bool reset = false)
@@ -126,10 +128,9 @@ namespace InABox.Configuration
                 ConfigurationCache.Add(ConfigurationCacheType.Global, section, obj);
             }
 
-            var client = new Client<GlobalSettings>();
             var data = reset 
                 ? new Dictionary<string, GlobalSettings>()
-                : client.Load(new Filter<GlobalSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
+                : Provider.Load(new Filter<GlobalSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
                     .And(x => x.Key).InList(objs.Keys.ToArray())).ToDictionary(x => x.Key, x => x);
 
             var settings = new List<GlobalSettings>(objs.Count);
@@ -143,34 +144,25 @@ namespace InABox.Configuration
                 setting.Contents = contents;
                 settings.Add(setting);
             }
-            client.Save(settings, "", (o, e) => { });
+            Provider.Save(settings);
         }
 
         public override void Delete(Action<Exception?>? callback = null)
         {
             ConfigurationCache.Clear<T>(ConfigurationCacheType.Global, Section);
 
-            var client = new Client<GlobalSettings>();
-
-            var result = client.Load(new Filter<GlobalSettings>(x => x.Section).IsEqualTo(typeof(T).Name).And(x => x.Key).IsEqualTo(Section))
+            var result = Provider.Load(new Filter<GlobalSettings>(x => x.Section).IsEqualTo(typeof(T).Name).And(x => x.Key).IsEqualTo(Section))
                 .FirstOrDefault();
 
             if (result != null)
             {
-                if(callback != null)
-                {
-                    client.Delete(result, "", (o, e) => { callback(e); });
-                }
-                else
-                {
-                    client.Delete(result, "");
-                }
+                Provider.Delete(result, callback);
             }
         }
 
         public override string[] Sections()
         {
-            var result = new Client<GlobalSettings>().Query(
+            var result = Provider.Query(
                 new Filter<GlobalSettings>(x => x.Section).IsEqualTo(typeof(T).Name),
                 Columns.None<GlobalSettings>().Add(x => x.Key),
                 new SortOrder<GlobalSettings>(x => x.Key)

+ 27 - 35
InABox.Core/Configuration/UserConfiguration.cs

@@ -1,7 +1,6 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
-using InABox.Clients;
 using InABox.Core;
 
 namespace InABox.Configuration
@@ -33,36 +32,39 @@ namespace InABox.Configuration
 
     public class UserConfiguration<T> : Configuration<T> where T : IUserConfigurationSettings, new()
     {
-        public UserConfiguration(string section = "") : base(section)
+        private IConfigurationProvider<UserSettings> Provider;
+
+        public UserConfiguration(string section = "") : this(section, ClientConfigurationProvider<UserSettings>.Provider)
+        {
+        }
+
+        public UserConfiguration(string section, IConfigurationProvider<UserSettings> provider) : base(section)
         {
+            Provider = provider;
         }
 
         private UserSettings NewSettings(string? section = null)
         {
-            return new UserSettings { Section = typeof(T).Name, Key = section ?? Section, UserID = ClientFactory.UserID };
+            return new UserSettings { Section = typeof(T).Name, Key = section ?? Section, UserID = Provider.UserID };
         }
 
         private UserSettings GetSettings()
         {
             UserSettings result = null;
-            if (ClientFactory.ClientType != null)
-            {
-                var client = new Client<UserSettings>();
+            var filter = new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
+                .And(x => x.Key).IsEqualTo(Section)
+                .And(x => x.UserID).IsEqualTo(Provider.UserID);
 
-                var filter = new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
-                    .And(x => x.Key).IsEqualTo(Section)
-                    .And(x => x.UserID).IsEqualTo(ClientFactory.UserID);
 
+            result = Provider.Query(filter, Columns.All<UserSettings>())
+                .ToObjects<UserSettings>().FirstOrDefault();
 
-                result = client.Query(filter, Columns.All<UserSettings>())
-                    .ToObjects<UserSettings>().FirstOrDefault();
-            }
             return result ?? NewSettings();
         }
 
         private string GetKey(string? section = null)
         {
-            return string.Format("{0}.{1}", section ?? Section, ClientFactory.UserID);
+            return string.Format("{0}.{1}", section ?? Section, Provider.UserID);
         }
 
         public override T Load(bool useCache = true)
@@ -97,12 +99,12 @@ namespace InABox.Configuration
         {
             var result = new Dictionary<string, T>();
             var filter = new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
-                .And(x => x.UserID).IsEqualTo(ClientFactory.UserID);
+                .And(x => x.UserID).IsEqualTo(Provider.UserID);
             if (sections != null)
             {
                 filter.And(x => x.Key).InList(sections.AsArray());
             }
-            var data = new Client<UserSettings>().Query(
+            var data = Provider.Query(
                 filter,
                 Columns.None<UserSettings>().Add(x => x.Key, x => x.Contents),
                 new SortOrder<UserSettings>(x => x.Key)
@@ -127,7 +129,7 @@ namespace InABox.Configuration
 
             setting.Contents = Serialization.Serialize(obj);
 
-            new Client<UserSettings>().Save(setting, "", (o, e) => { });
+            Provider.Save(setting);
         }
 
         public override void SaveAll(Dictionary<string, T> objs, bool reset = false)
@@ -137,13 +139,12 @@ namespace InABox.Configuration
                 ConfigurationCache.Add(ConfigurationCacheType.User, GetKey(section), obj);
             }
 
-            var client = new Client<UserSettings>();
             var data = reset 
                 ? new Dictionary<string,UserSettings>() 
-                : client.Query(
+                : Provider.Query(
                     new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
                         .And(x => x.Key).InList(objs.Keys.ToArray())
-                        .And(x => x.UserID).IsEqualTo(ClientFactory.UserID),
+                        .And(x => x.UserID).IsEqualTo(Provider.UserID),
                     Columns.All<UserSettings>())
                 .ToObjects<UserSettings>()
                 .ToDictionary(x => x.Key, x => x);
@@ -159,47 +160,38 @@ namespace InABox.Configuration
                 setting.Contents = contents;
                 settings.Add(setting);
             }
-            client.Save(settings, "", (o, e) => { });
+            Provider.Save(settings);
         }
 
         public override void Delete(Action<Exception?>? callback = null)
         {
             ConfigurationCache.Clear<T>(ConfigurationCacheType.User, GetKey());
 
-            var client = new Client<UserSettings>();
-
             var filter = new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name)
                 .And(x => x.Key).IsEqualTo(Section)
-                .And(x => x.UserID).IsEqualTo(ClientFactory.UserID);
+                .And(x => x.UserID).IsEqualTo(Provider.UserID);
 
             UserSettings? result;
             try
             {
-                result = client.Load(filter).FirstOrDefault();
+                result = Provider.Load(filter).FirstOrDefault();
             }
             catch (Exception e)
             {
-                Logger.Send(LogType.Error, ClientFactory.UserID, "Error in UserConfiguration.Delete(): " + CoreUtils.FormatException(e));
+                Logger.Send(LogType.Error, Provider.UserID, "Error in UserConfiguration.Delete(): " + CoreUtils.FormatException(e));
                 result = null;
             }
 
             if (result != null)
             {
-                if(callback != null)
-                {
-                    client.Delete(result, "", (o, e) => { callback(e); });
-                }
-                else
-                {
-                    client.Delete(result, "");
-                }
+                Provider.Delete(result, callback);
             }
         }
 
         public override string[] Sections()
         {
-            var result = new Client<UserSettings>().Query(
-                new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name).And(x => x.UserID).IsEqualTo(ClientFactory.UserID),
+            var result = Provider.Query(
+                new Filter<UserSettings>(x => x.Section).IsEqualTo(typeof(T).Name).And(x => x.UserID).IsEqualTo(Provider.UserID),
                 Columns.None<UserSettings>().Add(x => x.Key),
                 new SortOrder<UserSettings>(x => x.Key)
             ).Rows.Select(r => r.Get<UserSettings, string>(c => c.Key)).Distinct().ToArray();

+ 43 - 0
InABox.Database/DbConfigurationProvider.cs

@@ -0,0 +1,43 @@
+using InABox.Core;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace InABox.Database;
+public class DbConfigurationProvider<T> : IConfigurationProvider<T>
+    where T : Entity, IRemotable, IPersistent, new()
+{
+    public string UserID { get; init; }
+
+    public DbConfigurationProvider(string userID)
+    {
+        UserID = userID;
+    }
+
+    public void Delete(T entity, Action<Exception?>? callback = null)
+    {
+        DbFactory.Provider.Delete(entity, UserID);
+    }
+
+    public T[] Load(Filter<T> filter)
+    {
+        return DbFactory.Provider.Load(filter);
+    }
+
+    public CoreTable Query(Filter<T> filter, Columns<T> columns, SortOrder<T>? sort = null)
+    {
+        return DbFactory.Provider.Query(filter, columns, sort);
+    }
+
+    public void Save(T entity)
+    {
+        DbFactory.Provider.Save(entity);
+    }
+
+    public void Save(IEnumerable<T> entities)
+    {
+        DbFactory.Provider.Save(entities);
+    }
+}

+ 1 - 1
inabox.wpf/DynamicGrid/DynamicGrid.cs

@@ -1683,7 +1683,7 @@ public abstract class DynamicGrid<T> : DynamicGrid, IDynamicGridUIComponentParen
         if (!IsEnabled || bRefreshing)
             return false;
 
-        if (rows == null || !rows.Any())
+        if (rows == null || rows.Length == 0)
         {
 
             if (!CanCreateItems())