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
0 commit comments