Datei: NDODLL/ServiceProvider/NDOServiceProvider.cs

Last Commit (8aabfc4)
1 -- File didn't exist --
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
New Commit (21ade13)
1 //
2 // Copyright (c) 2002-2024 Mirko Matytschak
3 // (www.netdataobjects.de)
4 //
5 // Author: Mirko Matytschak
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
11 // conditions:
12
13 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
14 // of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22 using System;
23 using System.Collections.Generic;
24 using Microsoft.Extensions.DependencyInjection;
25 using Microsoft.Extensions.Logging;
26 using NDO.Query;
27 using NDO.SqlPersistenceHandling;
28
29 namespace NDO.Application
30 {
31 ····/// <summary>
32 ····/// Minimal IServiceProvider implementation, which is used, if the application
33 ····/// doesn't set up a host and doesn't call AddNdo and UseNdo.
34 ····/// </summary>
35 ····/// <remarks>
36 ····/// All registered services are transient, like the services registered in BuilderExtensions.AddNdo.
37 ····/// The provider acts as its own scope factory, so that IServiceProvider.CreateScope() works.
38 ····/// Logging services are provided by the NDOConsoleLogger.
39 ····/// </remarks>
40 ····internal class NDOServiceProvider : IServiceProvider, IServiceScopeFactory, IServiceScope
41 ····{
42 ········private readonly Dictionary<Type, Func<IServiceProvider, object>> factories = new Dictionary<Type, Func<IServiceProvider, object>>();
43
44 ········public NDOServiceProvider()
45 ········{
46 ············factories.Add( typeof( IPersistenceHandler ), sp => new SqlPersistenceHandler( sp ) );
47 ············factories.Add( typeof( IQueryGenerator ), sp => new SqlQueryGenerator() );
48 ············factories.Add( typeof( INDOTransactionScope ), sp => new NDOTransactionScope() );
49 ············factories.Add( typeof( ILoggerFactory ), sp => NDOConsoleLoggerFactory.Instance );
50 ············factories.Add( typeof( IServiceProvider ), sp => sp );
51 ············factories.Add( typeof( IServiceScopeFactory ), sp => sp );
52 ········}
53
54 ········/// <inheritdoc/>
55 ········public object GetService( Type serviceType )
56 ········{
57 ············if (serviceType == null)
58 ················throw new ArgumentNullException( nameof( serviceType ) );
59
60 ············if (factories.TryGetValue( serviceType, out var factory ))
61 ················return factory( this );
62
63 ············if (serviceType.IsGenericType && serviceType.GetGenericTypeDefinition() == typeof( ILogger<> ))
64 ················return Activator.CreateInstance( typeof( NDOConsoleLogger<> ).MakeGenericType( serviceType.GetGenericArguments() ) );
65
66 ············return null;
67 ········}
68
69 ········/// <inheritdoc/>
70 ········public IServiceScope CreateScope()
71 ········{
72 ············// There are no scoped services, so the provider itself can serve as scope.
73 ············return this;
74 ········}
75
76 ········IServiceProvider IServiceScope.ServiceProvider => this;
77
78 ········void IDisposable.Dispose()
79 ········{
80 ············// Nothing to dispose. The provider lives as long as the application.
81 ········}
82 ····}
83 }
84