Skip to content

Commit 011ff1b

Browse files
committed
删除IHttpApiFactory接口
1 parent 647546f commit 011ff1b

File tree

5 files changed

+72
-103
lines changed

5 files changed

+72
-103
lines changed

Demo/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static void Init()
2626
HttpServer.Start(9999);
2727

2828
// 注册与配置IUserApi接口
29-
HttpApiFactory.Add<IUserApi>(c =>
29+
HttpApiFactory.Add<IUserApi>().ConfigureHttpApiConfig(c =>
3030
{
3131
c.HttpHost = new Uri("http://localhost:9999/");
3232
c.LoggerFactory = new LoggerFactory().AddConsole();

WebApiClient/HttpApiFactory.Static.cs

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Concurrent;
3-
using System.Net.Http;
43

54
namespace WebApiClient
65
{
@@ -19,56 +18,35 @@ public static class HttpApiFactory
1918
/// <summary>
2019
/// 工厂字典
2120
/// </summary>
22-
private static readonly ConcurrentDictionary<Type, IHttpApiFactory> factories;
21+
private static readonly ConcurrentDictionary<Type, _IHttpApiFactory> factories;
2322

2423
/// <summary>
2524
/// 表示HttpApi创建工厂
2625
/// </summary>
2726
static HttpApiFactory()
2827
{
29-
factories = new ConcurrentDictionary<Type, IHttpApiFactory>();
28+
factories = new ConcurrentDictionary<Type, _IHttpApiFactory>();
3029
}
3130

3231
/// <summary>
33-
/// 注册http接口
32+
/// 创建并记录指定接口的HttpApiFactory
3433
/// </summary>
3534
/// <typeparam name="TInterface"></typeparam>
35+
/// <exception cref="InvalidOperationException"></exception>
3636
/// <returns></returns>
37-
public static bool Add<TInterface>() where TInterface : class, IHttpApi
38-
{
39-
return Add<TInterface>(configAction: null);
40-
}
41-
42-
/// <summary>
43-
/// 注册http接口
44-
/// </summary>
45-
/// <typeparam name="TInterface"></typeparam>
46-
/// <param name="configAction">HttpApiConfig的配置委托</param>
47-
/// <returns></returns>
48-
public static bool Add<TInterface>(Action<HttpApiConfig> configAction) where TInterface : class, IHttpApi
49-
{
50-
return Add<TInterface>(configAction, handlerFunc: null);
51-
}
52-
53-
/// <summary>
54-
/// 注册http接口
55-
/// </summary>
56-
/// <typeparam name="TInterface"></typeparam>
57-
/// <param name="configAction">HttpApiConfig的配置</param>
58-
/// <param name="handlerFunc">HttpMessageHandler创建委托</param>
59-
/// <returns></returns>
60-
public static bool Add<TInterface>(Action<HttpApiConfig> configAction, Func<HttpMessageHandler> handlerFunc) where TInterface : class, IHttpApi
37+
public static HttpApiFactory<TInterface> Add<TInterface>() where TInterface : class, IHttpApi
6138
{
6239
lock (syncRoot)
6340
{
6441
var apiType = typeof(TInterface);
6542
if (factories.ContainsKey(apiType) == true)
6643
{
67-
return false;
44+
throw new InvalidOperationException($"不允许重复注册接口:{apiType}");
6845
}
6946

70-
var factory = new HttpApiFactory<TInterface>(configAction, handlerFunc);
71-
return factories.TryAdd(apiType, factory);
47+
var factory = new HttpApiFactory<TInterface>();
48+
factories.TryAdd(apiType, factory);
49+
return factory;
7250
}
7351
}
7452

WebApiClient/HttpApiFactory.cs

Lines changed: 52 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ namespace WebApiClient
1111
/// 提供HttpApi的配置注册和实例创建
1212
/// 并对实例的生命周期进行自动管理
1313
/// </summary>
14-
public class HttpApiFactory<TInterface> : IHttpApiFactory<TInterface>, IHttpApiFactory, _IHttpApiFactory
14+
public class HttpApiFactory<TInterface> : IHttpApiFactory<TInterface>, _IHttpApiFactory
1515
where TInterface : class, IHttpApi
1616
{
1717
/// <summary>
1818
/// HttpApiConfig的配置委托
1919
/// </summary>
20-
private readonly Action<HttpApiConfig> configAction;
20+
private Action<HttpApiConfig> configAction;
2121

2222
/// <summary>
2323
/// HttpMessageHandler的创建委托
2424
/// </summary>
25-
private readonly Func<HttpMessageHandler> handlerFunc;
25+
private Func<HttpMessageHandler> handlerFunc;
2626

2727
/// <summary>
2828
/// handler的生命周期
@@ -46,84 +46,70 @@ public class HttpApiFactory<TInterface> : IHttpApiFactory<TInterface>, IHttpApiF
4646

4747

4848
/// <summary>
49-
/// 获取已过期但还未释放的HttpApi实例数量
49+
/// HttpApi创建工厂
5050
/// </summary>
51-
public int ExpiredCount
51+
public HttpApiFactory()
5252
{
53-
get => this.expiredEntries.Count;
53+
this.expiredEntries = new ConcurrentQueue<ExpiredEntry>();
54+
55+
this.activeEntryLazy = new Lazy<ActiveEntry>(
56+
this.CreateActiveEntry,
57+
LazyThreadSafetyMode.ExecutionAndPublication);
58+
59+
this.RegisteCleanup();
5460
}
5561

62+
5663
/// <summary>
57-
/// 获取或设置HttpApi实例的生命周期
64+
/// 置HttpApi实例的生命周期
5865
/// </summary>
66+
/// <param name="lifeTime">生命周期</param>
5967
/// <exception cref="ArgumentOutOfRangeException"></exception>
60-
public TimeSpan Lifetime
68+
public HttpApiFactory<TInterface> SetLifetime(TimeSpan lifeTime)
6169
{
62-
get
63-
{
64-
return this.lifeTime;
65-
}
66-
set
70+
if (lifeTime <= TimeSpan.Zero)
6771
{
68-
if (value <= TimeSpan.Zero)
69-
{
70-
throw new ArgumentOutOfRangeException();
71-
}
72-
this.lifeTime = value;
72+
throw new ArgumentOutOfRangeException();
7373
}
74+
this.lifeTime = lifeTime;
75+
return this;
7476
}
7577

7678
/// <summary>
7779
/// 获取或设置清理过期的HttpApi实例的时间间隔
7880
/// </summary>
81+
/// <param name="interval">时间间隔</param>
7982
/// <exception cref="ArgumentOutOfRangeException"></exception>
80-
public TimeSpan CleanupInterval
83+
public HttpApiFactory<TInterface> SetCleanupInterval(TimeSpan interval)
8184
{
82-
get
85+
if (interval <= TimeSpan.Zero)
8386
{
84-
return this.cleanupInterval;
85-
}
86-
set
87-
{
88-
if (value <= TimeSpan.Zero)
89-
{
90-
throw new ArgumentOutOfRangeException();
91-
}
92-
this.cleanupInterval = value;
87+
throw new ArgumentOutOfRangeException();
9388
}
89+
this.cleanupInterval = interval;
90+
return this;
9491
}
9592

9693
/// <summary>
97-
/// HttpApi创建工厂
94+
/// 配置HttpMessageHandler的创建
9895
/// </summary>
99-
public HttpApiFactory()
100-
: this(configAction: null)
101-
{
102-
}
103-
104-
/// <summary>
105-
/// HttpApi创建工厂
106-
/// </summary>
107-
/// <param name="configAction">HttpApiConfig的配置委托</param>
108-
public HttpApiFactory(Action<HttpApiConfig> configAction)
109-
: this(configAction, handlerFunc: null)
96+
/// <param name="handlerFunc">创建委托</param>
97+
/// <returns></returns>
98+
public HttpApiFactory<TInterface> ConfigureHttpMessageHandler(Func<HttpMessageHandler> handlerFunc)
11099
{
100+
this.handlerFunc = handlerFunc;
101+
return this;
111102
}
112103

113104
/// <summary>
114-
/// HttpApi创建工厂
105+
/// 配置HttpApiConfig
115106
/// </summary>
116-
/// <param name="configAction">HttpApiConfig的配置委托</param>
117-
/// <param name="handlerFunc">HttpMessageHandler的创建委托</param>
118-
public HttpApiFactory(Action<HttpApiConfig> configAction, Func<HttpMessageHandler> handlerFunc)
107+
/// <param name="configAction">配置委托</param>
108+
/// <returns></returns>
109+
public HttpApiFactory<TInterface> ConfigureHttpApiConfig(Action<HttpApiConfig> configAction)
119110
{
120111
this.configAction = configAction;
121-
this.handlerFunc = handlerFunc ?? new Func<HttpMessageHandler>(() => new DefaultHttpClientHandler());
122-
123-
this.expiredEntries = new ConcurrentQueue<ExpiredEntry>();
124-
this.activeEntryLazy = new Lazy<ActiveEntry>(this.CreateActiveEntry, LazyThreadSafetyMode.ExecutionAndPublication);
125-
126-
this.RegisteCleanup();
112+
return this;
127113
}
128114

129115
/// <summary>
@@ -132,14 +118,14 @@ public HttpApiFactory(Action<HttpApiConfig> configAction, Func<HttpMessageHandle
132118
/// <returns></returns>
133119
public TInterface CreateHttpApi()
134120
{
135-
return ((IHttpApiFactory)this).CreateHttpApi() as TInterface;
121+
return ((_IHttpApiFactory)this).CreateHttpApi() as TInterface;
136122
}
137123

138124
/// <summary>
139125
/// 创建接口的代理实例
140126
/// </summary>
141127
/// <returns></returns>
142-
object IHttpApiFactory.CreateHttpApi()
128+
object _IHttpApiFactory.CreateHttpApi()
143129
{
144130
var interceptor = this.activeEntryLazy.Value.Interceptor;
145131
return HttpApiClient.Create(typeof(TInterface), interceptor);
@@ -151,7 +137,8 @@ object IHttpApiFactory.CreateHttpApi()
151137
/// <returns></returns>
152138
private ActiveEntry CreateActiveEntry()
153139
{
154-
var httpApiConfig = new HttpApiConfig(this.handlerFunc.Invoke(), true);
140+
var handler = this.handlerFunc?.Invoke() ?? new DefaultHttpClientHandler();
141+
var httpApiConfig = new HttpApiConfig(handler, true);
155142
var interceptor = new LifeTimeTrackingInterceptor(httpApiConfig);
156143

157144
if (this.configAction != null)
@@ -173,12 +160,22 @@ private ActiveEntry CreateActiveEntry()
173160
void _IHttpApiFactory.OnEntryDeactivate(ActiveEntry active)
174161
{
175162
// 切换激活状态的记录的实例
176-
this.activeEntryLazy = new Lazy<ActiveEntry>(this.CreateActiveEntry, LazyThreadSafetyMode.ExecutionAndPublication);
163+
this.activeEntryLazy = new Lazy<ActiveEntry>(
164+
this.CreateActiveEntry,
165+
LazyThreadSafetyMode.ExecutionAndPublication);
177166

178167
var expired = new ExpiredEntry(active);
179168
this.expiredEntries.Enqueue(expired);
180169
}
181170

171+
/// <summary>
172+
/// 获取生命周期
173+
/// </summary>
174+
TimeSpan _IHttpApiFactory.Lifetime
175+
{
176+
get => this.lifeTime;
177+
}
178+
182179
/// <summary>
183180
/// 注册清理任务
184181
/// </summary>

WebApiClient/IHttpApiFactory.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,15 @@
11
namespace WebApiClient
22
{
3-
/// <summary>
4-
/// 定义HttpApi工厂的接口
5-
/// </summary>
6-
public interface IHttpApiFactory
7-
{
8-
/// <summary>
9-
/// 创建接口的代理实例
10-
/// </summary>
11-
/// <returns></returns>
12-
object CreateHttpApi();
13-
}
14-
153
/// <summary>
164
/// 定义HttpApi工厂的接口
175
/// </summary>
186
/// <typeparam name="TInterface"></typeparam>
19-
public interface IHttpApiFactory<TInterface> : IHttpApiFactory where TInterface : class, IHttpApi
7+
public interface IHttpApiFactory<TInterface> where TInterface : class, IHttpApi
208
{
219
/// <summary>
2210
/// 创建接口的代理实例
2311
/// </summary>
2412
/// <returns></returns>
25-
new TInterface CreateHttpApi();
13+
TInterface CreateHttpApi();
2614
}
2715
}

WebApiClient/Internal/HttpApiFactories/IHttpApiFactory.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@ namespace WebApiClient
88
interface _IHttpApiFactory
99
{
1010
/// <summary>
11-
/// 获取或设置生命周期
11+
/// 获取生命周期
1212
/// </summary>
13-
TimeSpan Lifetime { get; set; }
13+
TimeSpan Lifetime { get; }
14+
15+
/// <summary>
16+
/// 创建接口的代理实例
17+
/// </summary>
18+
/// <returns></returns>
19+
object CreateHttpApi();
1420

1521
/// <summary>
1622
/// 当有记录失效时

0 commit comments

Comments
 (0)