Skip to content

Commit

Permalink
docs: fix code sample references
Browse files Browse the repository at this point in the history
  • Loading branch information
egil committed Oct 12, 2023
1 parent be17d1f commit c37455b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
13 changes: 8 additions & 5 deletions docs/samples/tests/xunit/CustomServiceProviderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

namespace Bunit.Docs.Samples;

public sealed class CustomServiceProvider : IServiceProvider, IServiceScopeFactory, IServiceScope {
public sealed class CustomServiceProvider : IServiceProvider, IServiceScopeFactory, IServiceScope
{
private readonly IServiceProvider _serviceProvider;

public CustomServiceProvider(IServiceCollection serviceDescriptors)
=> _serviceProvider = serviceDescriptors.BuildServiceProvider();

public object GetService(Type serviceType) {
public object GetService(Type serviceType)
{
if (serviceType == typeof(IServiceScope) || serviceType == typeof(IServiceScopeFactory))
return this;

Expand All @@ -24,10 +26,10 @@ public object GetService(Type serviceType) {
void IDisposable.Dispose() { }
public IServiceScope CreateScope() => this;
IServiceProvider IServiceScope.ServiceProvider => this;

}

public sealed class CustomServiceProviderFactoryContainerBuilder {
public sealed class CustomServiceProviderFactoryContainerBuilder
{
private readonly IServiceCollection _serviceDescriptors;

public CustomServiceProviderFactoryContainerBuilder(IServiceCollection serviceDescriptors)
Expand All @@ -37,7 +39,8 @@ public IServiceProvider Build()
=> new CustomServiceProvider(_serviceDescriptors);
}

public sealed class CustomServiceProviderFactory : IServiceProviderFactory<CustomServiceProviderFactoryContainerBuilder> {
public sealed class CustomServiceProviderFactory : IServiceProviderFactory<CustomServiceProviderFactoryContainerBuilder>
{
public CustomServiceProviderFactoryContainerBuilder CreateBuilder(IServiceCollection services)
=> new CustomServiceProviderFactoryContainerBuilder(services);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,26 @@ The example makes use of `AutofacServiceProviderFactory` and `AutofacServiceProv

Here is a test where the Autofac service provider factory is used:

[!code-csharp[](../../../samples/tests/xunit/CustomServiceProviderFactoryUsage.cs?start=31&end=50)]
[!code-csharp[](../../../samples/tests/xunit/CustomServiceProviderFactoryUsage.cs?start=32&end=56)]

Here is a test where the Autofac service provider is used via delegate:

[!code-csharp[](../../../samples/tests/xunit/CustomServiceProviderFactoryUsage.cs?start=55&end=80)]
[!code-csharp[](../../../samples/tests/xunit/CustomServiceProviderFactoryUsage.cs?start=58&end=88)]

### Registering a custom service provider factory
The examples contain dummy implementations of `IServiceProvider` and `IServiceProviderFactory<TContainerBuilder>`. Normally those implementations are supplied by the creator of your custom dependency injection solution (e.g. Autofac example above). This dummy implementations are not intended to use as is.

This is an example of how to implement and use a dummy custom service provider factory.

[!code-csharp[](../../../samples/tests/xunit/CustomServiceProviderFactory.cs?start=8&end=46)]
[!code-csharp[](../../../samples/tests/xunit/CustomServiceProviderFactory.cs?start=8&end=49)]

Here is a test where the custom service provider factory is used:

[!code-csharp[](../../../samples/tests/xunit/CustomServiceProviderFactoryUsage.cs?start=13&end=17)]
[!code-csharp[](../../../samples/tests/xunit/CustomServiceProviderFactoryUsage.cs?start=15&end=19)]

Here is a test where the custom service provider is used via delegate:

[!code-csharp[](../../../samples/tests/xunit/CustomServiceProviderFactoryUsage.cs?start=22&end=26)]
[!code-csharp[](../../../samples/tests/xunit/CustomServiceProviderFactoryUsage.cs?start=25&end=29)]

## Further reading

Expand Down

0 comments on commit c37455b

Please sign in to comment.