From e8e1fb993deedbdadce5755a350fa1e9231b0b30 Mon Sep 17 00:00:00 2001 From: Railz Date: Mon, 1 Apr 2019 18:49:37 +0200 Subject: [PATCH] Removed ignoreDataMismatch and implemented runDataLossChecks instead --- Database-Attribute_System/ClassAction.cs | 37 ++++++++++++++++++++---- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/Database-Attribute_System/ClassAction.cs b/Database-Attribute_System/ClassAction.cs index 01a4a30..3c9a032 100644 --- a/Database-Attribute_System/ClassAction.cs +++ b/Database-Attribute_System/ClassAction.cs @@ -14,8 +14,8 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System /// /// Given object (marked with Db-attributes) /// The data - /// This disables errors when class-field is missing an data-attribute - public static void FillObject(T classObject, Dictionary data, bool ignoreDataAttributeNotInClass = false) + /// This checks if any class-field and data-attribute does not exists in either (Slower) + public static void FillObject(T classObject, Dictionary data, bool runDataLossChecks = true) { Type classType = classObject.GetType(); @@ -24,6 +24,36 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System // Get class-fields Dictionary dbFields = Function.ReadDbClassFields(classObject); + if (runDataLossChecks) + { + // Check every data-attribute for match in class-fields + foreach (KeyValuePair data_keySet in data) + { + bool isFound = false; + foreach (KeyValuePair field_keySet in dbFields) + { + if (field_keySet.Key.ToLower() == data_keySet.Key.ToLower()) + isFound = true; + } + + if (!isFound) + throw new InvalidOperationException($"Could not fill object. Data-Attribute '{data_keySet.Key}' was not found in class!"); + } + // Check every class-field for match in data-attributes + foreach (KeyValuePair field_keySet in dbFields) + { + bool isFound = false; + foreach (KeyValuePair data_keySet in data) + { + if (field_keySet.Key.ToLower() == data_keySet.Key.ToLower()) + isFound = true; + } + + if (!isFound) + throw new InvalidOperationException($"Could not fill object. Class-field '{field_keySet.Key}' was not found in data!"); + } + } + // Iterate through data foreach (KeyValuePair data_keySet in data) { @@ -41,9 +71,6 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System break; } } - - // If the field was not filled, throw an error if it will not be ignored - if (!ignoreDataAttributeNotInClass && !dataIsSet) throw new InvalidOperationException($"Could not fill object. Data-Attribute '{data_keySet.Key}' was not found class!"); } }