From 2f243e6ccfb56dc8cf24feb266ec3e008e678a7b Mon Sep 17 00:00:00 2001 From: Railz Date: Thu, 11 Jul 2019 12:42:13 +0200 Subject: [PATCH] Catching known errors that may occur whilst resolving unknown object --- Database-Attribute_System/ClassAction.cs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/Database-Attribute_System/ClassAction.cs b/Database-Attribute_System/ClassAction.cs index c5a4f37..29a784c 100644 --- a/Database-Attribute_System/ClassAction.cs +++ b/Database-Attribute_System/ClassAction.cs @@ -135,8 +135,15 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System if (!dataMatchFound) throw new InvalidOperationException($"PrimaryKey='{primaryKeyAtt.parentField.Name}' is missing."); } - ResolveByPrimaryKey(obj, queryExecutor); - + try + { + ResolveByPrimaryKey(obj, queryExecutor); + }catch(InvalidOperationException) + { + // If there is no result, return null + return default(T); + } + return obj; } @@ -258,13 +265,16 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System /// /// Given object (marked with Db-attributes) /// Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue] - public static void ResolveByPrimaryKey(T classObject, Func>> queryExecutor) + public static void ResolveByPrimaryKey(T classObject, Func>> queryExecutor, bool throwExceptions = true) { string query = QueryBuilder.SelectByPrimaryKey(classObject); // Generate query List> dataSet = queryExecutor(query); // Execute - if (dataSet.Count == 0) throw new InvalidOperationException($"Cannot fetch '{typeof(T).Name}' by primary key/s. No results!"); - FillObject(classObject, dataSet[0]); // Fill the object + if (dataSet.Count == 0) + { + if (throwExceptions) throw new InvalidOperationException($"Cannot fetch '{typeof(T).Name}' by primary key/s. No results!"); + } + else FillObject(classObject, dataSet[0]); // Fill the object } ///