Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- Fix issues #148 #117 #155

Merged
merged 2 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Scrutor/ServiceCollectionExtensions.Decoration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,16 @@ private static object GetInstance(this IServiceProvider provider, ServiceDescrip
return descriptor.ImplementationInstance;
}

if (descriptor.ImplementationType != null)
// Not suppose to be abstract.
Type implementationType = descriptor.ImplementationType;
if (implementationType != null)
{
return provider.GetServiceOrCreateInstance(descriptor.ImplementationType);
if (implementationType != descriptor.ServiceType)
return provider.GetServiceOrCreateInstance(implementationType);

// Since implementationType is equal to ServiceType we need explicitly create an implementation type through reflections in order to avoid infinite recursion.
// Should not cause issue with singletons, since singleton will be a decorator and after this fact we can don't care about lifecycle of decorable service (for sure, if IDisposable of decorator disposes underlying type:))
return provider.CreateInstance(implementationType);
}

return descriptor.ImplementationFactory(provider);
Expand Down
39 changes: 39 additions & 0 deletions test/Scrutor.Tests/DecorationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,47 @@ public void DecoratingNonRegisteredServiceThrows()
Assert.Throws<MissingTypeRegistrationException>(() => ConfigureProvider(services => services.Decorate<IDecoratedService, Decorator>()));
}

[Fact]
public void Issue148_Decorate_IsAbleToDecorateConcreateTypes()
{
var sp = ConfigureProvider(sc =>
{
sc
.AddTransient<IService, SomeRandomService>()
.AddTransient<DecoratedService>()
.Decorate<DecoratedService, Decorator2>();
});

var result = sp.GetService<DecoratedService>() as Decorator2;

Assert.NotNull(result);
Assert.NotNull(result.Inner);
Assert.NotNull(result.Inner.Dependency);
}

public interface IDecoratedService { }

public class DecoratedService
{
public DecoratedService(IService dependency)
{
Dependency = dependency;
}

public IService Dependency { get; }
}

public class Decorator2 : DecoratedService
{
public Decorator2(DecoratedService decoratedService)
: base(null)
{
Inner = decoratedService;
}

public DecoratedService Inner { get; }
}

public interface IService { }

private class SomeRandomService : IService { }
Expand Down