Skip to content

Commit 120c03d

Browse files
author
Peter Csajtai
committed
Fixed wrong PutInstanceInScope behavior
1 parent e69df38 commit 120c03d

18 files changed

+67
-39
lines changed

appveyor.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
artifact: /.*\.nupkg/
1818

1919
environment:
20-
build_version: 2.4.6
20+
build_version: 2.4.7
2121
COVERALLS_REPO_TOKEN:
2222
secure: Q9gcbZnzJ5a0YYF6mkr4QoQylIzeQmVytMnIe4WL7aPtb30SdNes7brLkvnXOirt
2323

@@ -79,7 +79,7 @@
7979
secure: 2bITagXOj2s3bTJaGXh8/iyWtST8OQOFaMM+0GAKgZts9OjCVCiV7C+E/0SYsM6M
8080

8181
environment:
82-
build_version: 2.4.6
82+
build_version: 2.4.7
8383
COVERALLS_REPO_TOKEN:
8484
secure: Q9gcbZnzJ5a0YYF6mkr4QoQylIzeQmVytMnIe4WL7aPtb30SdNes7brLkvnXOirt
8585

src/stashbox.tests/ContainerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public class TestResolver : Resolver
188188
{
189189
public override bool SupportsMany => true;
190190

191-
public override bool CanUseForResolution(IContainerContext containerContext, TypeInformation typeInfo)
191+
public override bool CanUseForResolution(IContainerContext containerContext, TypeInformation typeInfo, ResolutionInfo resolutionInfo)
192192
{
193193
return typeInfo.Type == typeof(ITest1);
194194
}
@@ -203,7 +203,7 @@ public class TestResolver2 : Resolver
203203
{
204204
public override bool SupportsMany => true;
205205

206-
public override bool CanUseForResolution(IContainerContext containerContext, TypeInformation typeInfo)
206+
public override bool CanUseForResolution(IContainerContext containerContext, TypeInformation typeInfo, ResolutionInfo resolutionInfo)
207207
{
208208
return typeInfo.Type == typeof(ITest1);
209209
}

src/stashbox.tests/DisposeTests.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ public void DisposeTests_PutInScope_Scoped()
237237
{
238238
using (IStashboxContainer container = new StashboxContainer())
239239
{
240-
container.RegisterType<ITest2, Test2>();
241-
container.RegisterType<Test3>();
240+
container.RegisterScoped<ITest2, Test2>();
241+
container.RegisterScoped<Test3>();
242242

243243
var test = new Test1();
244244

@@ -256,6 +256,27 @@ public void DisposeTests_PutInScope_Scoped()
256256
}
257257

258258
Assert.IsTrue(test.Disposed);
259+
260+
var test4 = new Test1();
261+
262+
using (var child = container.BeginScope())
263+
{
264+
child.PutInstanceInScope<ITest1>(test4);
265+
266+
var test1 = child.Resolve<ITest1>();
267+
var test2 = child.Resolve<ITest2>();
268+
var test3 = child.Resolve<Test3>();
269+
270+
Assert.AreSame(test4, test1);
271+
Assert.AreSame(test4, test2.Test1);
272+
Assert.AreSame(test4, test3.Test1);
273+
274+
Assert.AreNotSame(test, test1);
275+
Assert.AreNotSame(test, test2.Test1);
276+
Assert.AreNotSame(test, test3.Test1);
277+
}
278+
279+
Assert.IsTrue(test4.Disposed);
259280
}
260281
}
261282

src/stashbox/BuildUp/Resolution/DefaultValueResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public override Expression GetExpression(IContainerContext containerContext, Typ
2323
return null;
2424
}
2525

26-
public override bool CanUseForResolution(IContainerContext containerContext, TypeInformation typeInfo) =>
26+
public override bool CanUseForResolution(IContainerContext containerContext, TypeInformation typeInfo, ResolutionInfo resolutionInfo) =>
2727
containerContext.ContainerConfigurator.ContainerConfiguration.OptionalAndDefaultValueInjectionEnabled &&
2828
(typeInfo.HasDefaultValue || typeInfo.Type.GetTypeInfo().IsValueType || typeInfo.Type == typeof(string) || typeInfo.IsMember);
2929
}

src/stashbox/BuildUp/Resolution/EnumerableResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public override Expression GetExpression(IContainerContext containerContext, Typ
1717
Expression.NewArrayInit(enumerableType.Type, expressions);
1818
}
1919

