Datei: NDODLL/PersistenceHandlerManagement/NDOPersistenceHandlerManager.cs

Last Commit (1e1255d)
1 using NDO.SqlPersistenceHandling;
2 using System;
3 using Microsoft.Extensions.DependencyInjection;
4 using NDO.Mapping;
 
5
6 namespace NDO
7 {
8 ····/// <summary>
9 ····/// Default implementation of a PersistenceHandlerManager
10 ····/// </summary>
11 public class NDOPersistenceHandlerManager : IPersistenceHandlerManager
12 ····{
13 private readonly IServiceProvider configContainer;
14 ········private readonly NDOMapping mappings;
15 private readonly IPersistenceHandlerPool persistenceHandlerPool;
16
17 ········/// <summary>
18 ········/// Constructor
19 ········/// </summary>
20 ········/// <param name="serviceProvider"></param>
21 ········/// <param name="mappings"></param>
22 ········public NDOPersistenceHandlerManager(IServiceProvider serviceProvider, NDOMapping mappings)
23 ········{
24 this. configContainer = serviceProvider;
25 ············this.mappings = mappings;
26 ············this.persistenceHandlerPool = serviceProvider.GetRequiredService< IPersistenceHandlerPool>();
27 ········}
28 ········/// <summary>
29 ········/// Get a persistence handler for the given object.
30 ········/// </summary>
31 ········/// <param name="pc"></param>
32 ········/// <returns></returns>
33 ········public IPersistenceHandler GetPersistenceHandler( IPersistenceCapable pc )
34 ········{
35 ············return GetPersistenceHandler( pc.GetType() );
36 ········}
37
38 ········void ReleaseHandler(Type t, IPersistenceHandler handler)
39 ········{
40 ············// Don't close the connection or transaction here
41 ············// because it might be used with other handlers.
42 ············handler.Connection = null;
43 ············this.persistenceHandlerPool.ReleaseHandler( t, handler );
44 ········}
45
46 ········/// <summary>
47 ········/// Gets a persistence handler for a given type
48 ········/// </summary>
49 ········/// <param name="type"></param>
50 ········/// <returns></returns>
51 ········public IPersistenceHandler GetPersistenceHandler( Type type )
52 ········{
 
 
53 ············if (type.IsGenericType)
54 ················type = type.GetGenericTypeDefinition();
55
56 IPersistenceHandler handler = persistenceHandlerPool. GetHandler( type, ( t) =>
57 ············{
58 // 1. If a handler type is registered, use an instance of this handler
59 var newHandler = this. configContainer. GetService<IPersistenceHandler>( ) ;
60
61 // 2. try to use an NDOPersistenceHandler
62 if ( newHandler == null)
63 newHandler = new SqlPersistenceHandler( this. configContainer ) ;
64
65 return newHandler;
66 } ) ;
67
68 ············// The dataSet will be used as template to create a DataTable for the query results.
69 ············handler.Initialize( this.mappings, type, ReleaseHandler );
70
71 ············return handler;
72 ········}
73 ····}
74 }
75
New Commit (d0706b7)
1 using NDO.SqlPersistenceHandling;
2 using System;
3 using Microsoft.Extensions.DependencyInjection;
4 using NDO.Mapping;
5 using System.Collections.Generic;
6
7 namespace NDO
8 {
9 ····/// <summary>
10 ····/// Default implementation of a PersistenceHandlerManager
11 ····/// </summary>
12 public class NDOPersistenceHandlerManager
13 ····{
14 private readonly IServiceProvider serviceProvider;
15 ········private readonly NDOMapping mappings;
16 private readonly Dictionary<Type, IPersistenceHandler> handlers = new Dictionary<Type, IPersistenceHandler>( ) ;
17
18 ········/// <summary>
19 ········/// Constructor
20 ········/// </summary>
21 ········/// <param name="serviceProvider"></param>
22 ········/// <param name="mappings"></param>
23 ········public NDOPersistenceHandlerManager(IServiceProvider serviceProvider, NDOMapping mappings)
24 ········{
25 this. serviceProvider = serviceProvider;
26 ············this.mappings = mappings;
 
27 ········}
28 ········/// <summary>
29 ········/// Get a persistence handler for the given object.
30 ········/// </summary>
31 ········/// <param name="pc"></param>
32 ········/// <returns></returns>
33 ········public IPersistenceHandler GetPersistenceHandler( IPersistenceCapable pc )
34 ········{
35 ············return GetPersistenceHandler( pc.GetType() );
36 ········}
37
38 ········void ReleaseHandler(Type t, IPersistenceHandler handler)
39 ········{
40 ············// Don't close the connection or transaction here
41 ············// because it might be used with other handlers.
42 ············handler.Connection = null;
 
43 ········}
44
45 ········/// <summary>
46 ········/// Gets a persistence handler for a given type
47 ········/// </summary>
48 ········/// <param name="type"></param>
49 ········/// <returns></returns>
50 ········public IPersistenceHandler GetPersistenceHandler( Type type )
51 ········{
52 ············// This code doesn't need to be thread safe because
53 ············// each thread needs another pm and therefore another PersistenceHandler instance.
54 ············if (type.IsGenericType)
55 ················type = type.GetGenericTypeDefinition();
56 ············
57 if( !handlers. TryGetValue( type, out var handler ) )
58 ············{
59 // If a handler type is registered, use an instance of this handler
60 handler = this. serviceProvider. GetService<IPersistenceHandler>( ) ;
61
62 // We shouldn't reach this code, since NDO registers the NdoPersistenceHandler
63 if ( handler == null)
64 handler = new SqlPersistenceHandler( this. serviceProvider ) ;
65
66 handlers. Add( type, handler ) ;
67 } ;
68
69 ············// The dataSet will be used as template to create a DataTable for the query results.
70 ············handler.Initialize( this.mappings, type, ReleaseHandler );
71
72 ············return handler;
73 ········}
74 ····}
75 }
76