Skip to content

Commit caa111f

Browse files
committed
HttpApiFactory支持同一类型接口使用name区分
1 parent 6390e71 commit caa111f

File tree

1 file changed

+55
-9
lines changed

1 file changed

+55
-9
lines changed

WebApiClient/HttpApiFactory.Static.cs

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ public static class HttpApiFactory
1818
/// <summary>
1919
/// 工厂字典
2020
/// </summary>
21-
private static readonly ConcurrentDictionary<Type, IHttpApiFactory> factories;
21+
private static readonly ConcurrentDictionary<string, IHttpApiFactory> factories;
2222

2323
/// <summary>
2424
/// 表示HttpApi创建工厂
2525
/// </summary>
2626
static HttpApiFactory()
2727
{
28-
factories = new ConcurrentDictionary<Type, IHttpApiFactory>();
28+
factories = new ConcurrentDictionary<string, IHttpApiFactory>();
2929
}
3030

3131
/// <summary>
@@ -36,16 +36,34 @@ static HttpApiFactory()
3636
/// <returns></returns>
3737
public static HttpApiFactory<TInterface> Add<TInterface>() where TInterface : class, IHttpApi
3838
{
39+
var name = GetFactoryName<TInterface>();
40+
return Add<TInterface>(name);
41+
}
42+
43+
/// <summary>
44+
/// 创建并返回指定接口的HttpApiFactory
45+
/// </summary>
46+
/// <typeparam name="TInterface"></typeparam>
47+
/// <param name="name">工厂名称</param>
48+
/// <exception cref="ArgumentNullException"></exception>
49+
/// <exception cref="InvalidOperationException"></exception>
50+
/// <returns></returns>
51+
public static HttpApiFactory<TInterface> Add<TInterface>(string name) where TInterface : class, IHttpApi
52+
{
53+
if (string.IsNullOrEmpty(name) == true)
54+
{
55+
throw new ArgumentNullException(nameof(name));
56+
}
57+
3958
lock (syncRoot)
4059
{
41-
var apiType = typeof(TInterface);
42-
if (factories.ContainsKey(apiType) == true)
60+
if (factories.ContainsKey(name) == true)
4361
{
44-
throw new InvalidOperationException($"不允许重复创建指定接口的HttpApiFactory:{apiType}");
62+
throw new InvalidOperationException($"不允许创建重复名称的HttpApiFactory:{name}");
4563
}
4664

4765
var factory = new HttpApiFactory<TInterface>();
48-
factories.TryAdd(apiType, factory);
66+
factories.TryAdd(name, factory);
4967
return factory;
5068
}
5169
}
@@ -58,12 +76,40 @@ public static HttpApiFactory<TInterface> Add<TInterface>() where TInterface : cl
5876
/// <returns></returns>
5977
public static TInterface Create<TInterface>() where TInterface : class, IHttpApi
6078
{
61-
var apiType = typeof(TInterface);
62-
if (factories.TryGetValue(apiType, out var factory) == true)
79+
var name = GetFactoryName<TInterface>();
80+
return Create<TInterface>(name);
81+
}
82+
83+
/// <summary>
84+
/// 创建指定接口的代理实例
85+
/// </summary>
86+
/// <typeparam name="TInterface"></typeparam>
87+
/// <param name="name">工厂名称</param>
88+
/// <exception cref="ArgumentNullException"></exception>
89+
/// <exception cref="InvalidOperationException"></exception>
90+
/// <returns></returns>
91+
public static TInterface Create<TInterface>(string name) where TInterface : class, IHttpApi
92+
{
93+
if (string.IsNullOrEmpty(name) == true)
94+
{
95+
throw new ArgumentNullException(nameof(name));
96+
}
97+
98+
if (factories.TryGetValue(name, out var factory) == true)
6399
{
64100
return factory.CreateHttpApi() as TInterface;
65101
}
66-
throw new InvalidOperationException($"请先调用HttpApiFactory.Add()方法配置指定接口:{apiType}");
102+
throw new InvalidOperationException($"请先调用HttpApiFactory.Add()方法配置指定接口:{typeof(TInterface)}");
103+
}
104+
105+
/// <summary>
106+
/// 返回类型的工厂名称
107+
/// </summary>
108+
/// <typeparam name="TInterface"></typeparam>
109+
/// <returns></returns>
110+
private static string GetFactoryName<TInterface>()
111+
{
112+
return typeof(TInterface).FullName;
67113
}
68114
}
69115
}

0 commit comments

Comments
 (0)