Skip to content

Commit 929c13a

Browse files
committed
resolved #126
1 parent 0bedb46 commit 929c13a

File tree

3 files changed

+9
-39
lines changed

3 files changed

+9
-39
lines changed

src/CatLib.Core.Tests/Support/Container/ContainerTests.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -789,10 +789,10 @@ public class MakeTestClass
789789
[Inject]
790790
public MakeTestClassDependency DependencyRequired { get; set; }
791791

792-
[Inject("AliasName")]
792+
[Inject]
793793
public MakeTestClassDependency2 DependencyAlias { get; set; }
794794

795-
[Inject("AliasNameRequired")]
795+
[Inject]
796796
public MakeTestClassDependency DependencyAliasRequired { get; set; }
797797

798798
public MakeTestClass(MakeTestClassDependency dependency)
@@ -950,7 +950,7 @@ public void CanMake()
950950
public void CheckIllegalMakeTypeIsNotSame()
951951
{
952952
var container = MakeContainer();
953-
container.Singleton<MakeTestClass>();
953+
container.Singleton<MakeTestClass>().Needs("$DependencyAlias").Given("AliasName");
954954
container.Singleton<MakeTestClassDependency2>().Alias("AliasNameRequired");
955955
container.Singleton<MakeTestClassDependency>().Alias("AliasName");
956956

@@ -1053,8 +1053,7 @@ public void CheckIllegalMake()
10531053
public class TestMakeParamInjectAttrClass
10541054
{
10551055
private IMsg msg;
1056-
public TestMakeParamInjectAttrClass(
1057-
[Inject("AliasName")]IMsg msg)
1056+
public TestMakeParamInjectAttrClass(IMsg msg)
10581057
{
10591058
this.msg = msg;
10601059
}
@@ -1078,7 +1077,7 @@ public void CanParamUseInjectAttr()
10781077

10791078
bind.Needs<IMsg>().Given<MakeTestClassDependency>();
10801079
var cls = container.Make<TestMakeParamInjectAttrClass>();
1081-
Assert.AreEqual("world", cls.GetMsg());
1080+
Assert.AreEqual("hello", cls.GetMsg());
10821081

10831082
bind.Needs("AliasName").Given<MakeTestClassDependency>();
10841083
cls = container.Make<TestMakeParamInjectAttrClass>();
@@ -1496,7 +1495,7 @@ public void ComplexContextualRelationshipTest2()
14961495

14971496
class ComplexClassAlias
14981497
{
1499-
[Inject("IComplexInterface.alias")]
1498+
[Inject]
15001499
public IComplexInterface Msg { get; set; }
15011500
}
15021501

@@ -1507,7 +1506,7 @@ class ComplexClassAlias
15071506
public void ComplexContextualRelationshipTest3()
15081507
{
15091508
var container = MakeContainer();
1510-
container.Bind<ComplexClassAlias>();
1509+
container.Bind<ComplexClassAlias>().Needs("$Msg").Given("IComplexInterface.alias");
15111510
container.Bind<ComplexInjectClass1>().Alias<IComplexInterface>();
15121511
container.Bind<ComplexInjectClass2>().Alias("IComplexInterface.alias");
15131512

src/CatLib.Core/Support/Container/Container.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,10 +1123,7 @@ protected virtual bool ChangeType(ref object result, Type conversionType)
11231123
/// <returns>需求的服务名</returns>
11241124
protected virtual string GetPropertyNeedsService(PropertyInfo property)
11251125
{
1126-
var injectAttr = (InjectAttribute)property.GetCustomAttributes(injectTarget, false)[0];
1127-
return string.IsNullOrEmpty(injectAttr.Alias)
1128-
? Type2Service(property.PropertyType)
1129-
: injectAttr.Alias;
1126+
return Type2Service(property.PropertyType);
11301127
}
11311128

11321129
/// <summary>
@@ -1136,18 +1133,7 @@ protected virtual string GetPropertyNeedsService(PropertyInfo property)
11361133
/// <returns>需求的服务名</returns>
11371134
protected virtual string GetParamNeedsService(ParameterInfo baseParam)
11381135
{
1139-
var needService = Type2Service(baseParam.ParameterType);
1140-
if (!baseParam.IsDefined(injectTarget, false))
1141-
{
1142-
return needService;
1143-
}
1144-
1145-
var injectAttr = (InjectAttribute)baseParam.GetCustomAttributes(injectTarget, false)[0];
1146-
if (!string.IsNullOrEmpty(injectAttr.Alias))
1147-
{
1148-
needService = injectAttr.Alias;
1149-
}
1150-
return needService;
1136+
return Type2Service(baseParam.ParameterType);
11511137
}
11521138

11531139
/// <summary>

src/CatLib.Core/Support/Container/InjectAttribute.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,9 @@ namespace CatLib
1717
/// 注入标记
1818
/// </summary>
1919
[AttributeUsage(AttributeTargets.Interface |
20-
AttributeTargets.Parameter |
2120
AttributeTargets.Property)]
2221
public class InjectAttribute : Attribute
2322
{
24-
/// <summary>
25-
/// 注入服务的别名或者服务名
26-
/// </summary>
27-
public string Alias { get; }
28-
29-
/// <summary>
30-
/// 声明注入
31-
/// </summary>
32-
/// <param name="alias">依赖服务的别名或者服务名</param>
33-
public InjectAttribute(string alias)
34-
{
35-
Alias = alias;
36-
}
37-
3823
/// <summary>
3924
/// 声明注入
4025
/// </summary>

0 commit comments

Comments
 (0)