Catching known errors that may occur whilst resolving unknown object

master
Railz 6 years ago
parent 1c2c8f6e76
commit 2f243e6ccf

@ -135,7 +135,14 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
if (!dataMatchFound) throw new InvalidOperationException($"PrimaryKey='{primaryKeyAtt.parentField.Name}' is missing."); if (!dataMatchFound) throw new InvalidOperationException($"PrimaryKey='{primaryKeyAtt.parentField.Name}' is missing.");
} }
try
{
ResolveByPrimaryKey<T>(obj, queryExecutor); ResolveByPrimaryKey<T>(obj, queryExecutor);
}catch(InvalidOperationException)
{
// If there is no result, return null
return default(T);
}
return obj; return obj;
} }
@ -258,13 +265,16 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
/// <param name="classObject">Given object (marked with Db-attributes)</param> /// <param name="classObject">Given object (marked with Db-attributes)</param>
/// <param name="queryExecutor">Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue]</param> /// <param name="queryExecutor">Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue]</param>
public static void ResolveByPrimaryKey<T>(T classObject, Func<string, List<Dictionary<string, object>>> queryExecutor) public static void ResolveByPrimaryKey<T>(T classObject, Func<string, List<Dictionary<string, object>>> queryExecutor, bool throwExceptions = true)
{ {
string query = QueryBuilder.SelectByPrimaryKey(classObject); // Generate query string query = QueryBuilder.SelectByPrimaryKey(classObject); // Generate query
List<Dictionary<string, object>> dataSet = queryExecutor(query); // Execute List<Dictionary<string, object>> dataSet = queryExecutor(query); // Execute
if (dataSet.Count == 0) throw new InvalidOperationException($"Cannot fetch '{typeof(T).Name}' by primary key/s. No results!"); if (dataSet.Count == 0)
FillObject(classObject, dataSet[0]); // Fill the object {
if (throwExceptions) throw new InvalidOperationException($"Cannot fetch '{typeof(T).Name}' by primary key/s. No results!");
}
else FillObject(classObject, dataSet[0]); // Fill the object
} }
/// <summary> /// <summary>

Loading…
Cancel
Save