Fixed ResolveForeignKeys:

Changed generic creation to reflective generation of new object
Fixed loading himself
master
Railz 5 years ago
parent 178c92383b
commit d86e231bda

@ -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

@ -104,14 +104,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)
{ {
@ -301,7 +301,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
if(foreignObject_value == null) if(foreignObject_value == null)
{ {
// Resolve it // Resolve it
foreignObject_value = GetByPrimaryKey<T>(classType, foreignObjectAtt.foreignKeyAttribute.parentField.GetValue(classObject), queryExecutor); foreignObject_value = GetByPrimaryKey<T>(foreignObjectAtt.foreignObjectType, foreignObjectAtt.foreignKeyAttribute.parentField.GetValue(classObject), 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

Loading…
Cancel
Save