Skip to content

Commit

Permalink
Added tuple resolver, more refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
z4kn4fein committed Feb 21, 2017
1 parent f2c4918 commit b65d81c
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 3 deletions.
2 changes: 0 additions & 2 deletions src/stashbox.tests/ContainerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
using Stashbox.Entity;
using Stashbox.Exceptions;
using Stashbox.Infrastructure;
using System;
using System.Linq.Expressions;
using Stashbox.Infrastructure.Resolution;
using Stashbox.Entity.Resolution;

namespace Stashbox.Tests
{
Expand Down
14 changes: 14 additions & 0 deletions src/stashbox.tests/FuncTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Stashbox.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Stashbox.Tests
{
Expand Down Expand Up @@ -31,6 +33,18 @@ public void FuncTests_Resolve_Lazy()
Assert.IsInstanceOfType(inst().Value, typeof(Test));
}

[TestMethod]
public void FuncTests_Resolve_Enumerable()
{
var container = new StashboxContainer();
container.RegisterType<ITest, Test>();
var inst = container.Resolve<Func<IEnumerable<ITest>>>();

Assert.IsNotNull(inst);
Assert.IsInstanceOfType(inst, typeof(Func<IEnumerable<ITest>>));
Assert.IsInstanceOfType(inst().First(), typeof(Test));
}

[TestMethod]
public void FuncTests_Resolve_ConstructorDependency()
{
Expand Down
26 changes: 26 additions & 0 deletions src/stashbox.tests/LazyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,32 @@ public void LazyTests_Resolve()
Assert.IsInstanceOfType(inst.Value, typeof(Test));
}

[TestMethod]
public void LazyTests_Resolve_Func()
{
var container = new StashboxContainer();
container.RegisterType<ITest, Test>();
var inst = container.Resolve<Lazy<Func<ITest>>>();

Assert.IsNotNull(inst);
Assert.IsFalse(inst.IsValueCreated);
Assert.IsInstanceOfType(inst, typeof(Lazy<Func<ITest>>));
Assert.IsInstanceOfType(inst.Value(), typeof(Test));
}

[TestMethod]
public void LazyTests_Resolve_Enumerable()
{
var container = new StashboxContainer();
container.RegisterType<ITest, Test>();
var inst = container.Resolve<Lazy<IEnumerable<ITest>>>();

Assert.IsNotNull(inst);
Assert.IsFalse(inst.IsValueCreated);
Assert.IsInstanceOfType(inst, typeof(Lazy<IEnumerable<ITest>>));
Assert.IsInstanceOfType(inst.Value.First(), typeof(Test));
}

[TestMethod]
public void LazyTests_Resolve_ConstructorDependency()
{
Expand Down
106 changes: 106 additions & 0 deletions src/stashbox.tests/TupleTests.cs
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;
}
}
}
}
6 changes: 5 additions & 1 deletion src/stashbox/BuildUp/Resolution/FuncResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ internal class FuncResolver : Resolver
typeof(Func<>),
typeof(Func<,>),
typeof(Func<,,>),
typeof(Func<,,,>)
typeof(Func<,,,>),
typeof(Func<,,,,>),
typeof(Func<,,,,,>),
typeof(Func<,,,,,,>),
typeof(Func<,,,,,,,>)
};

public override bool SupportsMany => true;
Expand Down
43 changes: 43 additions & 0 deletions src/stashbox/BuildUp/Resolution/TupleResolver.cs
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);
}
}
}
1 change: 1 addition & 0 deletions src/stashbox/StashboxContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ private void RegisterResolvers()
this.resolverSelector.AddResolver(new EnumerableResolver());
this.resolverSelector.AddResolver(new LazyResolver(this.resolverSelector));
this.resolverSelector.AddResolver(new FuncResolver());
this.resolverSelector.AddResolver(new TupleResolver());
this.resolverSelector.AddResolver(new DefaultValueResolver());
this.resolverSelector.AddResolver(new UnknownTypeResolver());
this.resolverSelector.AddResolver(new ParentContainerResolver());
Expand Down

0 comments on commit b65d81c

Please sign in to comment.