Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Commit

Permalink
Revert "Fix mixed open and close generic service IEnumerable resoluti…
Browse files Browse the repository at this point in the history
…on (#530)" (#532)

This reverts commit ca88b00.
  • Loading branch information
pakrym authored Jun 6, 2017
1 parent ca88b00 commit 6104141
Show file tree
Hide file tree
Showing 34 changed files with 934 additions and 735 deletions.
2 changes: 1 addition & 1 deletion DependencyInjection.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26510.0
VisualStudioVersion = 15.0.26228.9
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{D26F6F80-63EE-4081-A814-EF3DBABE24D9}"
EndProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -680,31 +680,5 @@ public void DisposesInReverseOrderOfCreation()
Assert.Equal(outer.MultipleServices.Reverse(), callback.Disposed.Skip(1).Take(3).OfType<IFakeMultipleService>());
Assert.Equal(outer.SingleService, callback.Disposed[4]);
}

[Fact]
public void ResolvesMixedOpenClosedGenericsAsEnumerable()
{
// Arrange
var serviceCollection = new TestServiceCollection();
var instance = new FakeOpenGenericService<PocoClass>(null);

serviceCollection.AddTransient<PocoClass, PocoClass>();
serviceCollection.AddSingleton(typeof(IFakeOpenGenericService<PocoClass>), typeof(FakeService));
serviceCollection.AddSingleton(typeof(IFakeOpenGenericService<>), typeof(FakeOpenGenericService<>));
serviceCollection.AddSingleton<IFakeOpenGenericService<PocoClass>>(instance);

var serviceProvider = CreateServiceProvider(serviceCollection);

var enumerable = serviceProvider.GetService<IEnumerable<IFakeOpenGenericService<PocoClass>>>().ToArray();

// Assert
Assert.Equal(3, enumerable.Length);
Assert.NotNull(enumerable[0]);
Assert.NotNull(enumerable[1]);
Assert.NotNull(enumerable[2]);

Assert.Equal(instance, enumerable[2]);
Assert.IsType<FakeService>(enumerable[0]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,28 +98,42 @@ protected override Expression VisitConstant(ConstantCallSite constantCallSite, P

protected override Expression VisitCreateInstance(CreateInstanceCallSite createInstanceCallSite, ParameterExpression provider)
{
return Expression.New(createInstanceCallSite.ImplementationType);
return Expression.New(createInstanceCallSite.Descriptor.ImplementationType);
}

protected override Expression VisitServiceProvider(ServiceProviderCallSite serviceProviderCallSite, ParameterExpression provider)
protected override Expression VisitInstanceService(InstanceService instanceCallSite, ParameterExpression provider)
{
return Expression.Constant(
instanceCallSite.Descriptor.ImplementationInstance,
instanceCallSite.Descriptor.ServiceType);
}

protected override Expression VisitServiceProviderService(ServiceProviderService serviceProviderService, ParameterExpression provider)
{
return provider;
}

protected override Expression VisitServiceScopeFactory(ServiceScopeFactoryCallSite serviceScopeFactoryCallSite, ParameterExpression provider)
protected override Expression VisitEmptyIEnumerable(EmptyIEnumerableCallSite emptyIEnumerableCallSite, ParameterExpression provider)
{
return Expression.Constant(
emptyIEnumerableCallSite.ServiceInstance,
emptyIEnumerableCallSite.ServiceType);
}

protected override Expression VisitServiceScopeService(ServiceScopeService serviceScopeService, ParameterExpression provider)
{
return Expression.New(typeof(ServiceScopeFactory).GetTypeInfo()
.DeclaredConstructors
.Single(),
provider);
}

protected override Expression VisitFactory(FactoryCallSite factoryCallSite, ParameterExpression provider)
protected override Expression VisitFactoryService(FactoryService factoryService, ParameterExpression provider)
{
return Expression.Invoke(Expression.Constant(factoryCallSite.Factory), provider);
return Expression.Invoke(Expression.Constant(factoryService.Descriptor.ImplementationFactory), provider);
}

protected override Expression VisitIEnumerable(IEnumerableCallSite callSite, ParameterExpression provider)
protected override Expression VisitClosedIEnumerable(ClosedIEnumerableCallSite callSite, ParameterExpression provider)
{
return Expression.NewArrayInit(
callSite.ItemType,
Expand All @@ -131,7 +145,7 @@ protected override Expression VisitIEnumerable(IEnumerableCallSite callSite, Par

protected override Expression VisitTransient(TransientCallSite callSite, ParameterExpression provider)
{
var implType = callSite.ServiceCallSite.ImplementationType;
var implType = callSite.Service.ImplementationType;
// Elide calls to GetCaptureDisposable if the implemenation type isn't disposable
return TryCaptureDisposible(
implType,
Expand Down Expand Up @@ -175,7 +189,7 @@ private static Expression Convert(Expression expression, Type type)
protected override Expression VisitScoped(ScopedCallSite callSite, ParameterExpression provider)
{
var keyExpression = Expression.Constant(
callSite,
callSite.Key,
typeof(object));

var resolvedVariable = Expression.Variable(typeof(object), "resolved");
Expand All @@ -189,10 +203,10 @@ protected override Expression VisitScoped(ScopedCallSite callSite, ParameterExpr
resolvedVariable);

var service = VisitCallSite(callSite.ServiceCallSite, provider);
var captureDisposible = TryCaptureDisposible(callSite.ImplementationType, provider, service);
var captureDisposible = TryCaptureDisposible(callSite.Key.ImplementationType, provider, service);

var assignExpression = Expression.Assign(
resolvedVariable,
resolvedVariable,
captureDisposible);

var addValueExpression = Expression.Call(
Expand Down
Loading

0 comments on commit 6104141

Please sign in to comment.