Skip to content

Commit 28ff880

Browse files
committed
params optimization
1 parent 654288a commit 28ff880

File tree

9 files changed

+68
-22
lines changed

9 files changed

+68
-22
lines changed

src/CatLib.Core.NetStandard/CatLib.Core.NetStandard.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<Compile Include="..\CatLib.Core\Support\Container\IGivenData.cs" Link="Support\Container\IGivenData.cs" />
7878
<Compile Include="..\CatLib.Core\Support\Container\IMethodBind.cs" Link="Support\Container\IMethodBind.cs" />
7979
<Compile Include="..\CatLib.Core\Support\Container\InjectAttribute.cs" Link="Support\Container\InjectAttribute.cs" />
80+
<Compile Include="..\CatLib.Core\Support\Container\IParams.cs" Link="Support\Container\IParams.cs" />
8081
<Compile Include="..\CatLib.Core\Support\Container\IVariant.cs" Link="Support\Container\IVariant.cs" />
8182
<Compile Include="..\CatLib.Core\Support\Container\MethodBind.cs" Link="Support\Container\MethodBind.cs" />
8283
<Compile Include="..\CatLib.Core\Support\Container\MethodContainer.cs" Link="Support\Container\MethodContainer.cs" />

src/CatLib.Core.Tests/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
[assembly: AssemblyConfiguration("")]
1818
[assembly: AssemblyCompany("CatLib")]
1919
[assembly: AssemblyProduct("CatLib.Core.Tests")]
20-
[assembly: AssemblyCopyright("Copyright © CatLib 2017")]
20+
[assembly: AssemblyCopyright("Copyright © CatLib 2017-2018")]
2121
[assembly: AssemblyTrademark("")]
2222
[assembly: AssemblyCulture("")]
2323

2424
[assembly: ComVisible(false)]
2525

2626
[assembly: Guid("3c9f4024-910c-4881-a04d-34a6c3a09019")]
2727

28-
[assembly: AssemblyVersion("1.2.3.0")]
29-
[assembly: AssemblyFileVersion("1.2.3.0")]
28+
[assembly: AssemblyVersion("1.2.4.0")]
29+
[assembly: AssemblyFileVersion("1.2.4.0")]

src/CatLib.Core/CatLib.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<Compile Include="Support\Container\BindDataExtend.cs" />
5050
<Compile Include="Support\Container\IBindable.cs" />
5151
<Compile Include="Support\Container\IMethodBind.cs" />
52+
<Compile Include="Support\Container\IParams.cs" />
5253
<Compile Include="Support\Container\IVariant.cs" />
5354
<Compile Include="Support\Container\MethodBind.cs" />
5455
<Compile Include="Support\Container\MethodContainer.cs" />

src/CatLib.Core/CatLib/App.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,9 +666,9 @@ public static string Type2Service(Type type)
666666
{
667667
return Handler.Type2Service(type);
668668
}
669-
#endregion
669+
#endregion
670670

671-
#region Container Extend API
671+
#region Container Extend API
672672
/// <summary>
673673
/// 获取服务的绑定数据,如果绑定不存在则返回null
674674
/// </summary>
@@ -1269,6 +1269,6 @@ public static string Type2Service<TService>()
12691269
{
12701270
return Handler.Type2Service<TService>();
12711271
}
1272-
#endregion
1272+
#endregion
12731273
}
12741274
}

src/CatLib.Core/CatLib/Facade.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ private static void Rebind(IBindData newBinder)
166166
/// <returns>服务实例</returns>
167167
private static TService Build(params object[] userParams)
168168
{
169-
return (TService) App.Make(service, userParams);
169+
return (TService)App.Make(service, userParams);
170170
}
171171
}
172172
}

src/CatLib.Core/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818
[assembly: AssemblyConfiguration("")]
1919
[assembly: AssemblyCompany("CatLib")]
2020
[assembly: AssemblyProduct("CatLib.Core")]
21-
[assembly: AssemblyCopyright("Copyright © CatLib 2017")]
21+
[assembly: AssemblyCopyright("Copyright © CatLib 2017-2018")]
2222
[assembly: AssemblyTrademark("")]
2323
[assembly: AssemblyCulture("")]
2424

2525
[assembly: ComVisible(false)]
2626

2727
[assembly: Guid("4204658e-81fd-4106-a347-890cd369c8a4")]
2828

29-
[assembly: AssemblyVersion("1.2.3.0")]
30-
[assembly: AssemblyFileVersion("1.2.3.0")]
29+
[assembly: AssemblyVersion("1.2.4.0")]
30+
[assembly: AssemblyFileVersion("1.2.4.0")]
3131

3232
[assembly: InternalsVisibleTo("Assembly-CSharp-Editor"),
3333
InternalsVisibleTo("Assembly-CSharp-Editor-firstpass"),

src/CatLib.Core/Support/Container/Container.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,7 @@ protected virtual object GetCompactInjectUserParams(ParameterInfo baseParam, ref
13591359
}
13601360

13611361
/// <summary>
1362-
/// 获取参数(<see cref="Params"/>)匹配器
1362+
/// 获取参数(<see cref="IParams"/>)匹配器
13631363
/// <para>开发者重写后可以实现自己的匹配器</para>
13641364
/// <para>如果调用获取到的匹配器后返回结果为null则表示没有匹配到参数</para>
13651365
/// </summary>
@@ -1733,27 +1733,27 @@ private BindData GetBindFillable(string service)
17331733
}
17341734

17351735
/// <summary>
1736-
/// 从<paramref name="userParams"/>中获取<see cref="Params"/>类型的变量
1736+
/// 从<paramref name="userParams"/>中获取<see cref="IParams"/>类型的变量
17371737
/// </summary>
17381738
/// <param name="userParams">用户传入参数</param>
17391739
/// <returns>获取到的参数</returns>
1740-
private Params[] GetParamsTypeInUserParams(ref object[] userParams)
1740+
private IParams[] GetParamsTypeInUserParams(ref object[] userParams)
17411741
{
1742-
var elements = Arr.Remove(ref userParams, value => value is Params);
1743-
var results = new Params[elements.Length];
1742+
var elements = Arr.Remove(ref userParams, value => value is IParams);
1743+
var results = new IParams[elements.Length];
17441744
for (var i = 0; i < elements.Length; i++)
17451745
{
1746-
results[i] = (Params)elements[i];
1746+
results[i] = (IParams)elements[i];
17471747
}
17481748
return results;
17491749
}
17501750

17511751
/// <summary>
1752-
/// 生成一个默认的参数<see cref="Params" />匹配器
1752+
/// 生成一个默认的参数<see cref="IParams" />匹配器
17531753
/// </summary>
17541754
/// <param name="tables">参数表</param>
17551755
/// <returns>匹配器</returns>
1756-
private Func<ParameterInfo, object> MakeParamsMatcher(Params[] tables)
1756+
private Func<ParameterInfo, object> MakeParamsMatcher(IParams[] tables)
17571757
{
17581758
// 默认匹配器策略将会将参数名和参数表的参数名进行匹配
17591759
// 最先匹配到的有效参数值将作为返回值返回
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* This file is part of the CatLib package.
3+
*
4+
* (c) Yu Bin <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* Document: http://catlib.io/
10+
*/
11+
12+
namespace CatLib
13+
{
14+
/// <summary>
15+
/// 参数名注入表
16+
/// </summary>
17+
public interface IParams
18+
{
19+
/// <summary>
20+
/// 获取一个参数
21+
/// </summary>
22+
/// <param name="key">参数名</param>
23+
/// <param name="value">参数值</param>
24+
/// <returns>是否成功获取</returns>
25+
bool TryGetValue(string key, out object value);
26+
}
27+
}

src/CatLib.Core/Support/Container/Params.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ namespace CatLib
1717
/// 参数名注入表
1818
/// </summary>
1919
[ExcludeFromCodeCoverage]
20-
public sealed class Params : IEnumerable<KeyValuePair<string, object>>
20+
public sealed class Params : IParams, IEnumerable<KeyValuePair<string, object>>
2121
{
2222
/// <summary>
2323
/// 参数表
2424
/// </summary>
25-
private readonly Dictionary<string, object> table = new Dictionary<string, object>();
25+
private readonly IDictionary<string, object> table;
2626

2727
/// <summary>
2828
/// 迭代器
@@ -34,14 +34,31 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
3434
}
3535

3636
/// <summary>
37-
///
37+
/// 迭代器
3838
/// </summary>
39-
/// <returns></returns>
39+
/// <returns>迭代器</returns>
4040
IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
4141
{
4242
return table.GetEnumerator();
4343
}
4444

45+
/// <summary>
46+
/// 参数名注入表
47+
/// </summary>
48+
public Params()
49+
{
50+
table = new Dictionary<string, object>();
51+
}
52+
53+
/// <summary>
54+
/// 参数名注入表
55+
/// </summary>
56+
/// <param name="args">参数表</param>
57+
public Params(IDictionary<string, object> args)
58+
{
59+
table = args;
60+
}
61+
4562
/// <summary>
4663
/// 获取或者设定一个参数
4764
/// </summary>

0 commit comments

Comments
 (0)