@@ -23,8 +23,8 @@ public partial class HttpApi
23
23
/// <returns></returns>
24
24
public static HttpApiFactory < TInterface > Register < TInterface > ( ) where TInterface : class , IHttpApi
25
25
{
26
- var name = GetFactoryName < TInterface > ( ) ;
27
- return Register < TInterface > ( name ) ;
26
+ var factory = new HttpApiFactory < TInterface > ( ) ;
27
+ return RegisterFactory ( factory ) ;
28
28
}
29
29
30
30
/// <summary>
@@ -39,7 +39,7 @@ public static HttpApiFactory<TInterface> Register<TInterface>() where TInterface
39
39
public static HttpApiFactory < TInterface > Register < TInterface > ( string name ) where TInterface : class , IHttpApi
40
40
{
41
41
var factory = new HttpApiFactory < TInterface > ( ) ;
42
- return Register ( name , factory ) ;
42
+ return RegisterFactory ( factory , name ) ;
43
43
}
44
44
45
45
/// <summary>
@@ -55,35 +55,48 @@ public static HttpApiFactory<TInterface> Register<TInterface>(string name) where
55
55
public static HttpApiFactory Register ( string name , Type interfaceType )
56
56
{
57
57
var factory = new HttpApiFactory ( interfaceType ) ;
58
- return Register ( name , factory ) ;
58
+ return RegisterFactory ( factory , name ) ;
59
59
}
60
60
61
61
/// <summary>
62
62
/// 注册指定Api工厂
63
63
/// </summary>
64
- /// <typeparam name="THttpApiFactory"></typeparam>
65
- /// <param name="name">工厂名称</param>
64
+ /// <typeparam name="THttpApiFactory"></typeparam>
66
65
/// <param name="httpApiFactory">工厂实例</param>
67
66
/// <exception cref="ArgumentNullException"></exception>
68
67
/// <exception cref="InvalidOperationException"></exception>
69
68
/// <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
71
70
{
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
+ }
76
74
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
+ {
77
86
if ( httpApiFactory == null )
78
87
{
79
88
throw new ArgumentNullException ( nameof ( httpApiFactory ) ) ;
80
89
}
90
+ if ( string . IsNullOrEmpty ( name ) == true )
91
+ {
92
+ throw new ArgumentNullException ( nameof ( name ) ) ;
93
+ }
81
94
82
95
if ( factories . TryAdd ( name , httpApiFactory ) == true )
83
96
{
84
97
return httpApiFactory ;
85
98
}
86
- throw new InvalidOperationException ( $ "不允许注册重复名称的接口 :{ name } ") ;
99
+ throw new InvalidOperationException ( $ "不允许注册重复名称的工厂名称 :{ name } ") ;
87
100
}
88
101
89
102
/// <summary>
@@ -95,7 +108,7 @@ public static THttpApiFactory Register<THttpApiFactory>(string name, THttpApiFac
95
108
/// <returns></returns>
96
109
public static TInterface Resolve < TInterface > ( ) where TInterface : class , IHttpApi
97
110
{
98
- var name = GetFactoryName < TInterface > ( ) ;
111
+ var name = GetFactoryName ( typeof ( TInterface ) ) ;
99
112
return Resolve < TInterface > ( name ) ;
100
113
}
101
114
@@ -138,11 +151,11 @@ public static HttpApi Resolve(string name)
138
151
/// <summary>
139
152
/// 返回类型的工厂名称
140
153
/// </summary>
141
- /// <typeparam name="TInterface"></typeparam >
154
+ /// <param name="interfaceType">接口类型</param >
142
155
/// <returns></returns>
143
- private static string GetFactoryName < TInterface > ( )
156
+ private static string GetFactoryName ( Type interfaceType )
144
157
{
145
- return typeof ( TInterface ) . FullName ;
158
+ return interfaceType . FullName ;
146
159
}
147
160
}
148
161
}
0 commit comments