Commit d669d141 authored by mgravell's avatar mgravell

descriptor API: use index rather than constantly looking up the name

parent 733d1734
......@@ -70,7 +70,7 @@ internal static PropertyDescriptorCollection GetProperties(DapperTable table, ID
{
var type = row != null && row.TryGetValue(names[i], out var value) && value != null
? value.GetType() : typeof(object);
arr[i] = new RowBoundPropertyDescriptor(type, names[i]);
arr[i] = new RowBoundPropertyDescriptor(type, names[i], i);
}
return new PropertyDescriptorCollection(arr, true);
}
......@@ -84,20 +84,22 @@ internal static PropertyDescriptorCollection GetProperties(DapperTable table, ID
private sealed class RowBoundPropertyDescriptor : PropertyDescriptor
{
private readonly Type _type;
public RowBoundPropertyDescriptor(Type type, string name) : base(name, null)
=> _type = type;
public override bool CanResetValue(object component) => false;
public override void ResetValue(object component) => throw new NotSupportedException();
private readonly int _index;
public RowBoundPropertyDescriptor(Type type, string name, int index) : base(name, null)
{
_type = type;
_index = index;
}
public override bool CanResetValue(object component) => true;
public override void ResetValue(object component) => ((DapperRow)component).Remove(_index);
public override bool IsReadOnly => false;
public override bool ShouldSerializeValue(object component) => true;
public override bool ShouldSerializeValue(object component) => ((DapperRow)component).TryGetValue(_index, out _);
public override Type ComponentType => typeof(DapperRow);
public override Type PropertyType => _type;
public override object GetValue(object component)
=> ((IDictionary<string, object>)component).TryGetValue(Name, out var val) ? val : null;
=> ((DapperRow)component).TryGetValue(_index, out var val) ? (val ?? DBNull.Value): DBNull.Value;
public override void SetValue(object component, object value)
=> ((IDictionary<string, object>)component)[Name] = value;
=> ((DapperRow)component).SetValue(_index, value is DBNull ? null : value);
}
}
}
......
......@@ -40,8 +40,10 @@ private sealed class DeadValue
}
public bool TryGetValue(string key, out object value)
=> TryGetValue(table.IndexOfName(key), out value);
internal bool TryGetValue(int index, out object value)
{
var index = table.IndexOfName(key);
if (index < 0)
{ // doesn't exist
value = null;
......@@ -146,8 +148,10 @@ IEnumerator IEnumerable.GetEnumerator()
}
bool IDictionary<string, object>.Remove(string key)
=> Remove(table.IndexOfName(key));
internal bool Remove(int index)
{
int index = table.IndexOfName(key);
if (index < 0 || index >= values.Length || values[index] is DeadValue) return false;
values[index] = DeadValue.Default;
return true;
......@@ -177,6 +181,10 @@ private object SetValue(string key, object value, bool isAdd)
// then semantically, this value already exists
throw new ArgumentException("An item with the same key has already been added", nameof(key));
}
return SetValue(index, value);
}
internal object SetValue(int index, object value)
{
int oldLength = values.Length;
if (oldLength <= index)
{
......
......@@ -10,10 +10,12 @@ public BindingForm()
{
InitializeComponent();
SuspendLayout();
using (var conn = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=SSPI"))
{
mainGrid.DataSource = conn.Query("select * from sys.objects").AsList();
}
ResumeLayout();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment