Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/CatLib.Core.NetStandard/CatLib.Core.NetStandard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<OutputPath>..\..\build\NetStandard2.0\</OutputPath>
<DocumentationFile>..\..\build\NetStandard2.0\netstandard2.0\CatLib.Core.xml</DocumentationFile>
<DefineConstants>TRACE;RELEASE;NETSTANDARD2_0;CATLIB;RELEASE;NETSTANDARD2_0</DefineConstants>
<DefineConstants>TRACE;CATLIB;CATLIB_PERFORMANCE;RELEASE;NETSTANDARD2_0;RELEASE;NETSTANDARD2_0;RELEASE;NETSTANDARD2_0</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand All @@ -67,6 +67,7 @@
<Compile Include="..\CatLib.Core\Support\Attribute\PriorityAttribute.cs" Link="Support\Attribute\PriorityAttribute.cs" />
<Compile Include="..\CatLib.Core\Support\Container\Bindable.cs" Link="Support\Container\Bindable.cs" />
<Compile Include="..\CatLib.Core\Support\Container\BindData.cs" Link="Support\Container\BindData.cs" />
<Compile Include="..\CatLib.Core\Support\Container\BindDataExtend.cs" Link="Support\Container\BindDataExtend.cs" />
<Compile Include="..\CatLib.Core\Support\Container\Container.cs" Link="Support\Container\Container.cs" />
<Compile Include="..\CatLib.Core\Support\Container\ContainerExtend.cs" Link="Support\Container\ContainerExtend.cs" />
<Compile Include="..\CatLib.Core\Support\Container\GivenData.cs" Link="Support\Container\GivenData.cs" />
Expand Down
22 changes: 22 additions & 0 deletions src/CatLib.Core.Tests/CatLib/ApplicationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,28 @@ public void NoBootstrapInit()
});
}

/// <summary>
/// 测试终止程序
/// </summary>
[TestMethod]
public void TestTerminate()
{
var app = new Application();
var oldApp = App.Handler;
var num = 0;
oldApp.On(ApplicationEvents.OnTerminate, () =>
{
Assert.AreEqual(0, num++);
});
oldApp.On(ApplicationEvents.OnTerminated, () =>
{
Assert.AreEqual(1, num++);
});
App.Terminate();
Assert.AreNotEqual(oldApp, App.Handler);
Assert.AreEqual(2, num);
}

/// <summary>
/// 获取版本号测试
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/CatLib.Core.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@

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

