Skip to content

Commit 569814c

Browse files
author
喵喵大人
authored
Merge pull request #18 from CatLib/feature/1.2.2
Feature/1.2.2
2 parents 29cf824 + 47891ae commit 569814c

File tree

15 files changed

+326
-27
lines changed

15 files changed

+326
-27
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
4242
<OutputPath>..\..\build\NetStandard2.0\</OutputPath>
4343
<DocumentationFile>..\..\build\NetStandard2.0\netstandard2.0\CatLib.Core.xml</DocumentationFile>
44-
<DefineConstants>TRACE;RELEASE;NETSTANDARD2_0;CATLIB;RELEASE;NETSTANDARD2_0</DefineConstants>
44+
<DefineConstants>TRACE;CATLIB;CATLIB_PERFORMANCE;RELEASE;NETSTANDARD2_0;RELEASE;NETSTANDARD2_0;RELEASE;NETSTANDARD2_0</DefineConstants>
4545
</PropertyGroup>
4646

4747
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
@@ -67,6 +67,7 @@
6767
<Compile Include="..\CatLib.Core\Support\Attribute\PriorityAttribute.cs" Link="Support\Attribute\PriorityAttribute.cs" />
6868
<Compile Include="..\CatLib.Core\Support\Container\Bindable.cs" Link="Support\Container\Bindable.cs" />
6969
<Compile Include="..\CatLib.Core\Support\Container\BindData.cs" Link="Support\Container\BindData.cs" />
70+
<Compile Include="..\CatLib.Core\Support\Container\BindDataExtend.cs" Link="Support\Container\BindDataExtend.cs" />
7071
<Compile Include="..\CatLib.Core\Support\Container\Container.cs" Link="Support\Container\Container.cs" />
7172
<Compile Include="..\CatLib.Core\Support\Container\ContainerExtend.cs" Link="Support\Container\ContainerExtend.cs" />
7273
<Compile Include="..\CatLib.Core\Support\Container\GivenData.cs" Link="Support\Container\GivenData.cs" />

src/CatLib.Core.Tests/CatLib/ApplicationTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,28 @@ public void NoBootstrapInit()
106106
});
107107
}
108108

109+
/// <summary>
110+
/// 测试终止程序
111+
/// </summary>
112+
[TestMethod]
113+
public void TestTerminate()
114+
{
115+
var app = new Application();
116+
var oldApp = App.Handler;
117+
var num = 0;
118+
oldApp.On(ApplicationEvents.OnTerminate, () =>
119+
{
120+
Assert.AreEqual(0, num++);
121+
});
122+
oldApp.On(ApplicationEvents.OnTerminated, () =>
123+
{
124+
Assert.AreEqual(1, num++);
125+
});
126+
App.Terminate();
127+
Assert.AreNotEqual(oldApp, App.Handler);
128+
Assert.AreEqual(2, num);
129+
}
130+
109131
/// <summary>
110132
/// 获取版本号测试
111133
/// </summary>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@
2525

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

