Rewrite ReturnValueBinder to properly bind return values
This commit is contained in:
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>();
|
||||||
|
|
||||||
|
var all = operation.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
||||||
|
|
||||||
|
foreach (var prop in all)
|
||||||
{
|
{
|
||||||
properties = new List<PropertyInfo>();
|
if (prop.FindAttribute<ReturnValueAttribute>() is not null)
|
||||||
|
properties.Add(prop);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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>();
|
||||||
|
|
||||||
|
if (overwriteAtt is null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
switch (overwriteAtt.ValueBehavior)
|
||||||
{
|
{
|
||||||
var existingValue = property.GetValue(operation);
|
case PropertyValueBehavior.OverwriteDefault:
|
||||||
var overwriteAtt = property.FindAttribute<ReturnValueAttribute>();
|
var defaultValue = property.PropertyType.GetDefault();
|
||||||
|
|
||||||
switch (overwriteAtt.ValueBehavior)
|
if (Equals(existingValue, defaultValue))
|
||||||
{
|
property.SetValue(entity, parameter.Value);
|
||||||
case PropertyValueBehavior.OverwriteDefault:
|
break;
|
||||||
var defaultValue = property.PropertyType.GetDefault();
|
case PropertyValueBehavior.AlwaysOverwrite:
|
||||||
|
property.SetValue(entity, parameter.Value);
|
||||||
if (Equals(existingValue, defaultValue))
|
break;
|
||||||
property.SetValue(operation, parameter.Value);
|
|
||||||
break;
|
|
||||||
case PropertyValueBehavior.AlwaysOverwrite:
|
|
||||||
property.SetValue(operation, parameter.Value);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user