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
{
internal static class ReturnValueBinder
internal static class ReturnValueBinder
{
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)
{
if (parameter.Direction != ParameterDirection.ReturnValue && parameter.Direction != ParameterDirection.Output)
continue;
if (parameter.Value == DBNull.Value)
continue;
if (properties is null)
{
properties = new List<PropertyInfo>();
var 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)
{
if (prop.FindAttribute<ReturnValueAttribute>() is not null)
properties.Add(prop);
}
foreach (var prop in all)
{
if (prop.FindAttribute<ReturnValueAttribute>() 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<string>
{
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<ReturnValueAttribute>();
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<ReturnValueAttribute>();
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;
}
}
}
}
}

Loading…
Cancel
Save