|
@@ -9,9 +9,27 @@ namespace InABox.Core
|
|
|
public static class DatabaseSchema
|
|
|
{
|
|
|
// {className: {propertyName: property}}
|
|
|
- private static Dictionary<string, Dictionary<string, IProperty>> _Properties { get; }
|
|
|
+ private static Dictionary<string, Dictionary<string, IProperty>> _properties { get; }
|
|
|
= new Dictionary<string, Dictionary<string, IProperty>>();
|
|
|
|
|
|
+ private static Dictionary<Type, List<IProperty>> _entityLinks { get; } = new Dictionary<Type, List<IProperty>>();
|
|
|
+
|
|
|
+ private static List<IProperty> GetEntityLinks(Type t)
|
|
|
+ {
|
|
|
+ return _entityLinks.GetValueOrDefault(t) ?? new List<IProperty>();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void InitializeEntityLinks(BaseObject obj)
|
|
|
+ {
|
|
|
+ foreach(var linkProp in GetEntityLinks(obj.GetType()))
|
|
|
+ {
|
|
|
+ var link = (Activator.CreateInstance(linkProp.PropertyType) as IEntityLink)!;
|
|
|
+ linkProp.Setter()(obj, link);
|
|
|
+ link.SetLinkedParent(obj);
|
|
|
+ link.SetLinkedPath(linkProp.Name);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/*private static BaseEditor GetPropertyEditor(Type type, PropertyInfo property, string propertyName)
|
|
|
{
|
|
|
BaseEditor editor = new NullEditor();
|
|
@@ -60,6 +78,23 @@ namespace InABox.Core
|
|
|
return editor;
|
|
|
}*/
|
|
|
|
|
|
+ private static void RegisterEntityLink(IProperty property)
|
|
|
+ {
|
|
|
+ lock (_updatelock)
|
|
|
+ {
|
|
|
+ var type = property.Parent?.PropertyType ?? property.ClassType;
|
|
|
+ if(type != null)
|
|
|
+ {
|
|
|
+ if (!_entityLinks.TryGetValue(type, out var properties))
|
|
|
+ {
|
|
|
+ properties = new List<IProperty>();
|
|
|
+ _entityLinks[type] = properties;
|
|
|
+ }
|
|
|
+ properties.Add(property);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private static void RegisterProperties(Type master, Type type, string prefix, StandardProperty? parent)
|
|
|
{
|
|
|
try
|
|
@@ -72,7 +107,7 @@ namespace InABox.Core
|
|
|
(x.DeclaringType.IsSubclassOf(typeof(BaseObject))
|
|
|
|| x.DeclaringType.IsSubclassOf(typeof(BaseEditor)))
|
|
|
); //.OrderBy(x=>x.Name);
|
|
|
- var classProps = _Properties.GetValueOrDefault(classname);
|
|
|
+ var classProps = _properties.GetValueOrDefault(classname);
|
|
|
|
|
|
foreach (var prop in properties)
|
|
|
{
|
|
@@ -159,6 +194,10 @@ namespace InABox.Core
|
|
|
Parent = parent,
|
|
|
Property = prop
|
|
|
};
|
|
|
+ if (newProperty.IsEntityLink)
|
|
|
+ {
|
|
|
+ RegisterEntityLink(newProperty);
|
|
|
+ }
|
|
|
|
|
|
if (prop.PropertyType.GetInterfaces().Contains(typeof(IEntityLink)) ||
|
|
|
prop.PropertyType.GetInterfaces().Contains(typeof(IEnclosedEntity)) ||
|
|
@@ -209,12 +248,18 @@ namespace InABox.Core
|
|
|
{
|
|
|
lock (_updatelock)
|
|
|
{
|
|
|
- if (!_Properties.ContainsKey(entry.Class))
|
|
|
- _Properties[entry.Class] = new Dictionary<string, IProperty>();
|
|
|
- _Properties[entry.Class][entry.Name] = entry;
|
|
|
+ if (!_properties.ContainsKey(entry.Class))
|
|
|
+ _properties[entry.Class] = new Dictionary<string, IProperty>();
|
|
|
+ _properties[entry.Class][entry.Name] = entry;
|
|
|
+
|
|
|
//var dict = _Properties.GetOrAdd(entry.Class, className => new Dictionary<string, IProperty>());
|
|
|
//dict.TryAdd(entry.Name, entry);
|
|
|
}
|
|
|
+
|
|
|
+ if (entry.IsEntityLink)
|
|
|
+ {
|
|
|
+ RegisterEntityLink(entry);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public static void Load(CustomProperty[] customproperties)
|
|
@@ -226,7 +271,7 @@ namespace InABox.Core
|
|
|
private static void CheckProperties(Type type)
|
|
|
{
|
|
|
var entityName = type.EntityName();
|
|
|
- var props = _Properties.GetValueOrDefault(entityName);
|
|
|
+ var props = _properties.GetValueOrDefault(entityName);
|
|
|
var hasprops = props?.Any(x => x.Value is StandardProperty) == true;
|
|
|
if (type.IsSubclassOf(typeof(BaseObject)) && !hasprops)
|
|
|
RegisterProperties(type);
|
|
@@ -236,7 +281,7 @@ namespace InABox.Core
|
|
|
{
|
|
|
CheckProperties(type);
|
|
|
var entityName = type.EntityName();
|
|
|
- return _Properties.GetValueOrDefault(entityName)?.Select(x => x.Value) ?? Array.Empty<IProperty>();
|
|
|
+ return _properties.GetValueOrDefault(entityName)?.Select(x => x.Value) ?? Array.Empty<IProperty>();
|
|
|
}
|
|
|
|
|
|
public static IProperty? Property(Type type, string name)
|
|
@@ -245,7 +290,7 @@ namespace InABox.Core
|
|
|
var entityName = type.EntityName();
|
|
|
|
|
|
//IProperty prop = _Properties.ToArray().FirstOrDefault(x => (x.Class.Equals(type.EntityName())) && (x.Name.Equals(name)));
|
|
|
- var prop = _Properties.GetValueOrDefault(entityName)?.GetValueOrDefault(name);
|
|
|
+ var prop = _properties.GetValueOrDefault(entityName)?.GetValueOrDefault(name);
|
|
|
|
|
|
// Walk up the inheritance tree, see if an ancestor has this property
|
|
|
if (prop == null && type.BaseType != null)
|