From 8b28318a9fe8cf157fdd1f35e46db6d77dae53d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=96=B5=E5=96=B5=E5=A4=A7=E4=BA=BA?= Date: Mon, 26 Nov 2018 18:10:13 +0800 Subject: [PATCH 1/2] resolved #79 --- src/CatLib.Core/CatLib/App.cs | 8 +++---- .../Support/Container/ContainerExtend.cs | 22 +++++++++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/CatLib.Core/CatLib/App.cs b/src/CatLib.Core/CatLib/App.cs index 97ed474..1438d28 100644 --- a/src/CatLib.Core/CatLib/App.cs +++ b/src/CatLib.Core/CatLib/App.cs @@ -598,9 +598,9 @@ public static Func Factory(string service) /// /// 服务名或别名 /// 闭包 - public static void Extend(Func closure) + public static void Extend(Func closure) { - Handler.Extend(closure); + Handler.Extend(closure); } /// @@ -610,9 +610,9 @@ public static void Extend(Func closure) /// /// 服务名或别名 /// 闭包 - public static void Extend(Func closure) + public static void Extend(Func closure) { - Handler.Extend(closure); + Handler.Extend(closure); } /// diff --git a/src/CatLib.Core/Support/Container/ContainerExtend.cs b/src/CatLib.Core/Support/Container/ContainerExtend.cs index ad935b7..e8fd119 100644 --- a/src/CatLib.Core/Support/Container/ContainerExtend.cs +++ b/src/CatLib.Core/Support/Container/ContainerExtend.cs @@ -727,9 +727,16 @@ public static Func Factory(this IContainer container, params /// 服务名或别名 /// 服务容器 /// 闭包 - public static void Extend(this IContainer container, Func closure) + public static void Extend(this IContainer container, Func closure) { - container.Extend(container.Type2Service(typeof(TService)), closure); + container.Extend(container.Type2Service(typeof(TService)), (instance, c) => + { + if (instance is TService) + { + return closure((TService)instance, c); + } + return instance; + }); } /// @@ -740,9 +747,16 @@ public static void Extend(this IContainer container, Func服务名或别名 /// 服务容器 /// 闭包 - public static void Extend(this IContainer container, Func closure) + public static void Extend(this IContainer container, Func closure) { - container.Extend(container.Type2Service(typeof(TService)), (instance, _) => closure(instance)); + container.Extend(container.Type2Service(typeof(TService)), (instance, _) => + { + if (instance is TService) + { + return closure((TService)instance); + } + return instance; + }); } /// From a5c41bba76cf996f8711d90323168030613018b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=96=B5=E5=96=B5=E5=A4=A7=E4=BA=BA?= Date: Mon, 26 Nov 2018 18:18:12 +0800 Subject: [PATCH 2/2] sync to app --- src/CatLib.Core/CatLib/App.cs | 104 ++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/src/CatLib.Core/CatLib/App.cs b/src/CatLib.Core/CatLib/App.cs index 1438d28..f4ca226 100644 --- a/src/CatLib.Core/CatLib/App.cs +++ b/src/CatLib.Core/CatLib/App.cs @@ -591,6 +591,28 @@ public static Func Factory(string service) return Handler.Factory(service); } + /// + /// 扩展容器中的服务 + /// 允许在服务构建的过程中配置或者替换服务 + /// 如果服务已经被构建,拓展会立即生效。 + /// + /// 闭包 + public static void Extend(Func closure) + { + Handler.Extend(closure); + } + + /// + /// 扩展容器中的服务 + /// 允许在服务构建的过程中配置或者替换服务 + /// 如果服务已经被构建,拓展会立即生效。 + /// + /// 闭包 + public static void Extend(Func closure) + { + Handler.Extend(closure); + } + /// /// 扩展容器中的服务 /// 允许在服务构建的过程中配置或者替换服务 @@ -1241,6 +1263,26 @@ public static IContainer OnRelease(Action callback) return Handler.OnRelease(callback); } + /// + /// 当静态服务被释放时 + /// + /// 处理释放时的回调 + /// 当前容器实例 + public static IContainer OnRelease(Action closure) + { + return Handler.OnRelease(closure); + } + + /// + /// 当静态服务被释放时 + /// + /// 处理释放时的回调 + /// 当前容器实例 + public static IContainer OnRelease(Action closure) + { + return Handler.OnRelease(closure); + } + /// /// 当服务被解决时,生成的服务会经过注册的回调函数 /// @@ -1251,6 +1293,68 @@ public static IContainer OnResolving(Action callback) return Handler.OnResolving(callback); } + /// + /// 当服务被解决时,生成的服务会经过注册的回调函数 + /// + /// 回调函数 + /// 当前容器对象 + public static IContainer OnResolving(Action closure) + { + return Handler.OnResolving(closure); + } + + /// + /// 当服务被解决时,生成的服务会经过注册的回调函数 + /// + /// 回调函数 + /// 当前容器对象 + public static IContainer OnResolving(Action closure) + { + return Handler.OnResolving(closure); + } + + /// + /// 当服务被解决事件之后的回调 + /// + /// 闭包 + /// 当前容器 + public static IContainer OnAfterResolving(Action closure) + { + return Handler.OnAfterResolving(closure); + } + + /// + /// 当服务被解决事件之后的回调 + /// + /// 闭包 + /// 当前容器 + public static IContainer OnAfterResolving(Action closure) + { + return Handler.OnAfterResolving(closure); + } + + /// + /// 当服务被解决事件之后的回调 + /// + /// 筛选条件 + /// 闭包 + /// 当前容器 + public static IContainer OnAfterResolving(Action closure) + { + return Handler.OnAfterResolving(closure); + } + + /// + /// 当服务被解决事件之后的回调 + /// + /// 筛选条件 + /// 闭包 + /// 当前容器 + public static IContainer OnAfterResolving(Action closure) + { + return Handler.OnAfterResolving(closure); + } + /// /// 关注指定的服务,当服务触发重定义时调用指定对象的指定方法 /// 调用是以依赖注入的形式进行的