28-
[assembly: AssemblyVersion("1.2.1.0")]
29-
[assembly: AssemblyFileVersion("1.2.1.0")]
28+
[assembly: AssemblyVersion("1.2.2.0")]
29+
[assembly: AssemblyFileVersion("1.2.2.0")]

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,29 @@ public void CheckIllegalAlias()
124124
public void CanOnRelease()
125125
{
126126
var container = new Container();
127-
var bindData = new BindData(container, "CanAddOnRelease", (app, param) => "hello world", true);
127+
var bindData = container.Bind("CanAddOnRelease", (app, param) => "hello world", true);
128128

129129
bindData.OnRelease((bind, obj) =>
130130
{
131131
Assert.AreEqual("Test", obj);
132132
Assert.AreSame(bindData, bind);
133133
});
134134

135+
// double check
136+
bindData.OnRelease((obj) =>
137+
{
138+
Assert.AreEqual("Test", obj);
139+
});
140+
141+
var isCall = false;
142+
bindData.OnRelease(()=>
143+
{
144+
isCall = true;
145+
});
146+
135147
container.Instance("CanAddOnRelease", "Test");
136148
container.Release("CanAddOnRelease");
149+
Assert.AreEqual(true, isCall);
137150
}
138151
/// <summary>
139152
/// 检查无效的解决事件传入参数
@@ -142,7 +155,7 @@ public void CanOnRelease()
142155
public void CheckIllegalRelease()
143156
{
144157
var container = new Container();
145-
var bindData = new BindData(container, "CheckIllegalRelease", (app, param) => "hello world", false);
158+
var bindData = container.Bind("CheckIllegalRelease", (app, param) => "hello world", false);
146159

147160
ExceptionAssert.Throws<ArgumentNullException>(() =>
148161
{
@@ -151,7 +164,7 @@ public void CheckIllegalRelease()
151164

152165
ExceptionAssert.Throws<RuntimeException>(() =>
153166
{
154-
bindData.OnRelease((bind, obj) =>
167+
bindData.OnRelease((obj) =>
155168
{
156169
Assert.Fail();
157170
});
@@ -163,6 +176,17 @@ public void CheckIllegalRelease()
163176
#endregion
164177

165178
#region OnResolving
179+
180+
[TestMethod]
181+
public void TestAddOnResolvingWithExtend()
182+
{
183+
var container = new Container();
184+
var bindData = new BindData(container, "CanAddOnResolving", (app, param) => "hello world", false);
185+
bindData.OnResolving((obj) => Assert.AreEqual("hello world", obj));
186+
var data = bindData.TriggerResolving("hello world");
187+
Assert.AreEqual("hello world", data);
188+
}
189+
166190
/// <summary>
167191
/// 是否能追加到解决事件
168192
/// </summary>

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,17 @@ public void TestWatchLambdaNoParam()
309309
Assert.AreEqual(true, isCall);
310310
}
311311

312+
[TestMethod]
313+
public void TestReleaseWithObject()
314+
{
315+
var container = new Container();
316+
container.Instance<string>("abc");
317+
container.Instance<int>(10);
318+
Assert.AreEqual(true, container.Release("abc", 10, null));
319+
object[] data = null;
320+
Assert.AreEqual(true, container.Release(data));
321+
}
322+
312323
/// <summary>
313324
/// 生成容器
314325
/// </summary>

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public void CanTagService()
3333
ExceptionAssert.DoesNotThrow(() =>
3434
{
3535
container.Tag("TestTag", "service1", "service2");
36+
container.Tag<ContainerTest>("TestTag");
3637
});
3738
}
3839

@@ -1016,9 +1017,12 @@ public void CanMakeWithResolve()
10161017

10171018
bind.OnResolving((bindData, obj) => "local resolve");
10181019
container.OnResolving((bindData, obj) => obj + " global resolve");
1020+
var isTrigger = false;
1021+
container.OnResolving((obj) => isTrigger = true);
10191022

10201023
var result = container.Make(container.Type2Service(typeof(MakeTestClassDependency)));
10211024

1025+
Assert.AreEqual(true, isTrigger);
10221026
Assert.AreEqual("local resolve global resolve", result);
10231027
}
10241028

@@ -2261,7 +2265,7 @@ public void TestFlushAndInstance()
22612265
var container = new Application();
22622266
container.Instance<Application>(container);
22632267

2264-
container.OnRelease((_, __) =>
2268+
container.OnRelease((__) =>
22652269
{
22662270
container.Instance<Application>(container);
22672271
});

src/CatLib.Core/CatLib.Core.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<DebugType>pdbonly</DebugType>
2727
<Optimize>true</Optimize>
2828
<OutputPath>..\..\build\Net35\</OutputPath>
29-
<DefineConstants>TRACE;CATLIB</DefineConstants>
29+
<DefineConstants>TRACE;CATLIB;CATLIB_PERFORMANCE</DefineConstants>
3030
<ErrorReport>prompt</ErrorReport>
3131
<WarningLevel>4</WarningLevel>
3232
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
@@ -46,6 +46,7 @@
4646
<Compile Include="CatLib\IServiceProvider.cs" />
4747
<Compile Include="CatLib\IServiceProviderType.cs" />
4848
<Compile Include="Support\Container\Bindable.cs" />
49+
<Compile Include="Support\Container\BindDataExtend.cs" />
4950
<Compile Include="Support\Container\IBindable.cs" />
5051
<Compile Include="Support\Container\IMethodBind.cs" />
5152
<Compile Include="Support\Container\IVariant.cs" />

src/CatLib.Core/CatLib/App.cs

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ public static IApplication Handler
5757
#endregion
5858

5959
#region Application API
60+
/// <summary>
61+
/// 终止CatLib框架
62+
/// </summary>
63+
public static void Terminate()
64+
{
65+
Handler.Terminate();
66+
}
67+
6068
/// <summary>
6169
/// 注册服务提供者
6270
/// </summary>
@@ -359,7 +367,11 @@ public static bool HasBind(string service)
359367
/// <returns>是否已经静态化</returns>
360368
public static bool HasInstance<TService>()
361369
{
370+
#if CATLIB_PERFORMANCE
371+
return Facade<TService>.HasInstance || Handler.HasInstance<TService>();
372+
#else
362373
return Handler.HasInstance<TService>();
374+
#endif
363375
}
364376

365377
/// <summary>
@@ -654,9 +666,9 @@ public static string Type2Service(Type type)
654666
{
655667
return Handler.Type2Service(type);
656668
}
657-
#endregion
669+
#endregion
658670

659-
#region Container Extend API
671+
#region Container Extend API
660672
/// <summary>
661673
/// 获取服务的绑定数据,如果绑定不存在则返回null
662674
/// </summary>
@@ -1004,6 +1016,16 @@ public static void Unbind<TService>()
10041016
Handler.Unbind<TService>();
10051017
}
10061018

1019+
/// <summary>
1020+
/// 为一个服务定义一个标记
1021+
/// </summary>
1022+
/// <typeparam name="TService">服务</typeparam>
1023+
/// <param name="tag">标记名</param>
1024+
public static void Tag<TService>(string tag)
1025+
{
1026+
Handler.Tag<TService>(tag);
1027+
}
1028+
10071029
/// <summary>
10081030
/// 静态化一个服务,实例值会经过解决修饰器
10091031
/// </summary>
@@ -1023,6 +1045,16 @@ public static bool Release<TService>()
10231045
return Handler.Release<TService>();
10241046
}
10251047

1048+
/// <summary>
1049+
/// 根据实例对象释放静态化实例
1050+
/// </summary>
1051+
/// <param name="instances">需要释放静态化实例对象</param>
1052+
/// <returns>只要有一个没有释放成功那么返回false</returns>
1053+
public static bool Release(params object[] instances)
1054+
{
1055+
return Handler.Release(instances);
1056+
}
1057+
10261058
/// <summary>
10271059
/// 以依赖注入形式调用一个方法
10281060
/// </summary>
@@ -1123,7 +1155,11 @@ public static Action Wrap<T1, T2, T3, T4>(Action<T1, T2, T3, T4> method, params
11231155
/// <returns>服务实例</returns>
11241156
public static TService Make<TService>(params object[] userParams)
11251157
{
1158+
#if CATLIB_PERFORMANCE
11261159
return Facade<TService>.Make(userParams);
1160+
#else
1161+
return Handler.Make<TService>(userParams);
1162+
#endif
11271163
}
11281164

11291165
/// <summary>
@@ -1147,6 +1183,26 @@ public static Func<TService> Factory<TService>(params object[] userParams)
11471183
return () => Make<TService>(userParams);
11481184
}
11491185

1186+
/// <summary>
1187+
/// 当静态服务被释放时
1188+
/// </summary>
1189+
/// <param name="callback">处理释放时的回调</param>
1190+
/// <returns>当前容器实例</returns>
1191+
public static IContainer OnRelease(Action<object> callback)
1192+
{
1193+
return Handler.OnRelease(callback);
1194+
}
1195+
1196+
/// <summary>
1197+
/// 当服务被解决时,生成的服务会经过注册的回调函数
1198+
/// </summary>
1199+
/// <param name="callback">回调函数</param>
1200+
/// <returns>当前容器对象</returns>
1201+
public static IContainer OnResolving(Action<object> callback)
1202+
{
1203+
return Handler.OnResolving(callback);
1204+
}
1205+
11501206
/// <summary>
11511207
/// 关注指定的服务,当服务触发重定义时调用指定对象的指定方法
11521208
/// <param>调用是以依赖注入的形式进行的</param>
@@ -1213,6 +1269,6 @@ public static string Type2Service<TService>()
12131269
{
12141270
return Handler.Type2Service<TService>();
12151271
}
1216-
#endregion
1272+
#endregion
12171273
}
12181274
}

0 commit comments

Comments
 (0)