Rewrite ReturnValueBinder to properly bind return values

features/nuget_autobuild
Matija Koželj 2 years ago
parent d92f0a6f82
commit 57a4f2fb05

@ -6,44 +6,36 @@ using Connected.Interop;
namespace Connected.Data.Storage namespace Connected.Data.Storage
{ {
internal static class ReturnValueBinder internal static class ReturnValueBinder
{ {
public static void Bind(IStorageOperation operation, object entity) public static void Bind(IStorageOperation operation, object entity)
{ {
List<PropertyInfo> properties = null; if (operation is null || operation.Parameters is null)
return;
foreach (var parameter in operation.Parameters) foreach (var parameter in operation.Parameters)
{ {
if (parameter.Direction != ParameterDirection.ReturnValue && parameter.Direction != ParameterDirection.Output)
continue;
if (parameter.Value == DBNull.Value) if (parameter.Value == DBNull.Value)
continue; continue;
if (properties is null) var properties = new List<PropertyInfo>();
{
properties = new List<PropertyInfo>();
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) foreach (var prop in all)
{ {
if (prop.FindAttribute<ReturnValueAttribute>() is not null) if (prop.FindAttribute<ReturnValueAttribute>() is not null)
properties.Add(prop); 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) if (property is null)
{ {
var candidates = new List<string> var candidates = new List<string>
{ {
parameter.Name.Replace("@", string.Empty) parameter.Name.Replace("@", string.Empty)
}; };
foreach (var prop in properties) foreach (var prop in properties)
{ {
@ -82,26 +74,28 @@ namespace Connected.Data.Storage
} }
} }
if (property is not null) if (property is null)
{ return;
var existingValue = property.GetValue(operation);
var overwriteAtt = property.FindAttribute<ReturnValueAttribute>();
switch (overwriteAtt.ValueBehavior) var existingValue = property.GetValue(operation);
{ var overwriteAtt = property.FindAttribute<ReturnValueAttribute>();
case PropertyValueBehavior.OverwriteDefault:
var defaultValue = property.PropertyType.GetDefault();
if (Equals(existingValue, defaultValue)) if (overwriteAtt is null)
property.SetValue(operation, parameter.Value); return;
break;
case PropertyValueBehavior.AlwaysOverwrite: switch (overwriteAtt.ValueBehavior)
property.SetValue(operation, parameter.Value); {
break; 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;
} }
} }
} }
} }
} }

Loading…
Cancel
Save