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