[assembly: AssemblyVersion("1.2.1.0")]
[assembly: AssemblyFileVersion("1.2.1.0")]
[assembly: AssemblyVersion("1.2.2.0")]
[assembly: AssemblyFileVersion("1.2.2.0")]
30 changes: 27 additions & 3 deletions src/CatLib.Core.Tests/Support/Container/BindDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,29 @@ public void CheckIllegalAlias()
public void CanOnRelease()
{
var container = new Container();
var bindData = new BindData(container, "CanAddOnRelease", (app, param) => "hello world", true);
var bindData = container.Bind("CanAddOnRelease", (app, param) => "hello world", true);

bindData.OnRelease((bind, obj) =>
{
Assert.AreEqual("Test", obj);
Assert.AreSame(bindData, bind);
});

// double check
bindData.OnRelease((obj) =>
{
Assert.AreEqual("Test", obj);
});

var isCall = false;
bindData.OnRelease(()=>
{
isCall = true;
});

container.Instance("CanAddOnRelease", "Test");
container.Release("CanAddOnRelease");
Assert.AreEqual(true, isCall);
}
/// <summary>
/// 检查无效的解决事件传入参数
Expand All @@ -142,7 +155,7 @@ public void CanOnRelease()
public void CheckIllegalRelease()
{
var container = new Container();
var bindData = new BindData(container, "CheckIllegalRelease", (app, param) => "hello world", false);
var bindData = container.Bind("CheckIllegalRelease", (app, param) => "hello world", false);

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

ExceptionAssert.Throws<RuntimeException>(() =>
{
bindData.OnRelease((bind, obj) =>
bindData.OnRelease((obj) =>
{
Assert.Fail();
});
Expand All @@ -163,6 +176,17 @@ public void CheckIllegalRelease()
#endregion

#region OnResolving

[TestMethod]
public void TestAddOnResolvingWithExtend()
{
var container = new Container();
var bindData = new BindData(container, "CanAddOnResolving", (app, param) => "hello world", false);
bindData.OnResolving((obj) => Assert.AreEqual("hello world", obj));
var data = bindData.TriggerResolving("hello world");
Assert.AreEqual("hello world", data);
}

/// <summary>
/// 是否能追加到解决事件
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions src/CatLib.Core.Tests/Support/Container/ContainerHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,17 @@ public void TestWatchLambdaNoParam()
Assert.AreEqual(true, isCall);
}

[TestMethod]
public void TestReleaseWithObject()
{
var container = new Container();
container.Instance<string>("abc");
container.Instance<int>(10);
Assert.AreEqual(true, container.Release("abc", 10, null));
object[] data = null;
Assert.AreEqual(true, container.Release(data));
}

/// <summary>
/// 生成容器
/// </summary>
Expand Down
6 changes: 5 additions & 1 deletion src/CatLib.Core.Tests/Support/Container/ContainerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void CanTagService()
ExceptionAssert.DoesNotThrow(() =>
{
container.Tag("TestTag", "service1", "service2");
container.Tag<ContainerTest>("TestTag");
});
}

Expand Down Expand Up @@ -1016,9 +1017,12 @@ public void CanMakeWithResolve()

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

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

Assert.AreEqual(true, isTrigger);
Assert.AreEqual("local resolve global resolve", result);
}

Expand Down Expand Up @@ -2261,7 +2265,7 @@ public void TestFlushAndInstance()
var container = new Application();
container.Instance<Application>(container);

container.OnRelease((_, __) =>
container.OnRelease((__) =>
{
container.Instance<Application>(container);
});
Expand Down
3 changes: 2 additions & 1 deletion src/CatLib.Core/CatLib.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\build\Net35\</OutputPath>
<DefineConstants>TRACE;CATLIB</DefineConstants>
<DefineConstants>TRACE;CATLIB;CATLIB_PERFORMANCE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand All @@ -46,6 +46,7 @@
<Compile Include="CatLib\IServiceProvider.cs" />
<Compile Include="CatLib\IServiceProviderType.cs" />
<Compile Include="Support\Container\Bindable.cs" />
<Compile Include="Support\Container\BindDataExtend.cs" />
<Compile Include="Support\Container\IBindable.cs" />
<Compile Include="Support\Container\IMethodBind.cs" />
<Compile Include="Support\Container\IVariant.cs" />
Expand Down
62 changes: 59 additions & 3 deletions src/CatLib.Core/CatLib/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ public static IApplication Handler
#endregion

#region Application API
/// <summary>
/// 终止CatLib框架
/// </summary>
public static void Terminate()
{
Handler.Terminate();
}

/// <summary>
/// 注册服务提供者
/// </summary>
Expand Down Expand Up @@ -359,7 +367,11 @@ public static bool HasBind(string service)
/// <returns>是否已经静态化</returns>
public static bool HasInstance<TService>()
{
#if CATLIB_PERFORMANCE
return Facade<TService>.HasInstance || Handler.HasInstance<TService>();
#else
return Handler.HasInstance<TService>();
#endif
}

/// <summary>
Expand Down Expand Up @@ -654,9 +666,9 @@ public static string Type2Service(Type type)
{
return Handler.Type2Service(type);
}
#endregion
#endregion

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

/// <summary>
/// 为一个服务定义一个标记
/// </summary>
/// <typeparam name="TService">服务</typeparam>
/// <param name="tag">标记名</param>
public static void Tag<TService>(string tag)
{
Handler.Tag<TService>(tag);
}

/// <summary>
/// 静态化一个服务,实例值会经过解决修饰器
/// </summary>
Expand All @@ -1023,6 +1045,16 @@ public static bool Release<TService>()
return Handler.Release<TService>();
}

/// <summary>
/// 根据实例对象释放静态化实例
/// </summary>
/// <param name="instances">需要释放静态化实例对象</param>
/// <returns>只要有一个没有释放成功那么返回false</returns>
public static bool Release(params object[] instances)
{
return Handler.Release(instances);
}

/// <summary>
/// 以依赖注入形式调用一个方法
/// </summary>
Expand Down Expand Up @@ -1123,7 +1155,11 @@ public static Action Wrap<T1, T2, T3, T4>(Action<T1, T2, T3, T4> method, params
/// <returns>服务实例</returns>
public static TService Make<TService>(params object[] userParams)
{
#if CATLIB_PERFORMANCE
return Facade<TService>.Make(userParams);
#else
return Handler.Make<TService>(userParams);
#endif
}

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

/// <summary>
/// 当静态服务被释放时
/// </summary>
/// <param name="callback">处理释放时的回调</param>
/// <returns>当前容器实例</returns>
public static IContainer OnRelease(Action<object> callback)
{
return Handler.OnRelease(callback);
}

/// <summary>
/// 当服务被解决时,生成的服务会经过注册的回调函数
/// </summary>
/// <param name="callback">回调函数</param>
/// <returns>当前容器对象</returns>
public static IContainer OnResolving(Action<object> callback)
{
return Handler.OnResolving(callback);
}

/// <summary>
/// 关注指定的服务,当服务触发重定义时调用指定对象的指定方法
/// <param>调用是以依赖注入的形式进行的</param>
Expand Down Expand Up @@ -1213,6 +1269,6 @@ public static string Type2Service<TService>()
{
return Handler.Type2Service<TService>();
}
#endregion
#endregion
}
}
Loading