Skip to content

Commit 57b8ace

Browse files
adamjones2khellang
authored andcommitted
Add support for exposing decorated services
1 parent 2cdfc8d commit 57b8ace

File tree

5 files changed

+396
-30
lines changed

5 files changed

+396
-30
lines changed

src/Scrutor/DecoratedService.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Scrutor;
5+
6+
/// <summary>
7+
/// A handle to a decorated service. This can be used to resolve decorated services from an <see cref="IServiceProvider"/>
8+
/// using <see cref="ServiceProviderExtensions.GetRequiredDecoratedService{TService}(IServiceProvider, DecoratedService{TService})"/>.
9+
/// </summary>
10+
/// <typeparam name="TService">The type of services which were decorated.</typeparam>
11+
public sealed class DecoratedService<TService>
12+
{
13+
internal DecoratedService(Type serviceType, IReadOnlyList<string> serviceKeys)
14+
{
15+
if (!typeof(TService).IsAssignableFrom(serviceType))
16+
throw new ArgumentException($"The type {serviceType} is not assignable to the service type {typeof(TService)}");
17+
18+
ServiceType = serviceType;
19+
ServiceKeys = serviceKeys;
20+
}
21+
22+
internal Type ServiceType { get; }
23+
internal IReadOnlyList<string> ServiceKeys { get; } // In descending order of precedence
24+
25+
internal DecoratedService<TService2> Downcast<TService2>() => new(ServiceType, ServiceKeys);
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#if NETFRAMEWORK
2+
3+
using System;
4+
5+
/// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the corresponding type allows it.</summary>
6+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
7+
internal sealed class NotNullWhenAttribute : Attribute
8+
{
9+
/// <summary>Initializes the attribute with the specified return value condition.</summary>
10+
/// <param name="returnValue">
11+
/// The return value condition. If the method returns this value, the associated parameter will not be null.
12+
/// </param>
13+
public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
14+
15+
/// <summary>Gets the return value condition.</summary>
16+
public bool ReturnValue { get; }
17+
}
18+
19+
#endif

0 commit comments

Comments
 (0)