Skip to content

Commit 98db793

Browse files
committed
feat: exposes service registration api
Needed to provide a custom service provider via the UseInternalServiceProvider option.
1 parent c90d110 commit 98db793

File tree

2 files changed

+77
-44
lines changed

2 files changed

+77
-44
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#if EFCORE
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.EntityFrameworkCore.Diagnostics;
4+
using Microsoft.EntityFrameworkCore.Infrastructure;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.DependencyInjection.Extensions;
7+
using System;
8+
9+
namespace BlazarTech.QueryableValues
10+
{
11+
/// <summary>
12+
/// Provides extension methods to register core QueryableValues services.
13+
/// </summary>
14+
public static class QueryableValuesServiceCollectionExtensions
15+
{
16+
/// <summary>
17+
/// Adds the services required by QueryableValues for the Microsoft SQL Server database provider.
18+
/// </summary>
19+
/// <remarks>
20+
/// It is only needed when building the internal service provider for use with
21+
/// the <see cref="DbContextOptionsBuilder.UseInternalServiceProvider" /> method.
22+
/// This is not recommend other than for some advanced scenarios.
23+
/// </remarks>
24+
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
25+
/// <returns>The same service collection so that multiple calls can be chained.</returns>
26+
/// <exception cref="ArgumentNullException"></exception>
27+
public static IServiceCollection AddQueryableValuesSqlServer(this IServiceCollection services)
28+
{
29+
if (services is null)
30+
{
31+
throw new ArgumentNullException(nameof(services));
32+
}
33+
34+
for (var index = services.Count - 1; index >= 0; index--)
35+
{
36+
var descriptor = services[index];
37+
if (descriptor.ServiceType != typeof(IModelCustomizer))
38+
{
39+
continue;
40+
}
41+
42+
if (descriptor.ImplementationType is null)
43+
{
44+
continue;
45+
}
46+
47+
// Replace theirs with ours.
48+
services[index] = new ServiceDescriptor(
49+
descriptor.ServiceType,
50+
typeof(ModelCustomizer<>).MakeGenericType(descriptor.ImplementationType),
51+
descriptor.Lifetime
52+
);
53+
54+
// Add theirs as is, so we can inject it into ours.
55+
services.Add(
56+
new ServiceDescriptor(
57+
descriptor.ImplementationType,
58+
descriptor.ImplementationType,
59+
descriptor.Lifetime
60+
)
61+
);
62+
}
63+
64+
services.TryAddSingleton<Serializers.IXmlSerializer, Serializers.XmlSerializer>();
65+
services.TryAddSingleton<Serializers.IJsonSerializer, Serializers.JsonSerializer>();
66+
services.TryAddScoped<SqlServer.XmlQueryableFactory>();
67+
services.TryAddScoped<SqlServer.JsonQueryableFactory>();
68+
services.TryAddScoped<SqlServer.ExtensionOptions>();
69+
services.TryAddScoped<SqlServer.QueryableFactoryFactory>();
70+
services.TryAddScoped<IInterceptor, SqlServer.JsonSupportConnectionInterceptor>();
71+
72+
return services;
73+
}
74+
}
75+
}
76+
#endif

src/QueryableValues.SqlServer/QueryableValuesSqlServerExtension.cs

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#if EFCORE
2-
using Microsoft.EntityFrameworkCore.Diagnostics;
32
using Microsoft.EntityFrameworkCore.Infrastructure;
43
using Microsoft.Extensions.DependencyInjection;
5-
using System;
64
using System.Collections.Generic;
75

86
namespace BlazarTech.QueryableValues
@@ -14,48 +12,7 @@ internal sealed class QueryableValuesSqlServerExtension : IDbContextOptionsExten
1412

1513
public void ApplyServices(IServiceCollection services)
1614
{
17-
if (services is null)
18-
{
19-
throw new ArgumentNullException(nameof(services));
20-
}
21-
22-
for (var index = services.Count - 1; index >= 0; index--)
23-
{
24-
var descriptor = services[index];
25-
if (descriptor.ServiceType != typeof(IModelCustomizer))
26-
{
27-
continue;
28-
}
29-
30-
if (descriptor.ImplementationType is null)
31-
{
32-
continue;
33-
}
34-
35-
// Replace theirs with ours.
36-
services[index] = new ServiceDescriptor(
37-
descriptor.ServiceType,
38-
typeof(ModelCustomizer<>).MakeGenericType(descriptor.ImplementationType),
39-
descriptor.Lifetime
40-
);
41-
42-
// Add theirs as is, so we can inject it into ours.
43-
services.Add(
44-
new ServiceDescriptor(
45-
descriptor.ImplementationType,
46-
descriptor.ImplementationType,
47-
descriptor.Lifetime
48-
)
49-
);
50-
}
51-
52-
services.AddSingleton<Serializers.IXmlSerializer, Serializers.XmlSerializer>();
53-
services.AddSingleton<Serializers.IJsonSerializer, Serializers.JsonSerializer>();
54-
services.AddScoped<SqlServer.XmlQueryableFactory>();
55-
services.AddScoped<SqlServer.JsonQueryableFactory>();
56-
services.AddScoped<SqlServer.ExtensionOptions>();
57-
services.AddScoped<SqlServer.QueryableFactoryFactory>();
58-
services.AddScoped<IInterceptor, SqlServer.JsonSupportConnectionInterceptor>();
15+
services.AddQueryableValuesSqlServer();
5916
}
6017

6118
public void Validate(IDbContextOptions options)

0 commit comments

Comments
 (0)