Skip to content

Commit 4ba65bb

Browse files
authored
Merge pull request #8 from xin9le/refactor/misc
Refactoring
2 parents 9e57635 + 6090995 commit 4ba65bb

File tree

11 files changed

+168
-493
lines changed

11 files changed

+168
-493
lines changed

src/libs/QLimitive/BindParameterCollection.cs

Lines changed: 75 additions & 201 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
using System;
2-
using System.Collections;
1+
using System.Collections;
32
using System.Collections.Generic;
4-
using System.Linq.Expressions;
3+
using System.Diagnostics.CodeAnalysis;
54
using FastMember;
6-
using QLimitive.Internals;
75

86
namespace QLimitive;
97

@@ -14,11 +12,8 @@ namespace QLimitive;
1412
/// </summary>
1513
public sealed class BindParameterCollection : IDictionary<string, object?>, IReadOnlyDictionary<string, object?>
1614
{
17-
#region Properties
18-
/// <summary>
19-
/// Gets the key/value store that held inside.
20-
/// </summary>
21-
private IDictionary<string, object?> Inner { get; }
15+
#region Fields
16+
private readonly Dictionary<string, object?> _inner;
2217
#endregion
2318

2419

@@ -27,263 +22,142 @@ public sealed class BindParameterCollection : IDictionary<string, object?>, IRea
2722
/// Creates instance.
2823
/// </summary>
2924
public BindParameterCollection()
30-
: this(new Dictionary<string, object?>())
25+
: this(capacity: 0)
3126
{ }
3227

3328

3429
/// <summary>
3530
/// Creates instance.
3631
/// </summary>
37-
/// <param name="source"></param>
38-
public BindParameterCollection(IDictionary<string, object?> source)
39-
=> this.Inner = source;
32+
/// <param name="capacity"></param>
33+
public BindParameterCollection(int capacity)
34+
=> this._inner = new(capacity);
4035
#endregion
4136

4237

43-
#region IDictionary<TKey, TValue> implementations
44-
/// <summary>
45-
/// Gets or sets the element with the specified key.
46-
/// </summary>
47-
/// <param name="key"></param>
48-
/// <returns></returns>
38+
#region IDictionary<TKey, TValue>
39+
/// <inheritdoc/>
4940
public object? this[string key]
5041
{
51-
get => this.Inner[key];
52-
set => this.Inner[key] = value;
42+
get => this._inner[key];
43+
set => this._inner[key] = value;
5344
}
5445

5546

56-
/// <summary>
57-
/// Gets an <see cref="ICollection{T}"/> containing the keys of the <see cref="IDictionary{TKey, TValue}"/>.
58-
/// </summary>
59-
ICollection<string> IDictionary<string, object?>.Keys
60-
=> this.Inner.Keys;
47+
/// <inheritdoc/>
48+
public ICollection<string> Keys
49+
=> this._inner.Keys;
6150

6251

63-
/// <summary>
64-
/// Gets an <see cref="ICollection{T}"/> containing the values of the <see cref="IDictionary{TKey, TValue}"/>.
65-
/// </summary>
66-
ICollection<object?> IDictionary<string, object?>.Values
67-
=> this.Inner.Values;
52+
/// <inheritdoc/>
53+
public ICollection<object?> Values
54+
=> this._inner.Values;
6855

6956

70-
/// <summary>
71-
/// Gets the number of elements contained in the <see cref="ICollection{T}"/>.
72-
/// </summary>
57+
/// <inheritdoc/>
7358
public int Count
74-
=> this.Inner.Count;
59+
=> this._inner.Count;
7560

7661

77-
/// <summary>
78-
/// Gets a value indicating whether the <see cref="ICollection{T}"/> is read-only.
79-
/// </summary>
80-
bool ICollection<KeyValuePair<string, object?>>.IsReadOnly
81-
=> this.Inner.IsReadOnly;
62+
/// <inheritdoc/>
63+
public bool IsReadOnly
64+
=> false;
8265

8366

84-
/// <summary>
85-
/// Adds an item to the <see cref="ICollection{T}"/>.
86-
/// </summary>
87-
/// <param name="key"></param>
88-
/// <param name="value"></param>
67+
/// <inheritdoc/>
8968
public void Add(string key, object? value)
90-
=> this.Inner.Add(key, value);
69+
=> this._inner.Add(key, value);
9170

9271

93-
/// <summary>
94-
/// Adds an item to the <see cref="ICollection{T}"/>.
95-
/// </summary>
96-
/// <param name="item"></param>
97-
void ICollection<KeyValuePair<string, object?>>.Add(KeyValuePair<string, object?> item)
98-
=> this.Inner.Add(item);
72+
/// <inheritdoc/>
73+
public void Add(KeyValuePair<string, object?> item)
74+
{
75+
var dic = (IDictionary<string, object?>)this._inner;
76+
dic.Add(item);
77+
}
9978

10079

101-
/// <summary>
102-
/// Removes all items from the <see cref="ICollection{T}"/>.
103-
/// </summary>
80+
/// <inheritdoc/>
10481
public void Clear()
105-
=> this.Inner.Clear();
82+
=> this._inner.Clear();
10683

10784

108-
/// <summary>
109-
/// Determines whether the <see cref="ICollection{T}"/> contains a specific value.
110-
/// </summary>
111-
/// <param name="item"></param>
112-
/// <returns></returns>
113-
bool ICollection<KeyValuePair<string, object?>>.Contains(KeyValuePair<string, object?> item)
114-
=> this.Inner.Contains(item);
85+
/// <inheritdoc/>
86+
public bool Contains(KeyValuePair<string, object?> item)
87+
{
88+
var dic = (IDictionary<string, object?>)this._inner;
89+
return dic.Contains(item);
90+
}
11591

11692

117-
/// <summary>
118-
/// Determines whether the <see cref="IDictionary{TKey, TValue}"/> contains an element with the specified key.
119-
/// </summary>
120-
/// <param name="key"></param>
121-
/// <returns></returns>
93+
/// <inheritdoc/>
12294
public bool ContainsKey(string key)
123-
=> this.Inner.ContainsKey(key);
95+
=> this._inner.ContainsKey(key);
12496

12597

126-
/// <summary>
127-
/// Copies the elements of the <see cref="ICollection{T}"/> to an <see cref="Array"/>, starting at a particular <seealso cref="Array"/> index.
128-
/// </summary>
129-
/// <param name="array"></param>
130-
/// <param name="arrayIndex"></param>
131-
void ICollection<KeyValuePair<string, object?>>.CopyTo(KeyValuePair<string, object?>[] array, int arrayIndex)
132-
=> this.Inner.CopyTo(array, arrayIndex);
98+
/// <inheritdoc/>
99+
public void CopyTo(KeyValuePair<string, object?>[] array, int arrayIndex)
100+
{
101+
var dic = (IDictionary<string, object?>)this._inner;
102+
dic.CopyTo(array, arrayIndex);
103+
}
133104

134105

135-
/// <summary>
136-
/// Returns an enumerator that iterates through the collection.
137-
/// </summary>
138-
/// <returns></returns>
106+
/// <inheritdoc/>
139107
public IEnumerator<KeyValuePair<string, object?>> GetEnumerator()
140-
=> this.Inner.GetEnumerator();
108+
=> this._inner.GetEnumerator();
141109

142110

143-
/// <summary>
144-
/// Returns an enumerator that iterates through the collection.
145-
/// </summary>
146-
/// <returns></returns>
111+
/// <inheritdoc/>
147112
IEnumerator IEnumerable.GetEnumerator()
148-
=> this.Inner.GetEnumerator();
113+
=> this._inner.GetEnumerator();
149114

150115

151-
/// <summary>
152-
/// Removes the first occurrence of a specific object from the <see cref="ICollection{T}"/>.
153-
/// </summary>
154-
/// <param name="key"></param>
155-
/// <returns></returns>
116+
/// <inheritdoc/>
156117
public bool Remove(string key)
157-
=> this.Inner.Remove(key);
118+
=> this._inner.Remove(key);
158119

159120

160-
/// <summary>
161-
/// Removes the first occurrence of a specific object from the <see cref="ICollection{T}"/>.
162-
/// </summary>
163-
/// <param name="item"></param>
164-
/// <returns></returns>
165-
bool ICollection<KeyValuePair<string, object?>>.Remove(KeyValuePair<string, object?> item)
166-
=> this.Inner.Remove(item);
121+
/// <inheritdoc/>
122+
public bool Remove(KeyValuePair<string, object?> item)
123+
{
124+
var dic = (IDictionary<string, object?>)this._inner;
125+
return dic.Remove(item);
126+
}
167127

168128

169-
/// <summary>
170-
/// Gets the value associated with the specified key.
171-
/// </summary>
172-
/// <param name="key"></param>
173-
/// <param name="value"></param>
174-
/// <returns></returns>
175-
public bool TryGetValue(string key, out object? value)
176-
=> this.Inner.TryGetValue(key, out value);
129+
/// <inheritdoc/>
130+
public bool TryGetValue(string key, [MaybeNullWhen(false)] out object? value)
131+
=> this._inner.TryGetValue(key, out value);
177132
#endregion
178133

179134

180-
#region IReadOnlyDictionary<TKey, TValue> implementations
181-
/// <summary>
182-
/// Gets an enumerable collection that contains the keys in the read-only dictionary.
183-
/// </summary>
184-
public IEnumerable<string> Keys
185-
=> this.Inner.Keys;
135+
#region IReadOnlyDictionary<TKey, TValue>
136+
/// <inheritdoc/>
137+
IEnumerable<string> IReadOnlyDictionary<string, object?>.Keys
138+
=> this.Keys;
186139

187140

188-
/// <summary>
189-
/// Gets an enumerable collection that contains the values in the read-only dictionary.
190-
/// </summary>
191-
public IEnumerable<object?> Values
192-
=> this.Inner.Values;
141+
/// <inheritdoc/>
142+
IEnumerable<object?> IReadOnlyDictionary<string, object?>.Values
143+
=> this.Values;
193144
#endregion
194145

195146

196-
#region Create
197-
/// <summary>
198-
/// Creates an instance from the specified object.
199-
/// </summary>
200-
/// <typeparam name="T"></typeparam>
201-
/// <param name="obj"></param>
202-
/// <returns></returns>
203-
public static BindParameterCollection From<T>(T obj)
204-
{
205-
var result = new BindParameterCollection();
206-
var members = TypeAccessor.Create(typeof(T)).GetMembers();
207-
var accessor = ObjectAccessor.Create(obj);
208-
for (var i = 0; i < members.Count; i++)
209-
{
210-
var member = members[i];
211-
if (member.CanRead)
212-
result.Add(member.Name, accessor[member.Name]);
213-
}
214-
return result;
215-
}
216-
217-
147+
#region Utilities
218148
/// <summary>
219149
/// Clones the instance.
220150
/// </summary>
221151
/// <returns></returns>
222152
public BindParameterCollection Clone()
223153
{
224-
IDictionary<string, object?> result = new BindParameterCollection();
154+
var result = new BindParameterCollection(this._inner.Count);
225155
foreach (var x in this)
226-
result.Add(x);
227-
return (BindParameterCollection)result;
228-
}
229-
#endregion
230-
231-
232-
#region Append
233-
/// <summary>
234-
/// Appends the specified values.
235-
/// </summary>
236-
/// <param name="kvs"></param>
237-
public void Append(IEnumerable<KeyValuePair<string, object?>> kvs)
238-
{
239-
foreach (var x in kvs)
240-
this.Add(x.Key, x.Value);
241-
}
242-
243-
244-
/// <summary>
245-
/// Appends the specified values.
246-
/// </summary>
247-
/// <param name="obj"></param>
248-
public void Append<T>(T obj)
249-
{
250-
var members = TypeAccessor.Create(typeof(T)).GetMembers();
251-
var accessor = ObjectAccessor.Create(obj);
252-
for (var i = 0; i < members.Count; i++)
253-
{
254-
var member = members[i];
255-
if (member.CanRead)
256-
this.Add(member.Name, accessor[member.Name]);
257-
}
258-
}
259-
260-
261-
/// <summary>
262-
/// Appends the specified values.
263-
/// </summary>
264-
/// <param name="obj"></param>
265-
/// <param name="targetProperties"></param>
266-
public void Append<T>(T obj, Expression<Func<T, object>> targetProperties)
267-
{
268-
var memberNames = ExpressionHelper.GetMemberNames(targetProperties);
269-
var members = TypeAccessor.Create(typeof(T)).GetMembers();
270-
var accessor = ObjectAccessor.Create(obj);
271-
for (var i = 0; i < members.Count; i++)
272-
{
273-
var member = members[i];
274-
if (!member.CanRead)
275-
continue;
276-
277-
if (!memberNames.Contains(member.Name))
278-
continue;
279-
280-
this.Add(member.Name, accessor[member.Name]);
281-
}
156+
result._inner.Add(x.Key, x.Value);
157+
return result;
282158
}
283-
#endregion
284159

285160

286-
#region Overwrite
287161
/// <summary>
288162
/// Overwrites by the specified values.
289163
/// </summary>
@@ -299,10 +173,10 @@ public void Overwrite<T>(T obj)
299173
if (!member.CanRead)
300174
continue;
301175

302-
if (!this.Inner.ContainsKey(member.Name))
176+
if (!this._inner.ContainsKey(member.Name))
303177
continue;
304178

305-
this.Inner[member.Name] = accessor[member.Name];
179+
this._inner[member.Name] = accessor[member.Name];
306180
}
307181
}
308182
#endregion

0 commit comments

Comments
 (0)