瀏覽代碼

Some utility functions and allowing DatabaseSchema to use non-baseObjects

Kenric Nugteren 11 月之前
父節點
當前提交
ebd15c8040

+ 17 - 7
InABox.Core/CoreExpression.cs

@@ -8,6 +8,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Text.RegularExpressions;
+using Expressive.Exceptions;
 
 namespace InABox.Core
 {
@@ -118,7 +119,9 @@ namespace InABox.Core
 
         public static List<string> GetModelVariables(Type modelType)
         {
-            return CoreUtils.PropertyList(modelType, x => true).Select(x => x.Name).ToList();
+            var props = DatabaseSchema.Properties(modelType).Select(x => x.Name).ToList();
+            props.Sort();
+            return props;
         }
         public static List<string> GetModelVariables<TModel>() where TModel : IExpressionModel
             => GetModelVariables(typeof(TModel));
@@ -162,8 +165,8 @@ namespace InABox.Core
             }
             return default;
         }
-        [return: MaybeNull]
-        public TReturn Evaluate(TModel model)
+
+        public Result<TReturn, Exception> Evaluate(TModel model)
         {
             var values = new Dictionary<string, object?>();
             foreach(var variable in ReferencedVariables)
@@ -171,12 +174,19 @@ namespace InABox.Core
                 values[variable] = CoreUtils.GetPropertyValue(model, variable);
             }
 
-            var result = base.Evaluate(values);
-            if(result is TReturn ret)
+            try
             {
-                return ret;
+                var result = base.Evaluate(values);
+                if(result is TReturn ret)
+                {
+                    return Result.Ok(ret);
+                }
+                return Result.Ok<TReturn>(default);
+            }
+            catch (Exception e)
+            {
+                return Result.Error(e);
             }
-            return default;
         }
     }
 }

+ 2 - 4
InABox.Core/DatabaseSchema/DatabaseSchema.cs

@@ -99,9 +99,7 @@ namespace InABox.Core
             {
                 var properties = CoreUtils.PropertyList(
                     type,
-                    x => !x.PropertyType.IsInterface &&
-                        (x.DeclaringType.IsSubclassOf(typeof(BaseObject))
-                        || x.DeclaringType.IsSubclassOf(typeof(BaseEditor)))
+                    x => !x.PropertyType.IsInterface && x.DeclaringType != typeof(BaseObject)
                 );
 
                 var subObjects = new List<Tuple<Type, string>>();
@@ -295,7 +293,7 @@ namespace InABox.Core
             {
                 var props = _properties.GetValueOrDefault(type);
                 var hasprops = props?.Any(x => x.Value is StandardProperty) == true;
-                if (!hasprops && type.IsSubclassOf(typeof(BaseObject)))
+                if (!hasprops)
                 {
                     RegisterProperties(type);
                     return _properties.GetValueOrDefault(type);

+ 4 - 0
InABox.Core/Filter.cs

@@ -368,6 +368,10 @@ namespace InABox.Core
             result.Value = value;
             return result;
         }
+
+        public static Filter<T> All<T>() => new Filter<T>().All();
+
+        public static Filter<T> None<T>() => new Filter<T>().None();
     }
 
     public interface IFilter2<T>

+ 3 - 0
InABox.Core/ILookupDefinition.cs

@@ -50,6 +50,9 @@ namespace InABox.Core
         where TChild : class
     { }
 
+    /// <summary>
+    /// Define a lookup definition for a given property. The generator must derive from <see cref="LookupDefinitionGenerator{TLookup, TEntity}"/>.
+    /// </summary>
     public class LookupDefinitionAttribute : Attribute
     {
         public Type Generator { get; set; }