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