|
| 1 | +using System.Linq.Expressions; |
| 2 | +using System.Reflection; |
| 3 | +using Microsoft.Extensions.DependencyInjection; |
| 4 | + |
| 5 | +namespace Hyperbee.Expressions; |
| 6 | + |
| 7 | +public class InjectExpression<T> : Expression, IDependencyInjectionExpression |
| 8 | +{ |
| 9 | + private IServiceProvider _serviceProvider; |
| 10 | + private readonly string _key; |
| 11 | + private readonly Expression _defaultValue; |
| 12 | + |
| 13 | + public InjectExpression( IServiceProvider serviceProvider, string key, Expression defaultValue ) |
| 14 | + { |
| 15 | + _serviceProvider = serviceProvider; |
| 16 | + _key = key; |
| 17 | + _defaultValue = defaultValue; |
| 18 | + } |
| 19 | + |
| 20 | + public void SetServiceProvider( IServiceProvider serviceProvider ) |
| 21 | + { |
| 22 | + _serviceProvider ??= serviceProvider; |
| 23 | + } |
| 24 | + |
| 25 | + public override ExpressionType NodeType => ExpressionType.Extension; |
| 26 | + public override Type Type => typeof( T ); |
| 27 | + public override bool CanReduce => true; |
| 28 | + |
| 29 | + private static readonly MethodInfo GetServiceMethodInfo = typeof( ServiceProviderServiceExtensions ) |
| 30 | + .GetMethod( "GetService", [typeof( IServiceProvider )] )! |
| 31 | + .MakeGenericMethod( typeof( T ) ); |
| 32 | + |
| 33 | + private static readonly MethodInfo GetKeyedServiceMethodInfo = typeof( ServiceProviderKeyedServiceExtensions ) |
| 34 | + .GetMethod( "GetKeyedService", [typeof( IServiceProvider ), typeof( string )] )! |
| 35 | + .MakeGenericMethod( typeof( T ) ); |
| 36 | + |
| 37 | + public override Expression Reduce() |
| 38 | + { |
| 39 | + if ( _serviceProvider == null ) |
| 40 | + { |
| 41 | + throw new InvalidOperationException( "Service provider is not available." ); |
| 42 | + } |
| 43 | + |
| 44 | + var serviceValue = Variable( typeof( T ), "serviceValue" ); |
| 45 | + |
| 46 | + var callExpression = _key == null |
| 47 | + ? Call( |
| 48 | + null, |
| 49 | + GetServiceMethodInfo, |
| 50 | + Constant( _serviceProvider ) |
| 51 | + ) |
| 52 | + : Call( |
| 53 | + null, |
| 54 | + GetKeyedServiceMethodInfo, |
| 55 | + [Constant( _serviceProvider ), Constant( _key )] |
| 56 | + ); |
| 57 | + |
| 58 | + var defaultExpression = _defaultValue ?? |
| 59 | + Throw( New( ExceptionHelper.ConstructorInfo, Constant( "Service is not available." ) ), typeof( T ) ); |
| 60 | + |
| 61 | + return Block( |
| 62 | + [serviceValue], |
| 63 | + Assign( serviceValue, callExpression ), |
| 64 | + Condition( |
| 65 | + NotEqual( serviceValue, Constant( null, typeof( T ) ) ), |
| 66 | + serviceValue, |
| 67 | + defaultExpression |
| 68 | + ) |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + protected override Expression VisitChildren( ExpressionVisitor visitor ) |
| 73 | + { |
| 74 | + return this; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +internal static class ExceptionHelper |
| 79 | +{ |
| 80 | + internal static readonly ConstructorInfo ConstructorInfo = typeof( InvalidOperationException ) |
| 81 | + .GetConstructor( [typeof( string )] ); |
| 82 | +} |
| 83 | + |
| 84 | +public static partial class ExpressionExtensions |
| 85 | +{ |
| 86 | + public static InjectExpression<T> Inject<T>( IServiceProvider serviceProvider, string key = null, Expression defaultValue = null ) |
| 87 | + { |
| 88 | + return new InjectExpression<T>( serviceProvider, key, defaultValue ); |
| 89 | + } |
| 90 | + |
| 91 | + public static InjectExpression<T> Inject<T>( string key = null, Expression defaultValue = null ) |
| 92 | + { |
| 93 | + return new InjectExpression<T>( null, key, defaultValue ); |
| 94 | + } |
| 95 | +} |
0 commit comments