Skip to content

Commit

Permalink
honor the DataMemberAttribute.IsRequired property.
Browse files Browse the repository at this point in the history
Require *at least* one property to be bound.
  • Loading branch information
MarkPflug committed Nov 16, 2023
1 parent 474263a commit b52bd8b
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions source/Sylvan.Data/CompiledDataBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ ReadOnlyCollection<DbColumn> logicalSchema

// stores contextual state used by the binder.
var state = new List<object>();

var physicalColumns = new HashSet<DbColumn>();
foreach (var col in physicalSchema)
{
Expand Down Expand Up @@ -188,7 +188,9 @@ ReadOnlyCollection<DbColumn> logicalSchema
.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.Where(p => p.SetMethod != null && p.GetCustomAttribute<IgnoreDataMemberAttribute>() == null)
.ToDictionary(p => p.Name, p => p, StringComparer.OrdinalIgnoreCase);

HashSet<string> requiredProperties = new();
// always require at least 1 property to be bound.
int boundPropertyCount = 0;
foreach (var kvp in properties.ToArray())
{
var key = kvp.Key;
Expand All @@ -198,6 +200,11 @@ ReadOnlyCollection<DbColumn> logicalSchema
var columnOrdinal = memberAttribute?.Order;
var columnName = memberAttribute?.Name ?? property.Name;

if (memberAttribute?.IsRequired == true)
{
requiredProperties.Add(property.Name);
}

var col = GetCol(columnOrdinal, columnName);
Type colType = col?.DataType ?? typeof(object);
if (col == null)
Expand Down Expand Up @@ -413,12 +420,13 @@ ReadOnlyCollection<DbColumn> logicalSchema
expr
);
}

boundPropertyCount++;
var setExpr = Expression.Call(itemParam, setter, expr);
bodyExpressions.Add(Expression.Assign(idxVar, ordinalExpr));
bodyExpressions.Add(setExpr);
physicalColumns.Remove(col);
properties.Remove(key);
requiredProperties.Remove(key);
}
var props = properties.ToArray();
foreach (var kvp in props)
Expand Down Expand Up @@ -448,19 +456,26 @@ ReadOnlyCollection<DbColumn> logicalSchema
string[]? unboundProperties = null;
string[]? unboundColumns = null;

if (opts.BindingMode.HasFlag(DataBindingMode.AllProperties) && properties.Any())
if (boundPropertyCount == 0 || opts.BindingMode.HasFlag(DataBindingMode.AllProperties) && properties.Any())
{
unboundProperties = properties.Select(p => p.Value.Name).ToArray();
}

if (opts.BindingMode.HasFlag(DataBindingMode.AllColumns) && physicalColumns.Any())
else
{
if (requiredProperties.Any())
{
unboundProperties = requiredProperties.ToArray();
}
}

if (boundPropertyCount == 0 || opts.BindingMode.HasFlag(DataBindingMode.AllColumns) && physicalColumns.Any())
{
unboundColumns = physicalColumns.Select(p => p.ColumnName).ToArray();
}

if (unboundColumns != null || unboundProperties != null)
{
throw new UnboundMemberException(unboundProperties, unboundColumns);
throw new UnboundMemberException(unboundProperties ?? Array.Empty<string>(), unboundColumns ?? Array.Empty<string>());
}

this.state = state.ToArray();
Expand Down

0 comments on commit b52bd8b

Please sign in to comment.