Skip to content

Commit

Permalink
resolved #154
Browse files Browse the repository at this point in the history
  • Loading branch information
ymh199478 committed Dec 29, 2018
1 parent 6e95d55 commit 2901d0f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/CatLib.Core.Tests/Support/Container/ContainerHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,29 @@ public void TestExtendContainerWithService2()
Assert.AreEqual("hello world", container.Make("abc"));
}

[TestMethod]
public void TestExtendWithServiceName()
{
var container = new Container();
container.Extend<ITypeMatchInterface, TestTypeMatchOnResolvingClass>((instance) => null);
container.Bind<ITypeMatchInterface, TestTypeMatchOnResolvingClass>();

Assert.AreEqual(null, container.Make<ITypeMatchInterface>());
}

[TestMethod]
public void TestExtendWithServiceName2()
{
var container = new Container();
container.Extend<ITypeMatchInterface, TestTypeMatchOnResolvingClass>((instance, c) =>
{
Assert.AreEqual(container, c);
return null;
});
container.Bind<ITypeMatchInterface, TestTypeMatchOnResolvingClass>();
Assert.AreEqual(null, container.Make<ITypeMatchInterface>());
}

public interface ITypeMatchInterface
{

Expand Down
24 changes: 23 additions & 1 deletion src/CatLib.Core/CatLib/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,28 @@ public static void Extend(string service, Func<object, object> closure)

/// <summary>
/// 扩展容器中的服务
/// <para>允许在服务构建的过程中配置或者替换服务</para>
/// <para>如果服务已经被构建,拓展会立即生效。</para>
/// </summary>
/// <param name="closure">闭包</param>
public static void Extend<TService, TConcrete>(Func<TConcrete, object> closure)
{
Handler.Extend<TService, TConcrete>(closure);
}

/// <summary>
/// 扩展容器中的服务
/// <para>允许在服务构建的过程中配置或者替换服务</para>
/// <para>如果服务已经被构建,拓展会立即生效。</para>
/// </summary>
/// <param name="closure">闭包</param>
public static void Extend<TService, TConcrete>(Func<TConcrete, IContainer, object> closure)
{
Handler.Extend<TService, TConcrete>(closure);
}

/// <summary>
/// 扩展容器中的全部服务
/// <para>如果构建的实例符合指定的类型或者接口,那么触发扩展闭包</para>
/// </summary>
/// <typeparam name="TConcrete">实现的类型或接口</typeparam>
Expand All @@ -800,7 +822,7 @@ public static void Extend<TConcrete>(Func<TConcrete, IContainer, object> closure
}

/// <summary>
/// 扩展容器中的服务
/// 扩展容器中的全部服务
/// <para>如果构建的实例符合指定的类型或者接口,那么触发扩展闭包</para>
/// </summary>
/// <typeparam name="TConcrete">实现的类型或接口</typeparam>
Expand Down
25 changes: 25 additions & 0 deletions src/CatLib.Core/Support/Container/ExtendContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,31 @@ public static void Extend(this IContainer container, string service, Func<object
container.Extend(service, (instance, c) => closure(instance));
}

/// <summary>
/// 扩展容器中的服务
/// <para>允许在服务构建的过程中配置或者替换服务</para>
/// <para>如果服务已经被构建,拓展会立即生效。</para>
/// </summary>
/// <param name="container">服务容器</param>
/// <param name="closure">闭包</param>
public static void Extend<TService, TConcrete>(this IContainer container, Func<TConcrete, object> closure)
{
container.Extend(container.Type2Service(typeof(TService)), (instance, c) => closure((TConcrete) instance));
}

/// <summary>
/// 扩展容器中的服务
/// <para>允许在服务构建的过程中配置或者替换服务</para>
/// <para>如果服务已经被构建,拓展会立即生效。</para>
/// </summary>
/// <param name="container">服务容器</param>
/// <param name="closure">闭包</param>
public static void Extend<TService, TConcrete>(this IContainer container, Func<TConcrete, IContainer, object> closure)
{
container.Extend(container.Type2Service(typeof(TService)),
(instance, c) => closure((TConcrete) instance, c));
}

/// <summary>
/// 扩展容器中的服务
/// <para>如果构建的实例符合指定的类型或者接口,那么触发扩展闭包</para>
Expand Down

0 comments on commit 2901d0f

Please sign in to comment.