Skip to content

Commit d669d14

Browse files
committed
descriptor API: use index rather than constantly looking up the name
1 parent 733d173 commit d669d14

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

Dapper/SqlMapper.DapperRow.Descriptor.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ internal static PropertyDescriptorCollection GetProperties(DapperTable table, ID
7070
{
7171
var type = row != null && row.TryGetValue(names[i], out var value) && value != null
7272
? value.GetType() : typeof(object);
73-
arr[i] = new RowBoundPropertyDescriptor(type, names[i]);
73+
arr[i] = new RowBoundPropertyDescriptor(type, names[i], i);
7474
}
7575
return new PropertyDescriptorCollection(arr, true);
7676
}
@@ -84,20 +84,22 @@ internal static PropertyDescriptorCollection GetProperties(DapperTable table, ID
8484
private sealed class RowBoundPropertyDescriptor : PropertyDescriptor
8585
{
8686
private readonly Type _type;
87-
public RowBoundPropertyDescriptor(Type type, string name) : base(name, null)
88-
=> _type = type;
89-
90-
public override bool CanResetValue(object component) => false;
91-
public override void ResetValue(object component) => throw new NotSupportedException();
92-
87+
private readonly int _index;
88+
public RowBoundPropertyDescriptor(Type type, string name, int index) : base(name, null)
89+
{
90+
_type = type;
91+
_index = index;
92+
}
93+
public override bool CanResetValue(object component) => true;
94+
public override void ResetValue(object component) => ((DapperRow)component).Remove(_index);
9395
public override bool IsReadOnly => false;
94-
public override bool ShouldSerializeValue(object component) => true;
96+
public override bool ShouldSerializeValue(object component) => ((DapperRow)component).TryGetValue(_index, out _);
9597
public override Type ComponentType => typeof(DapperRow);
9698
public override Type PropertyType => _type;
9799
public override object GetValue(object component)
98-
=> ((IDictionary<string, object>)component).TryGetValue(Name, out var val) ? val : null;
100+
=> ((DapperRow)component).TryGetValue(_index, out var val) ? (val ?? DBNull.Value): DBNull.Value;
99101
public override void SetValue(object component, object value)
100-
=> ((IDictionary<string, object>)component)[Name] = value;
102+
=> ((DapperRow)component).SetValue(_index, value is DBNull ? null : value);
101103
}
102104
}
103105
}

Dapper/SqlMapper.DapperRow.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ int ICollection<KeyValuePair<string, object>>.Count
4040
}
4141

4242
public bool TryGetValue(string key, out object value)
43+
=> TryGetValue(table.IndexOfName(key), out value);
44+
45+
internal bool TryGetValue(int index, out object value)
4346
{
44-
var index = table.IndexOfName(key);
4547
if (index < 0)
4648
{ // doesn't exist
4749
value = null;
@@ -146,8 +148,10 @@ void IDictionary<string, object>.Add(string key, object value)
146148
}
147149

148150
bool IDictionary<string, object>.Remove(string key)
151+
=> Remove(table.IndexOfName(key));
152+
153+
internal bool Remove(int index)
149154
{
150-
int index = table.IndexOfName(key);
151155
if (index < 0 || index >= values.Length || values[index] is DeadValue) return false;
152156
values[index] = DeadValue.Default;
153157
return true;
@@ -177,6 +181,10 @@ private object SetValue(string key, object value, bool isAdd)
177181
// then semantically, this value already exists
178182
throw new ArgumentException("An item with the same key has already been added", nameof(key));
179183
}
184+
return SetValue(index, value);
185+
}
186+
internal object SetValue(int index, object value)
187+
{
180188
int oldLength = values.Length;
181189
if (oldLength <= index)
182190
{

UIBindingTest/BindingForm.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ public BindingForm()
1010
{
1111
InitializeComponent();
1212

13+
SuspendLayout();
1314
using (var conn = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=SSPI"))
1415
{
1516
mainGrid.DataSource = conn.Query("select * from sys.objects").AsList();
1617
}
18+
ResumeLayout();
1719
}
1820
}
1921
}

0 commit comments

Comments
 (0)