Skip to content

Commit

Permalink
Merge pull request #25 from z4kn4fein/dev
Browse files Browse the repository at this point in the history
Merge dev to master
  • Loading branch information
z4kn4fein authored Jun 8, 2017
2 parents bc56e89 + 07e8862 commit b97129e
Show file tree
Hide file tree
Showing 27 changed files with 193 additions and 143 deletions.
11 changes: 5 additions & 6 deletions src/stashbox.tests/BuildExtensionManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Stashbox.Entity;
using Stashbox.Infrastructure.ContainerExtension;
using System;
using Stashbox.Infrastructure.Registration;

namespace Stashbox.Tests
{
Expand All @@ -20,7 +21,7 @@ public void BuildExtensionManagerTests_AddPostBuildExtension()
container.RegisterType<object>(context => context.WithFactory(() => obj));

post.Setup(p => p.PostBuild(obj, container.ContainerContext, It.IsAny<ResolutionInfo>(),
It.IsAny<Type>(), null)).Returns(obj).Verifiable();
It.IsAny<IServiceRegistration>(), It.IsAny<Type>())).Returns(obj).Verifiable();

var inst = container.Resolve(typeof(object));
post.Verify(p => p.Initialize(container.ContainerContext));
Expand All @@ -40,7 +41,7 @@ public void BuildExtensionManagerTests_AddPostBuildExtension_WithDefaultReg()

bool called = false;
post.Setup(p => p.PostBuild(It.IsAny<object>(), container.ContainerContext, It.IsAny<ResolutionInfo>(),
It.IsAny<Type>(), null)).Returns(It.IsAny<object>()).Callback(() => called = true).Verifiable();
It.IsAny<IServiceRegistration>(), It.IsAny<Type>())).Returns(It.IsAny<object>()).Callback(() => called = true).Verifiable();

var inst = container.Resolve<Test>();
Assert.IsTrue(called);
Expand All @@ -60,8 +61,7 @@ public void BuildExtensionManagerTests_AddRegistrationBuildExtension()
container.RegisterExtension(post.Object);
container.RegisterInstanceAs(new object());
post.Verify(p => p.Initialize(container.ContainerContext));
post.Verify(p => p.OnRegistration(container.ContainerContext,
It.IsAny<Type>(), It.IsAny<Type>(), null));
post.Verify(p => p.OnRegistration(container.ContainerContext, It.IsAny<IServiceRegistration>()));
}

post.Verify(p => p.CleanUp());
Expand All @@ -83,8 +83,7 @@ public void BuildExtensionManagerTests_CreateCopy()
child.RegisterInstanceAs(new object());

post2.Verify(p => p.Initialize(child.ContainerContext));
post2.Verify(p => p.OnRegistration(child.ContainerContext,
It.IsAny<Type>(), It.IsAny<Type>(), null));
post2.Verify(p => p.OnRegistration(child.ContainerContext, It.IsAny<IServiceRegistration>()));
}

post2.Verify(p => p.CleanUp());
Expand Down
27 changes: 25 additions & 2 deletions src/stashbox.tests/DisposeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,15 @@ public void DisposeTests_Scoped()

container.RegisterScoped<ITest2, Test2>();
container.RegisterScoped<Test3>();
container.RegisterScoped<ITest1, Test1>();
container.RegisterScoped<ITest1, Test1>("test");

test = container.Resolve<ITest1>();
test2 = container.Resolve<ITest2>();
test3 = container.Resolve<Test3>();

using (var child = container.BeginScope())
{
test4 = child.Resolve<ITest1>();
test4 = (ITest1)child.Resolve(typeof(ITest1), "test");
test5 = child.Resolve<ITest2>();
test6 = child.Resolve<Test3>();
}
Expand Down Expand Up @@ -232,6 +232,29 @@ public void DisposeTests_PutInScope_RootScope()
Assert.IsTrue(test.Disposed);
}

