feat: working version with scopes.
This commit is contained in:
@@ -2,7 +2,6 @@ using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Godot;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OwofGames.GodotLume;
|
||||
|
||||
@@ -15,29 +14,28 @@ public partial class ServiceSource : Node
|
||||
[Export] private string _serviceScope = RootScopeQualifiedName;
|
||||
[Export] protected Node?[] InjectedNodes = [];
|
||||
|
||||
internal void ResolveInjectedNodes(IServiceProvider serviceProvider, Delegate? onScopeCreated)
|
||||
internal void ResolveInjectedNodes(IProvider provider, Delegate? onScopeCreated)
|
||||
{
|
||||
var logger = serviceProvider.GetRequiredService<ILogger<ServiceSource>>();
|
||||
var logger = provider.Get<ILogger<ServiceSource>>();
|
||||
|
||||
// if this service source must also act as a scope, create the scope and use its service provider
|
||||
if (_serviceScope != RootScopeQualifiedName)
|
||||
{
|
||||
// create the scoped provider
|
||||
var serviceScope = ScopeTypesAndNames.GetType(_serviceScope);
|
||||
var scopedProvider = serviceProvider
|
||||
.GetRequiredService<IProvider>()
|
||||
.GetScopedProvider(serviceScope);
|
||||
// convert it also to an IServiceProvider
|
||||
var serviceProviderCreator = serviceProvider.GetRequiredService<Func<IProvider, IServiceProvider>>();
|
||||
serviceProvider = serviceProviderCreator(scopedProvider);
|
||||
provider = provider.GetScopedProvider(serviceScope);
|
||||
// invoke onScopeCreated, if present
|
||||
// TODO: replace with a source code generation by turning this method private partial with Action<IProvider> argument, and every invocation site calls the necessary provider.Get<...> to build the argument list
|
||||
if (onScopeCreated != null)
|
||||
{
|
||||
var parameters = onScopeCreated.Method.GetParameters()
|
||||
.Select(parameterInfo => scopedProvider.Get(parameterInfo.ParameterType));
|
||||
var parameters = onScopeCreated.Method
|
||||
.GetParameters()
|
||||
.Select(parameterInfo => provider.Get(parameterInfo.ParameterType))
|
||||
.ToArray();
|
||||
onScopeCreated.DynamicInvoke(parameters);
|
||||
}
|
||||
|
||||
OnScopeCreated(provider);
|
||||
}
|
||||
else if (onScopeCreated != null)
|
||||
{
|
||||
@@ -56,7 +54,7 @@ public partial class ServiceSource : Node
|
||||
{
|
||||
if (field.GetCustomAttribute<InjectAttribute>() == null) continue;
|
||||
var fieldType = field.FieldType;
|
||||
var service = serviceProvider.GetRequiredService(fieldType);
|
||||
var service = provider.Get(fieldType);
|
||||
field.SetValue(node, service);
|
||||
}
|
||||
|
||||
@@ -70,7 +68,7 @@ public partial class ServiceSource : Node
|
||||
foreach (var parameter in parameters)
|
||||
{
|
||||
var parameterType = parameter.ParameterType;
|
||||
var service = serviceProvider.GetRequiredService(parameterType);
|
||||
var service = provider.Get(parameterType);
|
||||
values[i++] = service;
|
||||
}
|
||||
|
||||
@@ -78,4 +76,9 @@ public partial class ServiceSource : Node
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: also add source generator to mark methods in derived classes with [OnScopeCreated] and perform parameter injection
|
||||
protected virtual void OnScopeCreated(IProvider serviceProvider)
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user