Skip to content

Commit

Permalink
resolved #122
Browse files Browse the repository at this point in the history
  • Loading branch information
ymh199478 committed Dec 18, 2018
1 parent d17f249 commit 276a3d4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
27 changes: 27 additions & 0 deletions src/CatLib.Core.Tests/Support/Container/ContainerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2688,6 +2688,33 @@ public void TestOnAfterResolvingLocal()
Assert.AreEqual("world", container["hello"]);
Assert.AreEqual(30, val);
}

public class TestNeedGivenWithParamNameClass
{
public int MyParam { get; set; }

public TestNeedGivenWithParamNameClass(int myParam)
{
MyParam = myParam;
}
}

[TestMethod]
public void TestNeedGivenWithParamName()
{
var container = new Container();
container.Bind<TestNeedGivenWithParamNameClass>()
.Needs("$myParam").Given(() => 100);

Assert.AreEqual(100, container.Make<TestNeedGivenWithParamNameClass>().MyParam);

container = new Container();
container.Bind<TestNeedGivenWithParamNameClass>()
.Needs("$myParam").Given<int>();
container.Bind<int>(() => 200);

Assert.AreEqual(200, container.Make<TestNeedGivenWithParamNameClass>().MyParam);
}
#endregion

/// <summary>
Expand Down
47 changes: 39 additions & 8 deletions src/CatLib.Core/Support/Container/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,37 @@ protected virtual string GetParamNeedsService(ParameterInfo baseParam)
return needService;
}

/// <summary>
/// 根据上下文获取相关的构建闭包
/// </summary>
/// <param name="makeServiceBindData">请求注入操作的服务绑定数据</param>
/// <param name="service">服务名</param>
/// <param name="paramName">目标参数的名字</param>
/// <returns>构建闭包</returns>
protected virtual Func<object> GetContextualClosure(Bindable makeServiceBindData, string service,
string paramName)
{
return makeServiceBindData.GetContextualClosure(service) ??
makeServiceBindData.GetContextualClosure($"${paramName}");
}

/// <summary>
/// 从上下文闭包中进行构建获得实例
/// </summary>
/// <param name="closure">上下文闭包</param>
/// <param name="needType">参数需求的类型</param>
/// <returns>构建的实例</returns>
protected virtual object MakeFromContextualClosure(Func<object> closure, Type needType)
{
var instance = closure();
if (ChangeType(ref instance, needType))
{
return instance;
}

throw new LogicException("Parameter type cannot be converted to : " + needType);
}

/// <summary>
/// 解决基本类型
/// </summary>
Expand All @@ -1159,10 +1190,10 @@ protected virtual string GetParamNeedsService(ParameterInfo baseParam)
/// <returns>解决结果</returns>
protected virtual object ResolveAttrPrimitive(Bindable makeServiceBindData, string service, PropertyInfo baseParam)
{
var contextualClosure = makeServiceBindData.GetContextualClosure(service);
var contextualClosure = GetContextualClosure(makeServiceBindData, service, baseParam.Name);
if (contextualClosure != null)
{
return contextualClosure();
return MakeFromContextualClosure(contextualClosure, baseParam.PropertyType);
}

service = makeServiceBindData.GetContextual(service);
Expand All @@ -1189,10 +1220,10 @@ protected virtual object ResolveAttrPrimitive(Bindable makeServiceBindData, stri
/// <returns>解决结果</returns>
protected virtual object ResloveAttrClass(Bindable makeServiceBindData, string service, PropertyInfo baseParam)
{
var contextualClosure = makeServiceBindData.GetContextualClosure(service);
var contextualClosure = GetContextualClosure(makeServiceBindData, service, baseParam.Name);
if (contextualClosure != null)
{
return contextualClosure();
return MakeFromContextualClosure(contextualClosure, baseParam.PropertyType);
}

try
Expand Down Expand Up @@ -1220,10 +1251,10 @@ protected virtual object ResloveAttrClass(Bindable makeServiceBindData, string s
/// <returns>解决结果</returns>
protected virtual object ResolvePrimitive(Bindable makeServiceBindData, string service, ParameterInfo baseParam)
{
var contextualClosure = makeServiceBindData.GetContextualClosure(service);
var contextualClosure = GetContextualClosure(makeServiceBindData, service, baseParam.Name);
if (contextualClosure != null)
{
return contextualClosure();
return MakeFromContextualClosure(contextualClosure, baseParam.ParameterType);
}

service = makeServiceBindData.GetContextual(service);
Expand Down Expand Up @@ -1255,10 +1286,10 @@ protected virtual object ResolvePrimitive(Bindable makeServiceBindData, string s
/// <returns>解决结果</returns>
protected virtual object ResloveClass(Bindable makeServiceBindData, string service, ParameterInfo baseParam)
{
var contextualClosure = makeServiceBindData.GetContextualClosure(service);
var contextualClosure = GetContextualClosure(makeServiceBindData, service, baseParam.Name);
if (contextualClosure != null)
{
return contextualClosure();
return MakeFromContextualClosure(contextualClosure, baseParam.ParameterType);
}

try
Expand Down

0 comments on commit 276a3d4

Please sign in to comment.