Amend ScopeServiceFactory to return a tuple of 2 value

This commit is contained in:
TheXamlGuy
2024-09-29 16:39:28 +01:00
parent 9c1f5fd690
commit 750a57eb12
2 changed files with 7 additions and 5 deletions
+4 -2
View File
@@ -1,6 +1,8 @@
namespace Toolkit.Foundation; using Microsoft.Extensions.DependencyInjection;
namespace Toolkit.Foundation;
public interface IServiceScopeFactory<TService> public interface IServiceScopeFactory<TService>
{ {
TService? Create(params object?[] parameters); (IServiceScope, TService)? Create(params object?[] parameters);
} }
@@ -2,12 +2,12 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public class ServiceScopeFactory<TService>(IServiceScopeFactory serviceScopeFactory, public class ScopeServiceFactory<TService>(IServiceScopeFactory serviceScopeFactory,
ICache<TService, IServiceScope> cache) : ICache<TService, IServiceScope> cache) :
IServiceScopeFactory<TService> IServiceScopeFactory<TService>
where TService : notnull where TService : notnull
{ {
public TService? Create(params object?[] parameters) public (IServiceScope, TService)? Create(params object?[] parameters)
{ {
if (serviceScopeFactory.CreateScope() is IServiceScope serviceScope) if (serviceScopeFactory.CreateScope() is IServiceScope serviceScope)
{ {
@@ -16,7 +16,7 @@ public class ServiceScopeFactory<TService>(IServiceScopeFactory serviceScopeFact
if (factory.Create<TService>(parameters) is TService service) if (factory.Create<TService>(parameters) is TService service)
{ {
cache.Add(service, serviceScope); cache.Add(service, serviceScope);
return service; return (serviceScope, service);
} }
} }
} }