3 Commits

Author SHA1 Message Date
Railz
7e56187733 FillObject: Check if keys are Dbnull, so not set
ResolveForeignKeys: Check if key is set
2019-09-30 13:00:39 +02:00
Railz
ef73a51ab5 Fixed Init stackOverFlowException for recursive objects 2019-09-30 12:49:21 +02:00
Railz
d86e231bda Fixed ResolveForeignKeys:
Changed generic creation to reflective generation of new object
Fixed loading himself
2019-09-30 11:40:34 +02:00
3 changed files with 26 additions and 12 deletions

View File

@@ -26,14 +26,15 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
this._foreignKeyName = foreignKeyName; this._foreignKeyName = foreignKeyName;
} }
public void Init(FieldInfo fi, DbObject classAttribute) public void Init(FieldInfo fi, DbObject classAttribute, DbObject foreignClassAttribute = null)
{ {
this.parentField = fi; this.parentField = fi;
this.classAttribute = classAttribute; this.classAttribute = classAttribute;
this.foreignObjectType = fi.FieldType; this.foreignObjectType = fi.FieldType;
// Init foreign-object class // Init foreign-object class
DbObject foreignClassAttribute = ClassAction.Init(this.foreignObjectType); if(foreignClassAttribute == null)
foreignClassAttribute = ClassAction.Init(this.foreignObjectType);
// Check if something is weird // Check if something is weird
if (foreignClassAttribute.primaryKeyAttributes.Count < 1) throw new InvalidOperationException($"'{foreignClassAttribute.parentClassType.Name}' does not have a primaryKey."); if (foreignClassAttribute.primaryKeyAttributes.Count < 1) throw new InvalidOperationException($"'{foreignClassAttribute.parentClassType.Name}' does not have a primaryKey.");

View File

@@ -13,6 +13,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
public string _tableName; public string _tableName;
public Type parentClassType; public Type parentClassType;
public ConstructorInfo parentCInfo;
// All childrenAttributes // All childrenAttributes
public List<BaseAttribute> baseAttributes = new List<BaseAttribute>() { }; public List<BaseAttribute> baseAttributes = new List<BaseAttribute>() { };
@@ -35,6 +36,8 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
public void Init(Type classType) public void Init(Type classType)
{ {
this.parentClassType = classType; this.parentClassType = classType;
parentCInfo = classType.GetConstructor(Type.EmptyTypes);
this._tableName = this._tableName ?? classType.Name; // If no alternative table-name is specified, use the class-name this._tableName = this._tableName ?? classType.Name; // If no alternative table-name is specified, use the class-name
// Iterate thru all fields // Iterate thru all fields
@@ -66,7 +69,10 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
} }
else if (fi.GetCustomAttribute(typeof(DbForeignObject), true) is DbForeignObject fobj) // ForeignObjects else if (fi.GetCustomAttribute(typeof(DbForeignObject), true) is DbForeignObject fobj) // ForeignObjects
{ {
fobj.Init(fi, this); if(fi.FieldType == this.parentClassType) // Check if we are referencing ourselfes
fobj.Init(fi, this, this);
else
fobj.Init(fi, this);
this.foreignObjectAttributes.Add(fobj); this.foreignObjectAttributes.Add(fobj);
} }

View File

@@ -69,10 +69,16 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
if (baseAttribute._attributeName.ToLower() == data_keySet.Key.ToLower()) if (baseAttribute._attributeName.ToLower() == data_keySet.Key.ToLower())
{ {
object value = data_keySet.Value; object value = data_keySet.Value;
//if (baseAttribute.parentField.FieldType == typeof(Guid)) value = new Guid((string)value); // If its a guid, i need to convert if (!(value is DBNull)) // Check if value is empty
{
//if (baseAttribute.parentField.FieldType == typeof(Guid)) value = new Guid((string)value); // If its a guid, i need to convert
baseAttribute.parentField.SetValue(classObject, value);
break;
}
baseAttribute.parentField.SetValue(classObject, value);
break;
} }
} }
} }
@@ -104,14 +110,14 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
} }
public static T GetByPrimaryKey<T>(Type classType, Dictionary<string, object> primaryKeyData, Func<string, List<Dictionary<string, object>>> queryExecutor) where T: new() public static T GetByPrimaryKey<T>(Type classType, Dictionary<string, object> primaryKeyData, Func<string, List<Dictionary<string, object>>> queryExecutor) where T: new()
{ {
// Create new empty object
T obj = new T();
// Read dbObject-attribute // Read dbObject-attribute
DbObject dbObject = ClassAction.Init(classType); DbObject dbObject = ClassAction.Init(classType);
if (dbObject.primaryKeyAttributes.Count < 1) throw new InvalidOperationException($"No primaryKey found in '{classType.Name}'"); if (dbObject.primaryKeyAttributes.Count < 1) throw new InvalidOperationException($"No primaryKey found in '{classType.Name}'");
// Create new empty object
T obj = (T)dbObject.parentCInfo.Invoke(null);
// iterate thru them to check and fill object // iterate thru them to check and fill object
foreach (DbPrimaryKey primaryKeyAtt in dbObject.primaryKeyAttributes) foreach (DbPrimaryKey primaryKeyAtt in dbObject.primaryKeyAttributes)
{ {
@@ -295,13 +301,14 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
// Resolve foreignObjects // Resolve foreignObjects
foreach (DbForeignObject foreignObjectAtt in dbObject.foreignObjectAttributes) foreach (DbForeignObject foreignObjectAtt in dbObject.foreignObjectAttributes)
{ {
object foreignKey_value = foreignObjectAtt.foreignKeyAttribute.parentField.GetValue(classObject);
object foreignObject_value = foreignObjectAtt.parentField.GetValue(classObject); object foreignObject_value = foreignObjectAtt.parentField.GetValue(classObject);
// When its empty, get it & set it // When key is set and object is empty, get it & set it
if(foreignObject_value == null) if(foreignKey_value != null && foreignObject_value == null)
{ {
// Resolve it // Resolve it
foreignObject_value = GetByPrimaryKey<T>(classType, foreignObjectAtt.foreignKeyAttribute.parentField.GetValue(classObject), queryExecutor); foreignObject_value = GetByPrimaryKey<T>(foreignObjectAtt.foreignObjectType, foreignKey_value, queryExecutor);
foreignObjectAtt.parentField.SetValue(classObject, foreignObject_value); // Set the value foreignObjectAtt.parentField.SetValue(classObject, foreignObject_value); // Set the value
// Now scan the just resolved class to be able to set myself // Now scan the just resolved class to be able to set myself