-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It provides an easy way to resolve a custom component or just delegate to an existing patcher.
- Loading branch information
Showing
5 changed files
with
135 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
||
namespace DataIsland; | ||
|
||
internal class LambdaPatcher<TDataAccess, TTenant>(Func<IServiceProvider, TTenant, TDataAccess> _factoryMethod) | ||
: IDependencyPatcher<TDataAccess> | ||
where TDataAccess : notnull | ||
where TTenant : class | ||
{ | ||
public void Register(IServiceCollection serviceCollection) | ||
{ | ||
var originalRegistrations = serviceCollection.Where(x => x.ServiceType == typeof(TDataAccess)).ToList(); | ||
if (originalRegistrations.Count == 0) | ||
throw new InvalidOperationException($"Service {typeof(TDataAccess)} isn't in the service collection - it can't be patched. Ensure the service is registered as a service before calling the patch method to patch the registration."); | ||
|
||
var replacements = new List<ServiceDescriptor>(originalRegistrations.Count); | ||
foreach (var registration in originalRegistrations) | ||
{ | ||
replacements.Add(new ServiceDescriptor(typeof(TDataAccess), sp => | ||
{ | ||
var testContext = sp.GetRequiredService<ITestContext>(); | ||
var tenant = testContext.GetTenant<TTenant>(typeof(TDataAccess)); | ||
return _factoryMethod(sp, tenant); | ||
}, registration.Lifetime)); | ||
} | ||
|
||
serviceCollection.RemoveAll(typeof(TDataAccess)); | ||
serviceCollection.Add(replacements); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters