From 2901d0f8874c348084b9bc487b83acf178103724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=96=B5=E5=96=B5=E5=A4=A7=E4=BA=BA?= Date: Sat, 29 Dec 2018 16:58:45 +0800 Subject: [PATCH] resolved #154 --- .../Support/Container/ContainerHelperTests.cs | 23 +++++++++++++++++ src/CatLib.Core/CatLib/App.cs | 24 +++++++++++++++++- .../Support/Container/ExtendContainer.cs | 25 +++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/src/CatLib.Core.Tests/Support/Container/ContainerHelperTests.cs b/src/CatLib.Core.Tests/Support/Container/ContainerHelperTests.cs index 3a47a05..192e026 100644 --- a/src/CatLib.Core.Tests/Support/Container/ContainerHelperTests.cs +++ b/src/CatLib.Core.Tests/Support/Container/ContainerHelperTests.cs @@ -433,6 +433,29 @@ public void TestExtendContainerWithService2() Assert.AreEqual("hello world", container.Make("abc")); } + [TestMethod] + public void TestExtendWithServiceName() + { + var container = new Container(); + container.Extend((instance) => null); + container.Bind(); + + Assert.AreEqual(null, container.Make()); + } + + [TestMethod] + public void TestExtendWithServiceName2() + { + var container = new Container(); + container.Extend((instance, c) => + { + Assert.AreEqual(container, c); + return null; + }); + container.Bind(); + Assert.AreEqual(null, container.Make()); + } + public interface ITypeMatchInterface { diff --git a/src/CatLib.Core/CatLib/App.cs b/src/CatLib.Core/CatLib/App.cs index 14f3985..4eef8f8 100644 --- a/src/CatLib.Core/CatLib/App.cs +++ b/src/CatLib.Core/CatLib/App.cs @@ -790,6 +790,28 @@ public static void Extend(string service, Func closure) /// /// 扩展容器中的服务 + /// 允许在服务构建的过程中配置或者替换服务 + /// 如果服务已经被构建,拓展会立即生效。 + /// + /// 闭包 + public static void Extend(Func closure) + { + Handler.Extend(closure); + } + + /// + /// 扩展容器中的服务 + /// 允许在服务构建的过程中配置或者替换服务 + /// 如果服务已经被构建,拓展会立即生效。 + /// + /// 闭包 + public static void Extend(Func closure) + { + Handler.Extend(closure); + } + + /// + /// 扩展容器中的全部服务 /// 如果构建的实例符合指定的类型或者接口,那么触发扩展闭包 /// /// 实现的类型或接口 @@ -800,7 +822,7 @@ public static void Extend(Func closure } /// - /// 扩展容器中的服务 + /// 扩展容器中的全部服务 /// 如果构建的实例符合指定的类型或者接口,那么触发扩展闭包 /// /// 实现的类型或接口 diff --git a/src/CatLib.Core/Support/Container/ExtendContainer.cs b/src/CatLib.Core/Support/Container/ExtendContainer.cs index 6079144..768ac2d 100644 --- a/src/CatLib.Core/Support/Container/ExtendContainer.cs +++ b/src/CatLib.Core/Support/Container/ExtendContainer.cs @@ -720,6 +720,31 @@ public static void Extend(this IContainer container, string service, Func closure(instance)); } + /// + /// 扩展容器中的服务 + /// 允许在服务构建的过程中配置或者替换服务 + /// 如果服务已经被构建,拓展会立即生效。 + /// + /// 服务容器 + /// 闭包 + public static void Extend(this IContainer container, Func closure) + { + container.Extend(container.Type2Service(typeof(TService)), (instance, c) => closure((TConcrete) instance)); + } + + /// + /// 扩展容器中的服务 + /// 允许在服务构建的过程中配置或者替换服务 + /// 如果服务已经被构建,拓展会立即生效。 + /// + /// 服务容器 + /// 闭包 + public static void Extend(this IContainer container, Func closure) + { + container.Extend(container.Type2Service(typeof(TService)), + (instance, c) => closure((TConcrete) instance, c)); + } + /// /// 扩展容器中的服务 /// 如果构建的实例符合指定的类型或者接口,那么触发扩展闭包