From 57a4f2fb058e9d6d5e60f5c8076c560989d6a4bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20Ko=C5=BEelj?= Date: Tue, 24 Jan 2023 17:30:16 +0100 Subject: [PATCH] Rewrite ReturnValueBinder to properly bind return values --- .../Storage/ReturnValueBinder.cs | 76 +++++++++---------- 1 file changed, 35 insertions(+), 41 deletions(-) diff --git a/src/Connected.Data/Storage/ReturnValueBinder.cs b/src/Connected.Data/Storage/ReturnValueBinder.cs index 3cd66f5..02cb420 100644 --- a/src/Connected.Data/Storage/ReturnValueBinder.cs +++ b/src/Connected.Data/Storage/ReturnValueBinder.cs @@ -6,44 +6,36 @@ using Connected.Interop; namespace Connected.Data.Storage { - internal static class ReturnValueBinder + internal static class ReturnValueBinder { public static void Bind(IStorageOperation operation, object entity) { - List properties = null; + if (operation is null || operation.Parameters is null) + return; foreach (var parameter in operation.Parameters) { - if (parameter.Direction != ParameterDirection.ReturnValue && parameter.Direction != ParameterDirection.Output) - continue; - if (parameter.Value == DBNull.Value) continue; - if (properties is null) - { - properties = new List(); + var properties = new List(); - var all = operation.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + var all = operation.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); - foreach (var prop in all) - { - if (prop.FindAttribute() is not null) - properties.Add(prop); - } + foreach (var prop in all) + { + if (prop.FindAttribute() is not null) + properties.Add(prop); } - PropertyInfo property = null; + PropertyInfo? property = null; - if (property is null) + foreach (var prop in properties) { - foreach (var prop in properties) + if (string.Equals(prop.Name, parameter.Name, StringComparison.Ordinal)) { - if (string.Equals(prop.Name, parameter.Name, StringComparison.Ordinal)) - { - property = prop; - break; - } + property = prop; + break; } } @@ -62,9 +54,9 @@ namespace Connected.Data.Storage if (property is null) { var candidates = new List - { - parameter.Name.Replace("@", string.Empty) - }; + { + parameter.Name.Replace("@", string.Empty) + }; foreach (var prop in properties) { @@ -82,26 +74,28 @@ namespace Connected.Data.Storage } } - if (property is not null) - { - var existingValue = property.GetValue(operation); - var overwriteAtt = property.FindAttribute(); + if (property is null) + return; - switch (overwriteAtt.ValueBehavior) - { - case PropertyValueBehavior.OverwriteDefault: - var defaultValue = property.PropertyType.GetDefault(); + var existingValue = property.GetValue(operation); + var overwriteAtt = property.FindAttribute(); - if (Equals(existingValue, defaultValue)) - property.SetValue(operation, parameter.Value); - break; - case PropertyValueBehavior.AlwaysOverwrite: - property.SetValue(operation, parameter.Value); - break; - } + if (overwriteAtt is null) + return; + + switch (overwriteAtt.ValueBehavior) + { + case PropertyValueBehavior.OverwriteDefault: + var defaultValue = property.PropertyType.GetDefault(); + + if (Equals(existingValue, defaultValue)) + property.SetValue(entity, parameter.Value); + break; + case PropertyValueBehavior.AlwaysOverwrite: + property.SetValue(entity, parameter.Value); + break; } } } - } }