Skip to content

Commit f745b8a

Browse files
committed
增加RegisterFactory方法
1 parent 4906531 commit f745b8a

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

WebApiClient/HttpApi.Register.cs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public partial class HttpApi
2323
/// <returns></returns>
2424
public static HttpApiFactory<TInterface> Register<TInterface>() where TInterface : class, IHttpApi
2525
{
26-
var name = GetFactoryName<TInterface>();
27-
return Register<TInterface>(name);
26+
var factory = new HttpApiFactory<TInterface>();
27+
return RegisterFactory(factory);
2828
}
2929

3030
/// <summary>
@@ -39,7 +39,7 @@ public static HttpApiFactory<TInterface> Register<TInterface>() where TInterface
3939
public static HttpApiFactory<TInterface> Register<TInterface>(string name) where TInterface : class, IHttpApi
4040
{
4141
var factory = new HttpApiFactory<TInterface>();
42-
return Register(name, factory);
42+
return RegisterFactory(factory, name);
4343
}
4444

4545
/// <summary>
@@ -55,35 +55,48 @@ public static HttpApiFactory<TInterface> Register<TInterface>(string name) where
5555
public static HttpApiFactory Register(string name, Type interfaceType)
5656
{
5757
var factory = new HttpApiFactory(interfaceType);
58-
return Register(name, factory);
58+
return RegisterFactory(factory, name);
5959
}
6060

6161
/// <summary>
6262
/// 注册指定Api工厂
6363
/// </summary>
64-
/// <typeparam name="THttpApiFactory"></typeparam>
65-
/// <param name="name">工厂名称</param>
64+
/// <typeparam name="THttpApiFactory"></typeparam>
6665
/// <param name="httpApiFactory">工厂实例</param>
6766
/// <exception cref="ArgumentNullException"></exception>
6867
/// <exception cref="InvalidOperationException"></exception>
6968
/// <returns></returns>
70-
public static THttpApiFactory Register<THttpApiFactory>(string name, THttpApiFactory httpApiFactory) where THttpApiFactory : IHttpApiFactory
69+
public static THttpApiFactory RegisterFactory<THttpApiFactory>(THttpApiFactory httpApiFactory) where THttpApiFactory : IHttpApiFactory
7170
{
72-
if (string.IsNullOrEmpty(name) == true)
73-
{
74-
throw new ArgumentNullException(nameof(name));
75-
}
71+
var name = GetFactoryName(httpApiFactory.InterfaceType);
72+
return RegisterFactory(httpApiFactory, name);
73+
}
7674

75+
/// <summary>
76+
/// 注册指定Api工厂
77+
/// </summary>
78+
/// <typeparam name="THttpApiFactory"></typeparam>
79+
/// <param name="httpApiFactory">工厂实例</param>
80+
/// <param name="name">工厂名称</param>
81+
/// <exception cref="ArgumentNullException"></exception>
82+
/// <exception cref="InvalidOperationException"></exception>
83+
/// <returns></returns>
84+
public static THttpApiFactory RegisterFactory<THttpApiFactory>(THttpApiFactory httpApiFactory, string name) where THttpApiFactory : IHttpApiFactory
85+
{
7786
if (httpApiFactory == null)
7887
{
7988
throw new ArgumentNullException(nameof(httpApiFactory));
8089
}
90+
if (string.IsNullOrEmpty(name) == true)
91+
{
92+
throw new ArgumentNullException(nameof(name));
93+
}
8194

8295
if (factories.TryAdd(name, httpApiFactory) == true)
8396
{
8497
return httpApiFactory;
8598
}
86-
throw new InvalidOperationException($"不允许注册重复名称的接口{name}");
99+
throw new InvalidOperationException($"不允许注册重复名称的工厂名称{name}");
87100
}
88101

89102
/// <summary>
@@ -95,7 +108,7 @@ public static THttpApiFactory Register<THttpApiFactory>(string name, THttpApiFac
95108
/// <returns></returns>
96109
public static TInterface Resolve<TInterface>() where TInterface : class, IHttpApi
97110
{
98-
var name = GetFactoryName<TInterface>();
111+
var name = GetFactoryName(typeof(TInterface));
99112
return Resolve<TInterface>(name);
100113
}
101114

@@ -138,11 +151,11 @@ public static HttpApi Resolve(string name)
138151
/// <summary>
139152
/// 返回类型的工厂名称
140153
/// </summary>
141-
/// <typeparam name="TInterface"></typeparam>
154+
/// <param name="interfaceType">接口类型</param>
142155
/// <returns></returns>
143-
private static string GetFactoryName<TInterface>()
156+
private static string GetFactoryName(Type interfaceType)
144157
{
145-
return typeof(TInterface).FullName;
158+
return interfaceType.FullName;
146159
}
147160
}
148161
}

WebApiClient/HttpApiFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class HttpApiFactory : IHttpApiFactory
5252
/// <summary>
5353
/// 获取接口类型
5454
/// </summary>
55-
protected Type InterfaceType { get; }
55+
public Type InterfaceType { get; }
5656

5757
/// <summary>
5858
/// HttpApi创建工厂

WebApiClient/IHttpApiFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ namespace WebApiClient
88
/// </summary>
99
public interface IHttpApiFactory
1010
{
11+
/// <summary>
12+
/// 获取接口类型
13+
/// </summary>
14+
Type InterfaceType { get; }
15+
1116
/// <summary>
1217
/// 创建接口的代理实例
1318
/// </summary>

0 commit comments

Comments
 (0)