Datei: NDODLL/PersistenceManagerBase.cs
Last Commit (1e1255d)
| 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 System.Linq; |
| 25 | using System.IO; |
| 26 | using System.Data; |
| 27 | using NDOInterfaces; |
| 28 | using NDO.Mapping; |
| 29 | using NDO.Application; |
| 30 | using NDO.SchemaGenerator; |
| 31 | using System.Reflection; |
| 32 | using Microsoft.Extensions.DependencyInjection; |
| 33 | using Microsoft.Extensions.Logging; |
| 34 | using NDO.ProviderFactory; |
| 35 | |
| 36 | namespace NDO |
| 37 | { |
| 38 | ····/// <summary> |
| 39 | ····/// Provides base functionality for PersistenceManager classes |
| 40 | ····/// </summary> |
| 41 | ····public class PersistenceManagerBase : IPersistenceManagerBase |
| 42 | ····{ |
| 43 | ········internal Cache cache = new Cache(); |
| 44 | ········/// <summary> |
| 45 | ········/// The DataSet used as template for DataRows |
| 46 | ········/// </summary> |
| 47 | ········protected DataSet ds = null; |
| 48 | ········/// <summary> |
| 49 | ········/// The StateManager instance which will be used for all objects |
| 50 | ········/// </summary> |
| 51 | ········protected IStateManager sm; |
| 52 | ········internal Mappings mappings;··// protected will make the compiler complaining |
| 53 | ········private IServiceProvider scopedServiceProvider; |
| 54 | ········private IServiceScope scope; |
| 55 | ········/// <summary> |
| 56 | ········/// Gets or sets the logger for the PersistenceManager instance |
| 57 | ········/// </summary> |
| 58 | ········protected ILogger Logger { get; set; } |
| 59 | ········private IPersistenceHandlerManager persistenceHandlerManager; |
| 60 | ········bool isClosing = false; |
| 61 | ········private INDOProviderFactory providerFactory; |
| 62 | |
| 63 | ········/// <summary> |
| 64 | ········/// Provides the ProviderFactory to inherited persistence managers; |
| 65 | ········/// </summary> |
| 66 | ········protected INDOProviderFactory ProviderFactory |
| 67 | ········{ |
| 68 | ············get |
| 69 | ············{ |
| 70 | ················if (this.providerFactory == null) |
| 71 | ················{ |
| 72 | ····················var serviceProvider = NDOApplication.ServiceProvider; |
| 73 | |
| 74 | ····················if (serviceProvider != null) |
| 75 | ····················{ |
| 76 | ························this.providerFactory = serviceProvider.GetService<INDOProviderFactory>(); |
| 77 | ····················} |
| 78 | |
| 79 | ····················if (this.providerFactory == null) |
| 80 | ····················{ |
| 81 | ························this.providerFactory = NDOProviderFactory.Instance; |
| 82 | ····················} |
| 83 | ················} |
| 84 | |
| 85 | ················return this.providerFactory; |
| 86 | ············} |
| 87 | ········} |
| 88 | |
| 89 | |
| 90 | ········/// <summary> |
| 91 | ········/// If set to true, the resultset of each query is stored, so that results of an identical query |
| 92 | ········/// can be read from the cache. |
| 93 | ········/// </summary> |
| 94 | ········/// <remarks> |
| 95 | ········/// NDO computes an SHA256 hash for each query including the query parameters. The hash is the key to the query cache. |
| 96 | ········/// Do not use the QueryCache in applications, which work with one PersistenceManager |
| 97 | ········/// over a long time with multiple transactions. |
| 98 | ········/// </remarks> |
| 99 | ········public bool UseQueryCache { get; set; } |
| 100 | |
| 101 | ········private Dictionary<string, object> queryCache = new Dictionary<string, object>(); |
| 102 | |
| 103 | ········/// <summary> |
| 104 | ········/// This is the query cache in which resultsets can be stored. |
| 105 | ········/// </summary> |
| 106 | ········/// <remarks>Usually you won't have to access the query cache.</remarks> |
| 107 | ········public Dictionary<string, object> QueryCache => this.queryCache; |
| 108 | |
| 109 | ········/// <summary> |
| 110 | ········/// Register a listener to this event, if you have to provide user generated ids. |
| 111 | ········/// This event is usefull for databases, which doesn't provide auto-incremented ids, like the Oracle Db. |
| 112 | ········/// The event will be fired if a new id is needed. |
| 113 | ········/// </summary> |
| 114 | ········public event IdGenerationHandler IdGenerationEvent; |
| 115 | |
| 116 | ········/// <summary> |
| 117 | ········/// Constructor |
| 118 | ········/// </summary> |
| 119 | ········public PersistenceManagerBase( IServiceProvider scopedServiceProvider ) |
| 120 | ········{ |
| 121 | ············this.scopedServiceProvider = scopedServiceProvider; |
| 122 | ············string baseDir = AppDomain.CurrentDomain.BaseDirectory; |
| 123 | ············if (File.Exists( Path.Combine( baseDir, "Web.config" ) )) |
| 124 | ················baseDir = Path.Combine( baseDir, "bin" ); |
| 125 | ············var entryAssemblyName = Assembly.GetEntryAssembly()?.GetName()?.Name; |
| 126 | ············List<string> paths = new List<string>(); |
| 127 | ············if (entryAssemblyName != null) |
| 128 | ············paths.Add( Path.Combine( baseDir, $"{entryAssemblyName}.ndo.mapping" ) ); |
| 129 | ············paths.Add( Path.Combine( baseDir, "NDOMapping.xml" ) ); |
| 130 | ············ |
| 131 | ············bool found = false; |
| 132 | ············foreach (var path in paths) |
| 133 | ············{ |
| 134 | ················if (File.Exists(path)) |
| 135 | ················{ |
| 136 | ····················Init( path ); |
| 137 | ····················found = true; |
| 138 | ····················break; |
| 139 | ················} |
| 140 | ············} |
| 141 | ············if (!found) |
| 142 | ················throw new NDOException( 49, $"Can't determine the path to the mapping file. Tried the following locations:\n{string.Join("\n", paths)}\nPlease provide a mapping file path as argument to the PersistenceManager ctor." ); |
| 143 | ········} |
| 144 | |
| 145 | ········/// <summary> |
| 146 | ········/// Constructs a PersistenceManagerBase object using the path to a mapping file. |
| 147 | ········/// </summary> |
| 148 | ········/// <param name="scopedServiceProvider">An IServiceProvider instance, which represents a scope (e.g. a request in an AspNet application)</param> |
| 149 | ········/// <param name="pathToMappingFile"></param> |
| 150 | ········public PersistenceManagerBase(string pathToMappingFile, IServiceProvider scopedServiceProvider) |
| 151 | ········{ |
| 152 | ············this.scopedServiceProvider = scopedServiceProvider; |
| 153 | ············Init(pathToMappingFile); |
| 154 | ········} |
| 155 | |
| 156 | ········/// <summary> |
| 157 | ········/// Constructs a PersistenceManagerBase object using the mapping object. |
| 158 | ········/// </summary> |
| 159 | ········/// <param name="mapping"></param> |
| 160 | ········/// <param name="scopedServiceProvider">An IServiceProvider instance, which represents a scope (e.g. a request in an AspNet application)</param> |
| 161 | ········public PersistenceManagerBase(NDOMapping mapping, IServiceProvider scopedServiceProvider) |
| 162 | ········{ |
| 163 | ············this.scopedServiceProvider = scopedServiceProvider; |
| 164 | ············var localMappings = mapping as Mappings; |
| 165 | ············if (localMappings == null) |
| 166 | ················throw new ArgumentException( "The mapping must be constructed by a PersistenceManager", nameof( mapping ) ); |
| 167 | |
| 168 | ············Init( localMappings ); |
| 169 | ········} |
| 170 | |
| 171 | ········/// <summary> |
| 172 | ········/// Initializes a PersistenceManager using the path to a mapping file |
| 173 | ········/// </summary> |
| 174 | ········/// <param name="mappingPath"></param> |
| 175 | ········protected virtual void Init(string mappingPath) |
| 176 | ········{ |
| 177 | ············if (!File.Exists(mappingPath)) |
| 178 | ················throw new NDOException(45, String.Format("Mapping File {0} doesn't exist.", mappingPath)); |
| 179 | |
| 180 | ············Init( new Mappings( mappingPath, ProviderFactory ) ); |
| 181 | ········} |
| 182 | |
| 183 | ········/// <summary> |
| 184 | ········/// Initializes the persistence manager |
| 185 | ········/// </summary> |
| 186 | ········/// <remarks> |
| 187 | ········/// Note: This is the method, which will be called from all different ways to instantiate a PersistenceManagerBase. |
| 188 | ········/// </remarks> |
| 189 | ········/// <param name="mappings"></param> |
| 190 | ········internal virtual void Init( Mappings mappings ) |
| 191 | ········{ |
| 192 | ············if (NDOApplication.ServiceProvider == null) |
| 193 | ················throw new Exception( "ServiceProvider is not initialized. Please setup a host environment. See BuilderExtensions.AddNdo and .UseNdo." ); |
| 194 | |
| 195 | ············Logger = NDOApplication.ServiceProvider.GetService<ILoggerFactory>()?.CreateLogger( GetType() ); |
| 196 | ············this.mappings = mappings; |
| 197 | |
| 198 | ············this.ds = new NDODataSet( this.mappings, ProviderFactory );··// Each PersistenceManager instance must have it's own DataSet. |
| 199 | ········} |
| 200 | |
| 201 | ········/// <summary> |
| 202 | ········/// Used by PersistenceManagers, to get an owner supplied id. |
| 203 | ········/// </summary> |
| 204 | ········/// <param name="t">Type of the object, the id is intended for.</param> |
| 205 | ········/// <param name="oid">ObjectId object which will hold the id value.</param> |
| 206 | ········protected void FireIdGenerationEvent(Type t, ObjectId oid) |
| 207 | ········{ |
| 208 | ············if (IdGenerationEvent != null) |
| 209 | ················IdGenerationEvent(t, oid); |
| 210 | ········} |
| 211 | |
| 212 | |
| 213 | ········Dictionary<string,Class> myClassesName; |
| 214 | ········Dictionary<Type, Class> myClassesType; |
| 215 | |
| 216 | ········/// <summary> |
| 217 | ········/// Initializes the class mappings |
| 218 | ········/// </summary> |
| 219 | ········protected void InitClasses() |
| 220 | ········{ |
| 221 | ············int cnt = mappings.Classes.Count(); |
| 222 | ············myClassesName = new Dictionary<string, Class>(cnt); |
| 223 | ············myClassesType = new Dictionary<Type, Class>(cnt); |
| 224 | ············foreach(Class cl in mappings.Classes) |
| 225 | ············{ |
| 226 | ················myClassesName.Add(cl.FullName, cl); |
| 227 | ················myClassesType.Add(cl.SystemType, cl); |
| 228 | ············} |
| 229 | ········} |
| 230 | ········ |
| 231 | |
| 232 | ········internal Class GetClass(string name) |
| 233 | ········{ |
| 234 | ············if (!myClassesName.ContainsKey(name)) |
| 235 | ················throw new NDOException(17, "Can't find mapping information for class " + name); |
| 236 | |
| 237 | ············return myClassesName[name]; |
| 238 | ········} |
| 239 | |
| 240 | ········internal Class GetClass(IPersistenceCapable pc) |
| 241 | ········{ |
| 242 | ············return GetClass(pc.GetType()); |
| 243 | ········} |
| 244 | |
| 245 | ········internal Class GetClass(Type type) |
| 246 | ········{ |
| 247 | ············Type t = type; |
| 248 | |
| 249 | ············if (type.IsGenericType) |
| 250 | ················t = type.GetGenericTypeDefinition(); |
| 251 | |
| 252 | ············if (! myClassesType.ContainsKey(t)) |
| 253 | ················throw new NDOException(17, "Can't find mapping information for class " + t.FullName); |
| 254 | |
| 255 | ············return myClassesType[t]; |
| 256 | |
| 257 | ········} |
| 258 | |
| 259 | ········internal Field GetField(Class cl, string field) |
| 260 | ········{ |
| 261 | ············Field f = cl.FindField(field); |
| 262 | ············if (f == null) |
| 263 | ················throw new NDOException(7, "Can't find mapping information for field " + cl.FullName + "." + field); |
| 264 | ············return f; |
| 265 | ········} |
| 266 | |
| 267 | ········/// <summary> |
| 268 | ········/// Hilfsfunktion |
| 269 | ········/// Liefert die Tabelle im DataSet ab, in der die DataRows des Datentyps liegen |
| 270 | ········/// </summary> |
| 271 | ········/// <param name="t">Data type</param> |
| 272 | ········/// <returns></returns> |
| 273 | ········protected DataTable GetTable(Type t) |
| 274 | ········{ |
| 275 | ············return GetTable(GetClass(t).TableName); |
| 276 | ········} |
| 277 | |
| 278 | ········/// <summary> |
| 279 | ········/// Hilfsfunktion |
| 280 | ········/// Liefert die Tabelle im DataSet ab, in der die DataRow des Objekts liegt |
| 281 | ········/// </summary> |
| 282 | ········/// <param name="pc"></param> |
| 283 | ········/// <returns></returns> |
| 284 | ········protected DataTable GetTable(IPersistenceCapable pc) |
| 285 | ········{ |
| 286 | ············return GetTable(GetClass(pc).TableName); |
| 287 | ········} |
| 288 | |
| 289 | |
| 290 | ········/// <summary> |
| 291 | ········/// Retrieve a table with the given name |
| 292 | ········/// </summary> |
| 293 | ········/// <param name="name">Table name</param> |
| 294 | ········/// <returns></returns> |
| 295 | ········protected DataTable GetTable(string name) |
| 296 | ········{ |
| 297 | ············DataTable dt = ds.Tables[name]; |
| 298 | ············if (dt == null) |
| 299 | ················throw new NDOException(39, "Can't find table '" + name + "' in the schema. Check your mapping file."); |
| 300 | ············return dt; |
| 301 | ········} |
| 302 | |
| 303 | ········/// <summary> |
| 304 | ········/// Gets a DataRow for a given object; if necessary the row will be constructed |
| 305 | ········/// </summary> |
| 306 | ········/// <param name="pc">Persistence capable object</param> |
| 307 | ········/// <returns></returns> |
| 308 | ········protected DataRow GetDataRow(IPersistenceCapable pc) |
| 309 | ········{ |
| 310 | ············DataRow row; |
| 311 | ············if ((row = this.cache.GetDataRow(pc)) != null) |
| 312 | ················return row; |
| 313 | ············return null; |
| 314 | ········} |
| 315 | |
| 316 | ········/// <summary> |
| 317 | ········/// Indicates, if there is a listener registered for the IdGenerationEvent. |
| 318 | ········/// </summary> |
| 319 | ········public bool HasOwnerCreatedIds |
| 320 | ········{ |
| 321 | ············get { return IdGenerationEvent != null; } |
| 322 | ········} |
| 323 | |
| 324 | ········/// <summary> |
| 325 | ········/// Gets the IServiceProvider instance of the current scope. |
| 326 | ········/// If no ServiceProvider was passed in the constructor, a new scope is created. |
| 327 | ········/// </summary> |
| 328 | ········internal IServiceProvider ServiceProvider |
| 329 | ········{ |
| 330 | ············get |
| 331 | ············{ |
| 332 | ················if (this.scopedServiceProvider == null) |
| 333 | ················{ |
| 334 | ····················this.scope = NDOApplication.ServiceProvider.CreateScope(); |
| 335 | ····················this.scopedServiceProvider = scope.ServiceProvider; |
| 336 | ················} |
| 337 | |
| 338 | ················return this.scopedServiceProvider; |
| 339 | ············} |
| 340 | ········} |
| 341 | |
| 342 | ········/// <summary> |
| 343 | ········/// Gets or sets an implementation of the PersistenceHandlerManager. |
| 344 | ········/// </summary> |
| 345 | ········public IPersistenceHandlerManager PersistenceHandlerManager |
| 346 | ········{ |
| 347 | ············get |
| 348 | ············{ |
| 349 | ················return this.persistenceHandlerManager = new NDOPersistenceHandlerManager(this.ServiceProvider, this.mappings); |
| 350 | ············} |
| 351 | ············set { this.persistenceHandlerManager = value; } |
| 352 | ········} |
| 353 | |
| 354 | ········ |
| 355 | ········/// <summary> |
| 356 | ········/// Gets the Mapping structure of the application as stored in NDOMapping.xml. |
| 357 | ········/// Use it only if you know exactly, what you're doing! |
| 358 | ········/// Do not change anything in the mapping structure because it will cause the |
| 359 | ········/// NDO Framework to fail. |
| 360 | ········/// </summary> |
| 361 | ········public NDOMapping NDOMapping |
| 362 | ········{ |
| 363 | ············get { return this.mappings; } |
| 364 | ········} |
| 365 | |
| 366 | ········/// <summary> |
| 367 | ········/// Gets the DataSet behind the operations of the pm. |
| 368 | ········/// </summary> |
| 369 | ········/// <remarks>This property should't be used by user code. It exists only for test purposes.</remarks> |
| 370 | ········public DataSet DataSet |
| 371 | ········{ |
| 372 | ············get { return this.ds; } |
| 373 | ········} |
| 374 | |
| 375 | ········internal NDO.Cache Cache |
| 376 | ········{ |
| 377 | ············get { return this.cache; } |
| 378 | ········} |
| 379 | |
| 380 | ········/// <summary> |
| 381 | ········/// Checks whether an object is an IPersistenceCapable and converts the object into an IPersistenceCapable. |
| 382 | ········/// </summary> |
| 383 | ········/// <param name="o"></param> |
| 384 | ········/// <returns></returns> |
| 385 | ········/// <remarks>Throws an NDOException, if the object can't be converted.</remarks> |
| 386 | ········protected internal IPersistenceCapable CheckPc(object o) |
| 387 | ········{ |
| 388 | ············IPersistenceCapable pc = o as IPersistenceCapable; |
| 389 | ············if (pc == null && !(o == null)) |
| 390 | ················throw new NDOException(31, "Parameter should implement IPersistenceCapable. Check, if the type " + o.GetType().FullName + "," + o.GetType().Assembly.FullName + " is enhanced."); |
| 391 | ············return pc; |
| 392 | ········} |
| 393 | |
| 394 | ········/// <summary> |
| 395 | ········/// Closes the PersistenceManager and releases all resources. |
| 396 | ········/// </summary> |
| 397 | ········public virtual void Close() |
| 398 | ········{ |
| 399 | ············if (isClosing) |
| 400 | ················return; |
| 401 | ············isClosing = true; |
| 402 | ············this.ds.Dispose(); |
| 403 | ············this.ds = null; |
| 404 | ············if (this.scope != null) |
| 405 | ················this.scope.Dispose(); |
| 406 | ············this.queryCache.Clear(); |
| 407 | ········} |
| 408 | |
| 409 | ········/// <summary> |
| 410 | ········/// IDisposable implementation. |
| 411 | ········/// </summary> |
| 412 | ········/// <remarks>Note: The derived classes don't need to override the Dispose methods, since Close() is virtual. Just override Close() and call base.Close() in the overridden version.</remarks> |
| 413 | ········/// <param name="disposing"></param> |
| 414 | ········protected virtual void Dispose(bool disposing) |
| 415 | ········{ |
| 416 | ············if (disposing) |
| 417 | ················Close(); |
| 418 | ········} |
| 419 | |
| 420 | ········/// <summary> |
| 421 | ········/// Disposes any Resources which might be held by the PersistenceManager implementation. |
| 422 | ········/// </summary> |
| 423 | ········public virtual void Dispose() |
| 424 | ········{ |
| 425 | ············Dispose( true ); |
| 426 | ············GC.SuppressFinalize( this ); |
| 427 | ········} |
| 428 | |
| 429 | ········/// <summary> |
| 430 | ········/// Finalizer. |
| 431 | ········/// </summary> |
| 432 | ········~PersistenceManagerBase() |
| 433 | ········{ |
| 434 | ············Dispose( false ); |
| 435 | ········} |
| 436 | ····} |
| 437 | } |
| 438 |
New Commit (d0706b7)
| 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 System.Linq; |
| 25 | using System.IO; |
| 26 | using System.Data; |
| 27 | using NDOInterfaces; |
| 28 | using NDO.Mapping; |
| 29 | using NDO.Application; |
| 30 | using NDO.SchemaGenerator; |
| 31 | using System.Reflection; |
| 32 | using Microsoft.Extensions.DependencyInjection; |
| 33 | using Microsoft.Extensions.Logging; |
| 34 | using NDO.ProviderFactory; |
| 35 | |
| 36 | namespace NDO |
| 37 | { |
| 38 | ····/// <summary> |
| 39 | ····/// Provides base functionality for PersistenceManager classes |
| 40 | ····/// </summary> |
| 41 | ····public class PersistenceManagerBase : IPersistenceManagerBase |
| 42 | ····{ |
| 43 | ········internal Cache cache = new Cache(); |
| 44 | ········/// <summary> |
| 45 | ········/// The DataSet used as template for DataRows |
| 46 | ········/// </summary> |
| 47 | ········protected DataSet ds = null; |
| 48 | ········/// <summary> |
| 49 | ········/// The StateManager instance which will be used for all objects |
| 50 | ········/// </summary> |
| 51 | ········protected IStateManager sm; |
| 52 | ········internal Mappings mappings;··// protected will make the compiler complaining |
| 53 | ········private IServiceProvider scopedServiceProvider; |
| 54 | ········private IServiceScope scope; |
| 55 | ········/// <summary> |
| 56 | ········/// Gets or sets the logger for the PersistenceManager instance |
| 57 | ········/// </summary> |
| 58 | ········protected ILogger Logger { get; set; } |
| 59 | ········bool isClosing = false; |
| 60 | ········private INDOProviderFactory providerFactory; |
| 61 | ········private NDOPersistenceHandlerManager persistenceHandlerManager; |
| 62 | ········/// <summary> |
| 63 | ········/// Gets the PersistenceHandlerManager instance |
| 64 | ········/// </summary> |
| 65 | ········public NDOPersistenceHandlerManager PersistenceHandlerManager => persistenceHandlerManager; |
| 66 | |
| 67 | ········/// <summary> |
| 68 | ········/// Provides the ProviderFactory to inherited persistence managers; |
| 69 | ········/// </summary> |
| 70 | ········protected INDOProviderFactory ProviderFactory |
| 71 | ········{ |
| 72 | ············get |
| 73 | ············{ |
| 74 | ················if (this.providerFactory == null) |
| 75 | ················{ |
| 76 | ····················var serviceProvider = NDOApplication.ServiceProvider; |
| 77 | |
| 78 | ····················if (serviceProvider != null) |
| 79 | ····················{ |
| 80 | ························this.providerFactory = serviceProvider.GetService<INDOProviderFactory>(); |
| 81 | ····················} |
| 82 | |
| 83 | ····················if (this.providerFactory == null) |
| 84 | ····················{ |
| 85 | ························this.providerFactory = NDOProviderFactory.Instance; |
| 86 | ····················} |
| 87 | ················} |
| 88 | |
| 89 | ················return this.providerFactory; |
| 90 | ············} |
| 91 | ········} |
| 92 | |
| 93 | |
| 94 | ········/// <summary> |
| 95 | ········/// If set to true, the resultset of each query is stored, so that results of an identical query |
| 96 | ········/// can be read from the cache. |
| 97 | ········/// </summary> |
| 98 | ········/// <remarks> |
| 99 | ········/// NDO computes an SHA256 hash for each query including the query parameters. The hash is the key to the query cache. |
| 100 | ········/// Do not use the QueryCache in applications, which work with one PersistenceManager |
| 101 | ········/// over a long time with multiple transactions. |
| 102 | ········/// </remarks> |
| 103 | ········public bool UseQueryCache { get; set; } |
| 104 | |
| 105 | ········private Dictionary<string, object> queryCache = new Dictionary<string, object>(); |
| 106 | |
| 107 | ········/// <summary> |
| 108 | ········/// This is the query cache in which resultsets can be stored. |
| 109 | ········/// </summary> |
| 110 | ········/// <remarks>Usually you won't have to access the query cache.</remarks> |
| 111 | ········public Dictionary<string, object> QueryCache => this.queryCache; |
| 112 | |
| 113 | ········/// <summary> |
| 114 | ········/// Register a listener to this event, if you have to provide user generated ids. |
| 115 | ········/// This event is usefull for databases, which doesn't provide auto-incremented ids, like the Oracle Db. |
| 116 | ········/// The event will be fired if a new id is needed. |
| 117 | ········/// </summary> |
| 118 | ········public event IdGenerationHandler IdGenerationEvent; |
| 119 | |
| 120 | ········/// <summary> |
| 121 | ········/// Constructor |
| 122 | ········/// </summary> |
| 123 | ········public PersistenceManagerBase( IServiceProvider scopedServiceProvider ) |
| 124 | ········{ |
| 125 | ············this.scopedServiceProvider = scopedServiceProvider; |
| 126 | ············string baseDir = AppDomain.CurrentDomain.BaseDirectory; |
| 127 | ············if (File.Exists( Path.Combine( baseDir, "Web.config" ) )) |
| 128 | ················baseDir = Path.Combine( baseDir, "bin" ); |
| 129 | ············var entryAssemblyName = Assembly.GetEntryAssembly()?.GetName()?.Name; |
| 130 | ············List<string> paths = new List<string>(); |
| 131 | ············if (entryAssemblyName != null) |
| 132 | ············paths.Add( Path.Combine( baseDir, $"{entryAssemblyName}.ndo.mapping" ) ); |
| 133 | ············paths.Add( Path.Combine( baseDir, "NDOMapping.xml" ) ); |
| 134 | ············ |
| 135 | ············bool found = false; |
| 136 | ············foreach (var path in paths) |
| 137 | ············{ |
| 138 | ················if (File.Exists(path)) |
| 139 | ················{ |
| 140 | ····················Init( path ); |
| 141 | ····················found = true; |
| 142 | ····················break; |
| 143 | ················} |
| 144 | ············} |
| 145 | ············if (!found) |
| 146 | ················throw new NDOException( 49, $"Can't determine the path to the mapping file. Tried the following locations:\n{string.Join("\n", paths)}\nPlease provide a mapping file path as argument to the PersistenceManager ctor." ); |
| 147 | ········} |
| 148 | |
| 149 | ········/// <summary> |
| 150 | ········/// Constructs a PersistenceManagerBase object using the path to a mapping file. |
| 151 | ········/// </summary> |
| 152 | ········/// <param name="scopedServiceProvider">An IServiceProvider instance, which represents a scope (e.g. a request in an AspNet application)</param> |
| 153 | ········/// <param name="pathToMappingFile"></param> |
| 154 | ········public PersistenceManagerBase(string pathToMappingFile, IServiceProvider scopedServiceProvider) |
| 155 | ········{ |
| 156 | ············this.scopedServiceProvider = scopedServiceProvider; |
| 157 | ············Init(pathToMappingFile); |
| 158 | ········} |
| 159 | |
| 160 | ········/// <summary> |
| 161 | ········/// Constructs a PersistenceManagerBase object using the mapping object. |
| 162 | ········/// </summary> |
| 163 | ········/// <param name="mapping"></param> |
| 164 | ········/// <param name="scopedServiceProvider">An IServiceProvider instance, which represents a scope (e.g. a request in an AspNet application)</param> |
| 165 | ········public PersistenceManagerBase(NDOMapping mapping, IServiceProvider scopedServiceProvider) |
| 166 | ········{ |
| 167 | ············this.scopedServiceProvider = scopedServiceProvider; |
| 168 | ············var localMappings = mapping as Mappings; |
| 169 | ············if (localMappings == null) |
| 170 | ················throw new ArgumentException( "The mapping must be constructed by a PersistenceManager", nameof( mapping ) ); |
| 171 | |
| 172 | ············Init( localMappings ); |
| 173 | ········} |
| 174 | |
| 175 | ········/// <summary> |
| 176 | ········/// Initializes a PersistenceManager using the path to a mapping file |
| 177 | ········/// </summary> |
| 178 | ········/// <param name="mappingPath"></param> |
| 179 | ········protected virtual void Init(string mappingPath) |
| 180 | ········{ |
| 181 | ············if (!File.Exists(mappingPath)) |
| 182 | ················throw new NDOException(45, String.Format("Mapping File {0} doesn't exist.", mappingPath)); |
| 183 | |
| 184 | ············Init( new Mappings( mappingPath, ProviderFactory ) ); |
| 185 | ········} |
| 186 | |
| 187 | ········/// <summary> |
| 188 | ········/// Initializes the persistence manager |
| 189 | ········/// </summary> |
| 190 | ········/// <remarks> |
| 191 | ········/// Note: This is the method, which will be called from all different ways to instantiate a PersistenceManagerBase. |
| 192 | ········/// </remarks> |
| 193 | ········/// <param name="mappings"></param> |
| 194 | ········internal virtual void Init( Mappings mappings ) |
| 195 | ········{ |
| 196 | ············if (NDOApplication.ServiceProvider == null) |
| 197 | ················throw new Exception( "ServiceProvider is not initialized. Please setup a host environment. See BuilderExtensions.AddNdo and .UseNdo." ); |
| 198 | |
| 199 | ············Logger = NDOApplication.ServiceProvider.GetService<ILoggerFactory>()?.CreateLogger( GetType() ); |
| 200 | ············this.mappings = mappings; |
| 201 | this. persistenceHandlerManager = new NDOPersistenceHandlerManager( this. ServiceProvider, this. mappings ) ; |
| 202 | ············this.ds = new NDODataSet( this.mappings, ProviderFactory );··// Each PersistenceManager instance must have it's own DataSet. |
| 203 | ········} |
| 204 | |
| 205 | ········/// <summary> |
| 206 | ········/// Used by PersistenceManagers, to get an owner supplied id. |
| 207 | ········/// </summary> |
| 208 | ········/// <param name="t">Type of the object, the id is intended for.</param> |
| 209 | ········/// <param name="oid">ObjectId object which will hold the id value.</param> |
| 210 | ········protected void FireIdGenerationEvent(Type t, ObjectId oid) |
| 211 | ········{ |
| 212 | ············if (IdGenerationEvent != null) |
| 213 | ················IdGenerationEvent(t, oid); |
| 214 | ········} |
| 215 | |
| 216 | |
| 217 | ········Dictionary<string,Class> myClassesName; |
| 218 | ········Dictionary<Type, Class> myClassesType; |
| 219 | |
| 220 | ········/// <summary> |
| 221 | ········/// Initializes the class mappings |
| 222 | ········/// </summary> |
| 223 | ········protected void InitClasses() |
| 224 | ········{ |
| 225 | ············int cnt = mappings.Classes.Count(); |
| 226 | ············myClassesName = new Dictionary<string, Class>(cnt); |
| 227 | ············myClassesType = new Dictionary<Type, Class>(cnt); |
| 228 | ············foreach(Class cl in mappings.Classes) |
| 229 | ············{ |
| 230 | ················myClassesName.Add(cl.FullName, cl); |
| 231 | ················myClassesType.Add(cl.SystemType, cl); |
| 232 | ············} |
| 233 | ········} |
| 234 | ········ |
| 235 | |
| 236 | ········internal Class GetClass(string name) |
| 237 | ········{ |
| 238 | ············if (!myClassesName.ContainsKey(name)) |
| 239 | ················throw new NDOException(17, "Can't find mapping information for class " + name); |
| 240 | |
| 241 | ············return myClassesName[name]; |
| 242 | ········} |
| 243 | |
| 244 | ········internal Class GetClass(IPersistenceCapable pc) |
| 245 | ········{ |
| 246 | ············return GetClass(pc.GetType()); |
| 247 | ········} |
| 248 | |
| 249 | ········internal Class GetClass(Type type) |
| 250 | ········{ |
| 251 | ············Type t = type; |
| 252 | |
| 253 | ············if (type.IsGenericType) |
| 254 | ················t = type.GetGenericTypeDefinition(); |
| 255 | |
| 256 | ············if (! myClassesType.ContainsKey(t)) |
| 257 | ················throw new NDOException(17, "Can't find mapping information for class " + t.FullName); |
| 258 | |
| 259 | ············return myClassesType[t]; |
| 260 | |
| 261 | ········} |
| 262 | |
| 263 | ········internal Field GetField(Class cl, string field) |
| 264 | ········{ |
| 265 | ············Field f = cl.FindField(field); |
| 266 | ············if (f == null) |
| 267 | ················throw new NDOException(7, "Can't find mapping information for field " + cl.FullName + "." + field); |
| 268 | ············return f; |
| 269 | ········} |
| 270 | |
| 271 | ········/// <summary> |
| 272 | ········/// Hilfsfunktion |
| 273 | ········/// Liefert die Tabelle im DataSet ab, in der die DataRows des Datentyps liegen |
| 274 | ········/// </summary> |
| 275 | ········/// <param name="t">Data type</param> |
| 276 | ········/// <returns></returns> |
| 277 | ········protected DataTable GetTable(Type t) |
| 278 | ········{ |
| 279 | ············return GetTable(GetClass(t).TableName); |
| 280 | ········} |
| 281 | |
| 282 | ········/// <summary> |
| 283 | ········/// Hilfsfunktion |
| 284 | ········/// Liefert die Tabelle im DataSet ab, in der die DataRow des Objekts liegt |
| 285 | ········/// </summary> |
| 286 | ········/// <param name="pc"></param> |
| 287 | ········/// <returns></returns> |
| 288 | ········protected DataTable GetTable(IPersistenceCapable pc) |
| 289 | ········{ |
| 290 | ············return GetTable(GetClass(pc).TableName); |
| 291 | ········} |
| 292 | |
| 293 | |
| 294 | ········/// <summary> |
| 295 | ········/// Retrieve a table with the given name |
| 296 | ········/// </summary> |
| 297 | ········/// <param name="name">Table name</param> |
| 298 | ········/// <returns></returns> |
| 299 | ········protected DataTable GetTable(string name) |
| 300 | ········{ |
| 301 | ············DataTable dt = ds.Tables[name]; |
| 302 | ············if (dt == null) |
| 303 | ················throw new NDOException(39, "Can't find table '" + name + "' in the schema. Check your mapping file."); |
| 304 | ············return dt; |
| 305 | ········} |
| 306 | |
| 307 | ········/// <summary> |
| 308 | ········/// Gets a DataRow for a given object; if necessary the row will be constructed |
| 309 | ········/// </summary> |
| 310 | ········/// <param name="pc">Persistence capable object</param> |
| 311 | ········/// <returns></returns> |
| 312 | ········protected DataRow GetDataRow(IPersistenceCapable pc) |
| 313 | ········{ |
| 314 | ············DataRow row; |
| 315 | ············if ((row = this.cache.GetDataRow(pc)) != null) |
| 316 | ················return row; |
| 317 | ············return null; |
| 318 | ········} |
| 319 | |
| 320 | ········/// <summary> |
| 321 | ········/// Indicates, if there is a listener registered for the IdGenerationEvent. |
| 322 | ········/// </summary> |
| 323 | ········public bool HasOwnerCreatedIds |
| 324 | ········{ |
| 325 | ············get { return IdGenerationEvent != null; } |
| 326 | ········} |
| 327 | |
| 328 | ········/// <summary> |
| 329 | ········/// Gets the IServiceProvider instance of the current scope. |
| 330 | ········/// If no ServiceProvider was passed in the constructor, a new scope is created. |
| 331 | ········/// </summary> |
| 332 | ········internal IServiceProvider ServiceProvider |
| 333 | ········{ |
| 334 | ············get |
| 335 | ············{ |
| 336 | ················if (this.scopedServiceProvider == null) |
| 337 | ················{ |
| 338 | ····················this.scope = NDOApplication.ServiceProvider.CreateScope(); |
| 339 | ····················this.scopedServiceProvider = scope.ServiceProvider; |
| 340 | ················} |
| 341 | |
| 342 | ················return this.scopedServiceProvider; |
| 343 | ············} |
| 344 | ········} |
| 345 | ········ |
| 346 | ········/// <summary> |
| 347 | ········/// Gets the Mapping structure of the application as stored in NDOMapping.xml. |
| 348 | ········/// Use it only if you know exactly, what you're doing! |
| 349 | ········/// Do not change anything in the mapping structure because it will cause the |
| 350 | ········/// NDO Framework to fail. |
| 351 | ········/// </summary> |
| 352 | ········public NDOMapping NDOMapping |
| 353 | ········{ |
| 354 | ············get { return this.mappings; } |
| 355 | ········} |
| 356 | |
| 357 | ········/// <summary> |
| 358 | ········/// Gets the DataSet behind the operations of the pm. |
| 359 | ········/// </summary> |
| 360 | ········/// <remarks>This property should't be used by user code. It exists only for test purposes.</remarks> |
| 361 | ········public DataSet DataSet |
| 362 | ········{ |
| 363 | ············get { return this.ds; } |
| 364 | ········} |
| 365 | |
| 366 | ········internal NDO.Cache Cache |
| 367 | ········{ |
| 368 | ············get { return this.cache; } |
| 369 | ········} |
| 370 | |
| 371 | ········/// <summary> |
| 372 | ········/// Checks whether an object is an IPersistenceCapable and converts the object into an IPersistenceCapable. |
| 373 | ········/// </summary> |
| 374 | ········/// <param name="o"></param> |
| 375 | ········/// <returns></returns> |
| 376 | ········/// <remarks>Throws an NDOException, if the object can't be converted.</remarks> |
| 377 | ········protected internal IPersistenceCapable CheckPc(object o) |
| 378 | ········{ |
| 379 | ············IPersistenceCapable pc = o as IPersistenceCapable; |
| 380 | ············if (pc == null && !(o == null)) |
| 381 | ················throw new NDOException(31, "Parameter should implement IPersistenceCapable. Check, if the type " + o.GetType().FullName + "," + o.GetType().Assembly.FullName + " is enhanced."); |
| 382 | ············return pc; |
| 383 | ········} |
| 384 | |
| 385 | ········/// <summary> |
| 386 | ········/// Closes the PersistenceManager and releases all resources. |
| 387 | ········/// </summary> |
| 388 | ········public virtual void Close() |
| 389 | ········{ |
| 390 | ············if (isClosing) |
| 391 | ················return; |
| 392 | ············isClosing = true; |
| 393 | ············this.ds.Dispose(); |
| 394 | ············this.ds = null; |
| 395 | ············if (this.scope != null) |
| 396 | ················this.scope.Dispose(); |
| 397 | ············this.queryCache.Clear(); |
| 398 | ········} |
| 399 | |
| 400 | ········/// <summary> |
| 401 | ········/// IDisposable implementation. |
| 402 | ········/// </summary> |
| 403 | ········/// <remarks>Note: The derived classes don't need to override the Dispose methods, since Close() is virtual. Just override Close() and call base.Close() in the overridden version.</remarks> |
| 404 | ········/// <param name="disposing"></param> |
| 405 | ········protected virtual void Dispose(bool disposing) |
| 406 | ········{ |
| 407 | ············if (disposing) |
| 408 | ················Close(); |
| 409 | ········} |
| 410 | |
| 411 | ········/// <summary> |
| 412 | ········/// Disposes any Resources which might be held by the PersistenceManager implementation. |
| 413 | ········/// </summary> |
| 414 | ········public virtual void Dispose() |
| 415 | ········{ |
| 416 | ············Dispose( true ); |
| 417 | ············GC.SuppressFinalize( this ); |
| 418 | ········} |
| 419 | |
| 420 | ········/// <summary> |
| 421 | ········/// Finalizer. |
| 422 | ········/// </summary> |
| 423 | ········~PersistenceManagerBase() |
| 424 | ········{ |
| 425 | ············Dispose( false ); |
| 426 | ········} |
| 427 | ····} |
| 428 | } |
| 429 |