Skip to content

Commit

Permalink
重构DefaultDataCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Jan 25, 2021
1 parent 63a275e commit 1080057
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
6 changes: 3 additions & 3 deletions WebApiClientCore/Implementations/DefaultApiActionInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public virtual async Task<TResult> InvokeAsync(HttpClientContext context, object
var requiredUri = context.HttpApiOptions.HttpHost ?? context.HttpClient.BaseAddress;
var useDefaultUserAgent = context.HttpApiOptions.UseDefaultUserAgent;
using var requestMessage = new HttpApiRequestMessageImpl(requiredUri, useDefaultUserAgent);
var httpContext = new HttpContext(context, requestMessage);
var dataCollection = new DefaultDataCollection();
var requestContext = new ApiRequestContext(httpContext, this.ActionDescriptor, arguments, dataCollection);

var httpContext = new HttpContext(context, requestMessage);
var requestContext = new ApiRequestContext(httpContext, this.ActionDescriptor, arguments, new DefaultDataCollection());
return await this.InvokeAsync(requestContext).ConfigureAwait(false);
}
catch (HttpRequestException)
Expand Down
32 changes: 18 additions & 14 deletions WebApiClientCore/Implementations/DefaultDataCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace WebApiClientCore.Implementations
{
/// <summary>
/// 表示延时初始化的数据集合
/// 表示数据集合
/// </summary>
[DebuggerDisplay("Count = {Count}")]
[DebuggerTypeProxy(typeof(DebugView))]
Expand All @@ -16,12 +16,12 @@ public class DefaultDataCollection : IDataCollection
/// <summary>
/// 数据字典
/// </summary>
private readonly Lazy<Dictionary<object, object?>> dictionary = new Lazy<Dictionary<object, object?>>(() => new Dictionary<object, object?>());
private Dictionary<object, object?>? dictionary;

/// <summary>
/// 获取集合元素的数量
/// </summary>
public int Count => this.dictionary.IsValueCreated ? this.dictionary.Value.Count : 0;
public int Count => this.dictionary == null ? 0 : this.dictionary.Count;

/// <summary>
/// 设置值
Expand All @@ -30,7 +30,11 @@ public class DefaultDataCollection : IDataCollection
/// <param name="value">值</param>
public void Set(object key, object? value)
{
this.dictionary.Value[key] = value;
if (this.dictionary == null)
{
this.dictionary = new Dictionary<object, object?>();
}
this.dictionary[key] = value;
}

/// <summary>
Expand All @@ -40,7 +44,7 @@ public void Set(object key, object? value)
/// <returns></returns>
public bool ContainsKey(object key)
{
return this.dictionary.IsValueCreated && this.dictionary.Value.ContainsKey(key);
return this.dictionary != null && this.dictionary.ContainsKey(key);
}

/// <summary>
Expand Down Expand Up @@ -86,14 +90,14 @@ public bool TryGetValue<TValue>(object key, [MaybeNullWhen(false)] out TValue va
/// <returns></returns>
public bool TryGetValue(object key, out object? value)
{
if (this.dictionary.IsValueCreated == false)
if (this.dictionary == null)
{
value = default;
return false;
}
else
{
return this.dictionary.Value.TryGetValue(key, out value);
return this.dictionary.TryGetValue(key, out value);
}
}

Expand All @@ -105,14 +109,14 @@ public bool TryGetValue(object key, out object? value)
/// <returns></returns>
public bool TryRemove(object key, out object? value)
{
if (this.dictionary.IsValueCreated == false)
if (this.dictionary == null)
{
value = default;
return false;
}
else
{
return this.dictionary.Value.Remove(key, out value);
return this.dictionary.Remove(key, out value);
}
}

Expand All @@ -124,7 +128,7 @@ private class DebugView
/// <summary>
/// 查看的对象
/// </summary>
private readonly DefaultDataCollection target;
private readonly Dictionary<object, object?>? dictionary;

/// <summary>
/// 查看的内容
Expand All @@ -134,9 +138,9 @@ private class DebugView
{
get
{
return this.target.dictionary.IsValueCreated
? this.target.dictionary.Value.ToArray()
: Array.Empty<KeyValuePair<object, object?>>();
return this.dictionary == null
? Array.Empty<KeyValuePair<object, object?>>()
: this.dictionary.ToArray();
}
}

Expand All @@ -146,7 +150,7 @@ private class DebugView
/// <param name="target">查看的对象</param>
public DebugView(DefaultDataCollection target)
{
this.target = target;
this.dictionary = target.dictionary;
}
}
}
Expand Down

0 comments on commit 1080057

Please sign in to comment.