[TestMethod]
public void DisposeTests_PutInScope_RootScope_WithoutDispose()
{
var test = new Test1();
using (IStashboxContainer container = new StashboxContainer())
{
container.RegisterType<ITest2, Test2>();
container.RegisterType<Test3>();

container.PutInstanceInScope<ITest1>(test, true);

var test1 = container.Resolve<ITest1>();
var test2 = container.Resolve(typeof(ITest2));
var test3 = container.Resolve<Test3>();

Assert.AreSame(test, test1);
Assert.AreSame(test, ((ITest2)test2).Test1);
Assert.AreSame(test, test3.Test1);
}

Assert.IsFalse(test.Disposed);
}

[TestMethod]
public void DisposeTests_PutInScope_Scoped()
{
Expand Down
25 changes: 22 additions & 3 deletions src/stashbox.tests/GenericTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ public void GenericTests_Resolve_Constraint_Multiple()
{
using (var container = new StashboxContainer())
{
container.RegisterType(typeof(IConstraintTest<>), typeof(ConstraintTest<>));
container.RegisterType(typeof(IConstraintTest<>), typeof(ConstraintTest2<>));
container.RegisterType(typeof(IConstraintTest<>), typeof(ConstraintTest<>));

var inst = container.Resolve<IConstraintTest<ConstraintArgument>>();
Assert.IsInstanceOfType(inst, typeof(ConstraintTest<ConstraintArgument>));
Expand Down Expand Up @@ -189,16 +189,35 @@ public void GenericTests_Resolve_Constraint_Constructor()
}
}

public interface Constraint { }
[TestMethod]
public void GenericTests_Resolve_Constraint_Pick_RightImpl()
{
using (var container = new StashboxContainer())
{
container.RegisterType(typeof(IConstraintTest<>), typeof(ConstraintTest2<>));
container.RegisterType(typeof(IConstraintTest<>), typeof(ConstraintTest3<>));

var inst = container.Resolve<IConstraintTest<ConstraintArgument1>>();
Assert.IsInstanceOfType(inst, typeof(ConstraintTest3<ConstraintArgument1>));
}
}

public interface IConstraint { }

public interface IConstraint1 { }

public interface IConstraintTest<T> { }

public class ConstraintTest<T> : IConstraintTest<T> { }

public class ConstraintTest2<T> : IConstraintTest<T> where T : Constraint { }
public class ConstraintTest2<T> : IConstraintTest<T> where T : IConstraint { }

public class ConstraintTest3<T> : IConstraintTest<T> where T : IConstraint1 { }

public class ConstraintArgument { }

public class ConstraintArgument1 : IConstraint1 { }

public class ConstraintTest3
{
public IConstraintTest<ConstraintArgument> Test { get; set; }
Expand Down
16 changes: 15 additions & 1 deletion src/stashbox.tests/InjectionMemberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,21 @@ public void InjectionMemberTests_Resolve()
}

[TestMethod]
public void InjectionMemberTests_BuildUp()
public void InjectionMemberTests_Resolve_WithoutRegistered()
{
var container = new StashboxContainer();
var test1 = new Test1();
container.WireUpAs<ITest1>(test1);

var inst = container.Resolve<ITest1>();

Assert.IsNotNull(inst);
Assert.IsNull(inst.Test);
Assert.IsNull(((Test1)inst).TestFieldProperty);
}

[TestMethod]
public void InjectionMemberTests_WireUp()
{
using (var container = new StashboxContainer())
{
Expand Down
22 changes: 9 additions & 13 deletions src/stashbox.tests/LazyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void LazyTests_Resolve_Enumerable_Null()
{
var container = new StashboxContainer();
var inst = container.Resolve<Lazy<IEnumerable<ITest>>>();

Assert.AreEqual(0, inst.Value.Count());
}

Expand Down Expand Up @@ -101,7 +101,7 @@ public void LazyTests_Resolve_ConstructorDependency_Null()
[TestMethod]
public void LazyTests_Resolve_Circular()
{
var container = new StashboxContainer(config =>
var container = new StashboxContainer(config =>
config.WithCircularDependencyTracking()
.WithCircularDependencyWithLazy());

Expand All @@ -116,24 +116,22 @@ public void LazyTests_Resolve_Circular()
}

[TestMethod]
public void LazyTests_Resolve_Circular_FromCache()
public void LazyTests_Resolve_Circular_Evaluate()
{
var container = new StashboxContainer(config =>
config.WithCircularDependencyTracking()
.WithCircularDependencyWithLazy());

container.RegisterSingleton<Circular1>();
container.RegisterSingleton<Circular2>();
container.RegisterType<Circular1>();
container.RegisterType<Circular2>();

var inst1 = container.Resolve<Circular1>();
var inst2 = container.Resolve<Circular2>();

Assert.AreSame(container.Resolve<Circular2>(), inst1.Dep.Value);
Assert.AreSame(container.Resolve<Circular1>(), inst2.Dep.Value);
Assert.IsNotNull(inst1.Dep.Value.Dep.Value.Dep.Value.Dep);
}

[TestMethod]
public void LazyTests_Resolve_Circular_Evaluate()
public void LazyTests_Resolve_Circular_Evaluate_Singleton()
{
var container = new StashboxContainer(config =>
config.WithCircularDependencyTracking()
Expand All @@ -144,15 +142,13 @@ public void LazyTests_Resolve_Circular_Evaluate()

var inst1 = container.Resolve<Circular1>();

var val = inst1.Dep.Value.Dep.Value.Dep.Value.Dep;

Assert.IsNotNull(val);
Assert.IsNotNull(inst1.Dep.Value.Dep.Value.Dep.Value.Dep);
}

[TestMethod]
public void LazyTests_Resolve_IEnumerable_Circular()
{
var container = new StashboxContainer(config =>
var container = new StashboxContainer(config =>
config.WithCircularDependencyTracking()
.WithCircularDependencyWithLazy());

Expand Down
2 changes: 1 addition & 1 deletion src/stashbox.tests/StandardResolveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public void StandardResolveTests_Resolve_Parallel()
container.RegisterType<ITest1, Test1>(context => context.WithName(i.ToString()));
container.RegisterType<ITest3, Test3>(context => context.WithName($"ITest3{i.ToString()}"));
var test33 = container.Resolve<ITest3>($"ITest3{i.ToString()}");
var test11 = container.Resolve<ITest1>(i.ToString());
var test11 = container.Resolve(typeof(ITest1), i.ToString());
Assert.IsNotNull(test33);
Assert.IsNotNull(test11);
Expand Down
27 changes: 23 additions & 4 deletions src/stashbox.tests/WireUpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,26 @@ public void WireUp_Multiple()
}

[TestMethod]
public void InjectionMemberTests_WireUp()
public void WireUp_Multiple_Named()
{
using (var container = new StashboxContainer())
{
var test1 = new Test();
container.WireUpAs<ITest>(test1, "test1");

var test2 = new Test();
container.WireUpAs<ITest>(test2, "test2");

var inst = container.Resolve<ITest>("test1");
var inst2 = container.Resolve<ITest>("test2");

Assert.AreSame(test1, inst);
Assert.AreSame(test2, inst2);
}
}

[TestMethod]
public void WireUpTests_InjectionMember()
{
using (var container = new StashboxContainer())
{
Expand All @@ -49,7 +68,7 @@ public void InjectionMemberTests_WireUp()
}

[TestMethod]
public void InjectionMemberTests_WireUp_ServiceUpdated()
public void WireUpTests_InjectionMember_ServiceUpdated()
{
using (var container = new StashboxContainer())
{
Expand All @@ -73,7 +92,7 @@ public void InjectionMemberTests_WireUp_ServiceUpdated()
}

[TestMethod]
public void InjectionMemberTests_WireUp_WithoutService()
public void WireUpTests_InjectionMember_WithoutService()
{
using (var container = new StashboxContainer())
{
Expand All @@ -90,7 +109,7 @@ public void InjectionMemberTests_WireUp_WithoutService()
}

[TestMethod]
public void InjectionMemberTests_WireUp_WithoutService_NonGeneric()
public void WireUpTests_WithoutService_NonGeneric()
{
using (var container = new StashboxContainer())
{
Expand Down
12 changes: 7 additions & 5 deletions src/stashbox/BuildExtensionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using Stashbox.Utils;
using System;
using Stashbox.Infrastructure.Registration;

namespace Stashbox
{
Expand All @@ -24,22 +25,23 @@ public void AddExtension(IContainerExtension containerExtension)
{
if (containerExtension is IPostBuildExtension)
this.HasPostBuildExtensions = true;
else

if (containerExtension is IRegistrationExtension)
this.HasRegistrationExtensions = true;

this.repository.Add(containerExtension);
}

public object ExecutePostBuildExtensions(object instance, IContainerContext containerContext, ResolutionInfo resolutionInfo,
Type resolveType, InjectionParameter[] injectionParameters = null) =>
this.repository.OfType<IPostBuildExtension>().Aggregate(instance, (current, extension) => extension.PostBuild(current, containerContext, resolutionInfo, resolveType, injectionParameters));
IServiceRegistration serviceRegistration, Type requestedType) =>
this.repository.OfType<IPostBuildExtension>().Aggregate(instance, (current, extension) => extension.PostBuild(current, containerContext, resolutionInfo, serviceRegistration, requestedType));

public void ExecuteOnRegistrationExtensions(IContainerContext containerContext, Type typeTo, Type typeFrom, InjectionParameter[] injectionParameters = null)
public void ExecuteOnRegistrationExtensions(IContainerContext containerContext, IServiceRegistration serviceRegistration)
{
if (!this.HasRegistrationExtensions) return;

foreach (var extension in this.repository.OfType<IRegistrationExtension>())
extension.OnRegistration(containerContext, typeTo, typeFrom, injectionParameters);
extension.OnRegistration(containerContext, serviceRegistration);
}

public IContainerExtensionManager CreateCopy()
Expand Down
4 changes: 2 additions & 2 deletions src/stashbox/BuildUp/Expressions/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private ResolutionConstructor CreateResolutionConstructor(IServiceRegistration s
resolutionInfo, serviceRegistration.MetaInformation.GetTypeInformationForParameter(parameter),
serviceRegistration.RegistrationContext.InjectionParameters);

parameterExpressions[i] = expression ?? throw new ResolutionFailedException(serviceRegistration.ImplementationType,
parameterExpressions[i] = expression ?? throw new ResolutionFailedException(serviceRegistration.ImplementationType,
$"Constructor {constructor}, unresolvable parameter: ({parameter.ParameterType}){parameter.Name}");
}

Expand Down Expand Up @@ -171,7 +171,7 @@ private Expression CreatePostWorkExpressionIfAny(IServiceRegistration serviceReg
if (this.containerExtensionManager.HasPostBuildExtensions)
{
var call = Expression.Call(Expression.Constant(this.containerExtensionManager), Constants.BuildExtensionMethod, newVariable, Expression.Constant(this.containerContext),
Expression.Constant(resolutionInfo), Expression.Constant(serviceType), Expression.Constant(serviceRegistration.RegistrationContext.InjectionParameters, typeof(InjectionParameter[])));
Expression.Constant(resolutionInfo), Expression.Constant(serviceRegistration), Expression.Constant(serviceType));

block.Add(Expression.Assign(newVariable, Expression.Convert(call, serviceType)));
}
Expand Down
Loading

0 comments on commit b97129e

Please sign in to comment.