20-
public override bool CanUseForResolution(IContainerContext containerContext, TypeInformation typeInfo) =>
20+
public override bool CanUseForResolution(IContainerContext containerContext, TypeInformation typeInfo, ResolutionInfo resolutionInfo) =>
2121
typeInfo.Type.GetEnumerableType() != null;
2222
}
2323
}

src/stashbox/BuildUp/Resolution/FuncResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public override Expression[] GetExpressions(IContainerContext containerContext,
5656
return funcExpressions;
5757
}
5858

59-
public override bool CanUseForResolution(IContainerContext containerContext, TypeInformation typeInfo) =>
59+
public override bool CanUseForResolution(IContainerContext containerContext, TypeInformation typeInfo, ResolutionInfo resolutionInfo) =>
6060
typeInfo.Type.IsClosedGenericType() && this.supportedTypes.Contains(typeInfo.Type.GetGenericTypeDefinition());
6161

6262
private ParameterExpression[] PrepareExtraParameters(Type wrappedType, ResolutionInfo resolutionInfo, Type[] args)

src/stashbox/BuildUp/Resolution/LazyResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private Expression CreateLazyExpressionCall(IServiceRegistration serviceRegistra
8686
return Expression.New(constructor, Expression.Lambda(convert));
8787
}
8888

89-
public override bool CanUseForResolution(IContainerContext containerContext, TypeInformation typeInfo) =>
89+
public override bool CanUseForResolution(IContainerContext containerContext, TypeInformation typeInfo, ResolutionInfo resolutionInfo) =>
9090
typeInfo.Type.IsClosedGenericType() && typeInfo.Type.GetGenericTypeDefinition() == typeof(Lazy<>);
9191

9292
private static readonly MethodInfo DelegateCacheMethod = typeof(DelegateCache).GetSingleMethod("CreateLazyDelegate");

src/stashbox/BuildUp/Resolution/ParentContainerResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Stashbox.BuildUp.Resolution
77
{
88
internal class ParentContainerResolver : Resolver
99
{
10-
public override bool CanUseForResolution(IContainerContext containerContext, TypeInformation typeInfo) =>
10+
public override bool CanUseForResolution(IContainerContext containerContext, TypeInformation typeInfo, ResolutionInfo resolutionInfo) =>
1111
containerContext.Container.ParentContainer != null && containerContext.Container.ParentContainer.CanResolve(typeInfo.Type, typeInfo.DependencyName);
1212

1313
public override Expression GetExpression(IContainerContext containerContext, TypeInformation typeInfo, ResolutionInfo resolutionInfo) =>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Linq.Expressions;
2+
using Stashbox.Entity;
3+
using Stashbox.Infrastructure;
4+
using Stashbox.Infrastructure.Resolution;
5+
6+
namespace Stashbox.BuildUp.Resolution
7+
{
8+
internal class ScopedInstanceResolver : Resolver
9+
{
10+
public override Expression GetExpression(IContainerContext containerContext, TypeInformation typeInfo, ResolutionInfo resolutionInfo) =>
11+
Expression.Convert(Expression.Call(Constants.ScopeExpression, Constants.GetScopedInstanceMethod, Expression.Constant(typeInfo.Type)), typeInfo.Type);
12+
13+
public override bool CanUseForResolution(IContainerContext containerContext, TypeInformation typeInfo, ResolutionInfo resolutionInfo) =>
14+
resolutionInfo.ResolutionScope.HasScopedInstances && resolutionInfo.ResolutionScope.GetScopedInstanceOrDefault(typeInfo.Type) != null;
15+
}
16+
}

src/stashbox/BuildUp/Resolution/TupleResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ internal class TupleResolver : Resolver
2121
typeof(Tuple<,,,,,,,>)
2222
};
2323

24-
public override bool CanUseForResolution(IContainerContext containerContext, TypeInformation typeInfo) =>
24+
public override bool CanUseForResolution(IContainerContext containerContext, TypeInformation typeInfo, ResolutionInfo resolutionInfo) =>
2525
typeInfo.Type.IsClosedGenericType() && this.supportedTypes.Contains(typeInfo.Type.GetGenericTypeDefinition());
2626

2727
public override Expression GetExpression(IContainerContext containerContext, TypeInformation typeInfo, ResolutionInfo resolutionInfo)

0 commit comments

Comments
 (0)