-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
195 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Stashbox.Tests | ||
{ | ||
[TestClass] | ||
public class TupleTests | ||
{ | ||
[TestMethod] | ||
public void TupleTests_Resolve() | ||
{ | ||
var container = new StashboxContainer(); | ||
container.RegisterType<ITest, Test>(); | ||
container.RegisterType<ITest1, Test1>(); | ||
var inst = container.Resolve<Tuple<ITest, ITest1>>(); | ||
|
||
Assert.IsNotNull(inst); | ||
Assert.IsInstanceOfType(inst, typeof(Tuple<ITest, ITest1>)); | ||
Assert.IsInstanceOfType(inst.Item1, typeof(Test)); | ||
Assert.IsInstanceOfType(inst.Item2, typeof(Test1)); | ||
} | ||
|
||
[TestMethod] | ||
public void TupleTests_Resolve_Lazy() | ||
{ | ||
var container = new StashboxContainer(); | ||
container.RegisterType<ITest, Test>(); | ||
container.RegisterType<ITest1, Test1>(); | ||
var inst = container.Resolve<Tuple<ITest, Lazy<ITest1>>>(); | ||
|
||
Assert.IsNotNull(inst); | ||
Assert.IsInstanceOfType(inst, typeof(Tuple<ITest, Lazy<ITest1>>)); | ||
Assert.IsInstanceOfType(inst.Item1, typeof(Test)); | ||
Assert.IsInstanceOfType(inst.Item2.Value, typeof(Test1)); | ||
} | ||
|
||
[TestMethod] | ||
public void TupleTests_Resolve_Func() | ||
{ | ||
var container = new StashboxContainer(); | ||
container.RegisterType<ITest, Test>(); | ||
container.RegisterType<ITest1, Test1>(); | ||
var inst = container.Resolve<Tuple<ITest, Func<ITest1>>>(); | ||
|
||
Assert.IsNotNull(inst); | ||
Assert.IsInstanceOfType(inst, typeof(Tuple<ITest, Func<ITest1>>)); | ||
Assert.IsInstanceOfType(inst.Item1, typeof(Test)); | ||
Assert.IsInstanceOfType(inst.Item2(), typeof(Test1)); | ||
} | ||
|
||
[TestMethod] | ||
public void TupleTests_Resolve_Enumerable() | ||
{ | ||
var container = new StashboxContainer(); | ||
container.RegisterType<ITest, Test>(); | ||
container.RegisterType<ITest1, Test1>(); | ||
var inst = container.Resolve<Tuple<IEnumerable<ITest>, Func<ITest1>>>(); | ||
|
||
Assert.IsNotNull(inst); | ||
Assert.IsInstanceOfType(inst, typeof(Tuple<IEnumerable<ITest>, Func<ITest1>>)); | ||
Assert.IsInstanceOfType(inst.Item1.First(), typeof(Test)); | ||
Assert.IsInstanceOfType(inst.Item2(), typeof(Test1)); | ||
} | ||
|
||
[TestMethod] | ||
public void TupleTests_Resolve_Constructor() | ||
{ | ||
var container = new StashboxContainer(); | ||
container.RegisterType<ITest, Test>(); | ||
container.RegisterType<ITest1, Test1>(); | ||
container.RegisterType<Test2>(); | ||
var inst = container.Resolve<Test2>(); | ||
|
||
Assert.IsNotNull(inst); | ||
Assert.IsInstanceOfType(inst.Test, typeof(Tuple<ITest, ITest1>)); | ||
Assert.IsInstanceOfType(inst.Test.Item1, typeof(Test)); | ||
Assert.IsInstanceOfType(inst.Test.Item2, typeof(Test1)); | ||
} | ||
|
||
public interface ITest | ||
{ } | ||
|
||
public interface ITest1 | ||
{ } | ||
|
||
public class Test : ITest | ||
{ } | ||
|
||
public class Test1 : ITest1 | ||
{ } | ||
|
||
public class Test2 | ||
{ | ||
public Tuple<ITest, ITest1> Test { get; } | ||
|
||
public Test2(Tuple<ITest, ITest1> test) | ||
{ | ||
this.Test = test; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Stashbox.Infrastructure.Resolution; | ||
using System; | ||
using Stashbox.Entity; | ||
using Stashbox.Infrastructure; | ||
using System.Linq.Expressions; | ||
using System.Collections.Generic; | ||
using Stashbox.Exceptions; | ||
|
||
namespace Stashbox.BuildUp.Resolution | ||
{ | ||
internal class TupleResolver : Resolver | ||
{ | ||
private readonly ISet<Type> supportedTypes = new HashSet<Type> | ||
{ | ||
typeof(Tuple<>), | ||
typeof(Tuple<,>), | ||
typeof(Tuple<,,>), | ||
typeof(Tuple<,,,>), | ||
typeof(Tuple<,,,,>), | ||
typeof(Tuple<,,,,,>), | ||
typeof(Tuple<,,,,,,>), | ||
typeof(Tuple<,,,,,,,>) | ||
}; | ||
|
||
public override bool CanUseForResolution(IContainerContext containerContext, TypeInformation typeInfo) => | ||
typeInfo.Type.IsConstructedGenericType && this.supportedTypes.Contains(typeInfo.Type.GetGenericTypeDefinition()); | ||
|
||
public override Expression GetExpression(IContainerContext containerContext, TypeInformation typeInfo, ResolutionInfo resolutionInfo) | ||
{ | ||
var tupleConstructor = typeInfo.Type.GetConstructor(typeInfo.Type.GenericTypeArguments); | ||
var length = typeInfo.Type.GenericTypeArguments.Length; | ||
var expressions = new Expression[length]; | ||
for (int i = 0; i < length; i++) | ||
{ | ||
var argumentInfo = new TypeInformation { Type = typeInfo.Type.GenericTypeArguments[i] }; | ||
var expr = containerContext.ResolutionStrategy.BuildResolutionExpression(containerContext, resolutionInfo, argumentInfo, null); | ||
expressions[i] = expr ?? throw new ResolutionFailedException(typeInfo.Type.FullName); | ||
} | ||
|
||
return Expression.New(tupleConstructor, expressions); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters