/// <summary> /// Start the service and bind all of the sub-services /// </summary> public bool Start() { // Startup on system AuthenticationContext.Current = new AuthenticationContext(AuthenticationContext.SystemPrincipal); // notify startup this.Starting?.Invoke(this, EventArgs.Empty); if (this.m_running) return true; try { // Verify schema version using (DataContext mdc = this.GetConfiguration().Provider.GetReadonlyConnection()) { mdc.Open(); Version dbVer = new Version(mdc.FirstOrDefault<String>("get_sch_vrsn")), oizVer = typeof(AdoPersistenceService).Assembly.GetName().Version; if (oizVer < dbVer) throw new InvalidOperationException(String.Format("Invalid Schema Version. SanteDB version {0} is older than the database schema version {1}", oizVer, dbVer)); this.m_tracer.TraceInfo("SanteDB Schema Version {0} on {1}", dbVer, this.GetConfiguration().Provider.Invariant); } } catch (Exception e) { this.m_tracer.TraceEvent(EventLevel.Error, "Error starting ADO provider: {0}", e); throw; } // Iterate the persistence services foreach (var t in typeof(AdoPersistenceService).GetTypeInfo().Assembly.ExportedTypes.Where(o => o.Namespace == "SanteDB.Persistence.Data.ADO.Services.Persistence" && !o.GetTypeInfo().IsAbstract && !o.IsGenericTypeDefinition)) { try { this.m_tracer.TraceEvent(EventLevel.Informational, "Loading {0}...", t.AssemblyQualifiedName); // If the persistence service is generic then we should check if we're allowed if(!t.IsGenericType || t.IsGenericType && (this.GetConfiguration().AllowedResources.Count == 0 || this.GetConfiguration().AllowedResources.Contains(t.GetGenericArguments()[0].GetCustomAttribute<XmlTypeAttribute>()?.TypeName))) ApplicationServiceContext.Current.GetService<IServiceManager>().AddServiceProvider(t); // Add to cache since we're here anyways //s_persistenceCache.Add(t.GetGenericArguments()[0], Activator.CreateInstance(t) as IAdoPersistenceService); } catch (Exception e) { this.m_tracer.TraceEvent(EventLevel.Error, "Error adding service {0} : {1}", t.AssemblyQualifiedName, e); } } // Now iterate through the map file and ensure we have all the mappings, if a class does not exist create it try { this.m_tracer.TraceEvent(EventLevel.Informational, "Creating secondary model maps..."); var map = ModelMap.Load(typeof(AdoPersistenceService).GetTypeInfo().Assembly.GetManifestResourceStream(AdoDataConstants.MapResourceName)); foreach (var itm in map.Class) { // Is there a persistence service? var idpType = typeof(IDataPersistenceService<>); Type modelClassType = Type.GetType(itm.ModelClass), domainClassType = Type.GetType(itm.DomainClass); // Make sure we're allowed to run this if (this.GetConfiguration().AllowedResources.Count > 0 && !this.GetConfiguration().AllowedResources.Contains(modelClassType.GetCustomAttribute<XmlTypeAttribute>()?.TypeName)) continue; idpType = idpType.MakeGenericType(modelClassType); if (modelClassType.IsAbstract || domainClassType.IsAbstract) continue; // Already created if (ApplicationServiceContext.Current.GetService(idpType) != null) continue; this.m_tracer.TraceEvent(EventLevel.Verbose, "Creating map {0} > {1}", modelClassType, domainClassType); if (modelClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IBaseEntityData)) && domainClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IDbBaseData))) { // Construct a type Type pclass = null; if (modelClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IVersionedAssociation))) pclass = typeof(GenericBaseVersionedAssociationPersistenceService<,>); else if (modelClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(ISimpleAssociation))) pclass = typeof(GenericBaseAssociationPersistenceService<,>); else pclass = typeof(GenericBasePersistenceService<,>); pclass = pclass.MakeGenericType(modelClassType, domainClassType); ApplicationServiceContext.Current.GetService<IServiceManager>().AddServiceProvider(pclass); // Add to cache since we're here anyways this.m_persistenceCache.Add(modelClassType, Activator.CreateInstance(pclass) as IAdoPersistenceService); } else if (modelClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IIdentifiedEntity)) && domainClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IDbIdentified))) { // Construct a type Type pclass = null; if (modelClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IVersionedAssociation))) pclass = typeof(GenericIdentityVersionedAssociationPersistenceService<,>); else if (modelClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(ISimpleAssociation))) pclass = typeof(GenericIdentityAssociationPersistenceService<,>); else pclass = typeof(GenericIdentityPersistenceService<,>); pclass = pclass.MakeGenericType(modelClassType, domainClassType); ApplicationServiceContext.Current.GetService<IServiceManager>().AddServiceProvider(pclass); this.m_persistenceCache.Add(modelClassType, Activator.CreateInstance(pclass) as IAdoPersistenceService); } else this.m_tracer.TraceEvent(EventLevel.Warning, "Classmap {0}>{1} cannot be created, ignoring", modelClassType, domainClassType); } } catch (Exception e) { this.m_tracer.TraceEvent(EventLevel.Error, "Error initializing local persistence: {0}", e); throw e; } // Bind subscription execution ApplicationServiceContext.Current.GetService<IServiceManager>().AddServiceProvider(typeof(AdoSubscriptionExector)); // Bind BI stuff ApplicationServiceContext.Current.GetService<IBiMetadataRepository>()?.Insert(new SanteDB.BI.Model.BiDataSourceDefinition() { IsSystemObject = true, MetaData = new BiMetadata() { Version = typeof(AdoPersistenceService).Assembly.GetName().Version.ToString(), Status = BiDefinitionStatus.Active, }, Id = "org.santedb.bi.dataSource.main", Name = "main", ConnectionString = this.m_configuration.ReadonlyConnectionString, ProviderType = typeof(OrmBiDataProvider) }); // Bind some basic service stuff ApplicationServiceContext.Current.GetService<IDataPersistenceService<Core.Model.Security.SecurityUser>>().Inserting += (o, e) => { if (String.IsNullOrEmpty(e.Data.SecurityHash)) e.Data.SecurityHash = Guid.NewGuid().ToString(); }; ApplicationServiceContext.Current.GetService<IDataPersistenceService<Core.Model.Security.SecurityUser>>().Updating += (o, e) => { e.Data.SecurityHash = Guid.NewGuid().ToString(); }; // Unload configuration when app domain unloads ApplicationServiceContext.Current.Stopped += (o, e) => this.m_configuration = null; // Attempt to cache concepts this.m_tracer.TraceEvent(EventLevel.Verbose, "Caching concept dictionary..."); this.m_running = true; this.Started?.Invoke(this, EventArgs.Empty); return true; }
// Constructor public LocalPersistenceService() { var appSection = ApplicationContext.Current.Configuration.GetSection <ApplicationConfigurationSection>(); this.m_tracer.TraceInfo("Starting local persistence services..."); // Iterate the persistence services foreach (var t in typeof(LocalPersistenceService).GetTypeInfo().Assembly.ExportedTypes.Where(o => o.Namespace == "OpenIZ.Mobile.Core.Data.Persistence" && !o.GetTypeInfo().IsAbstract&& !o.GetTypeInfo().IsGenericTypeDefinition)) { try { this.m_tracer.TraceVerbose("Loading {0}...", t.AssemblyQualifiedName); appSection.Services.Add(Activator.CreateInstance(t)); } catch (Exception e) { this.m_tracer.TraceError("Error adding service {0} : {1}", t.AssemblyQualifiedName, e); } } // Now iterate through the map file and ensure we have all the mappings, if a class does not exist create it try { this.m_tracer.TraceVerbose("Creating secondary model maps..."); var map = ModelMap.Load(typeof(LocalPersistenceService).GetTypeInfo().Assembly.GetManifestResourceStream("OpenIZ.Mobile.Core.Data.Map.ModelMap.xml")); foreach (var itm in map.Class) { // Is there a persistence service? var idpType = typeof(IDataPersistenceService <>); Type modelClassType = Type.GetType(itm.ModelClass), domainClassType = Type.GetType(itm.DomainClass); idpType = idpType.MakeGenericType(modelClassType); // Already created if (appSection.Services.Any(o => idpType.GetTypeInfo().IsAssignableFrom(o.GetType().GetTypeInfo()))) { continue; } this.m_tracer.TraceVerbose("Creating map {0} > {1}", modelClassType, domainClassType); // Is the model class a Versioned entity? if (modelClassType.GetRuntimeProperty("VersionKey") != null && typeof(DbVersionedData).GetTypeInfo().IsAssignableFrom(domainClassType.GetTypeInfo())) { // Construct a type var pclass = typeof(GenericVersionedPersistenceService <,>); pclass = pclass.MakeGenericType(modelClassType, domainClassType); appSection.Services.Add(Activator.CreateInstance(pclass)); } else if (modelClassType.GetRuntimeProperty("CreatedByKey") != null && typeof(DbBaseData).GetTypeInfo().IsAssignableFrom(domainClassType.GetTypeInfo())) { // Construct a type var pclass = typeof(GenericBasePersistenceService <,>); pclass = pclass.MakeGenericType(modelClassType, domainClassType); appSection.Services.Add(Activator.CreateInstance(pclass)); } else { // Construct a type Type pclass = null; if (modelClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IVersionedAssociation))) { pclass = typeof(GenericIdentityAssociationPersistenceService <,>); } else if (modelClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(ISimpleAssociation))) { pclass = typeof(GenericIdentityAssociationPersistenceService <,>); } else { pclass = typeof(GenericIdentityPersistenceService <,>); } pclass = pclass.MakeGenericType(modelClassType, domainClassType); appSection.Services.Add(Activator.CreateInstance(pclass)); } } } catch (Exception e) { this.m_tracer.TraceError("Error initializing local persistence: {0}", e); throw e; } }
/// <summary> /// Start the service and bind all of the sub-services /// </summary> public bool Start() { // notify startup this.Starting?.Invoke(this, EventArgs.Empty); if (this.m_running) { return(true); } // Verify schema version using (ModelDataContext mdc = new ModelDataContext(this.m_configuration.ReadonlyConnectionString)) { Version dbVer = new Version(mdc.fn_OpenIzSchemaVersion()), oizVer = typeof(SqlServerPersistenceService).Assembly.GetName().Version; if (oizVer < dbVer) { throw new InvalidOperationException(String.Format("Invalid Schema Version. OpenIZ version {0} is older than the database schema version {1}", oizVer, dbVer)); } this.m_tracer.TraceInformation("OpenIZ Schema Version {0}", dbVer); } // Iterate the persistence services foreach (var t in typeof(SqlServerPersistenceService).GetTypeInfo().Assembly.ExportedTypes.Where(o => o.Namespace == "OpenIZ.Persistence.Data.MSSQL.Services.Persistence" && !o.GetTypeInfo().IsAbstract)) { try { this.m_tracer.TraceEvent(TraceEventType.Verbose, 0, "Loading {0}...", t.AssemblyQualifiedName); ApplicationContext.Current.AddServiceProvider(t); } catch (Exception e) { this.m_tracer.TraceEvent(TraceEventType.Error, e.HResult, "Error adding service {0} : {1}", t.AssemblyQualifiedName, e); } } // Now iterate through the map file and ensure we have all the mappings, if a class does not exist create it try { this.m_tracer.TraceEvent(TraceEventType.Verbose, 0, "Creating secondary model maps..."); var map = ModelMap.Load(typeof(SqlServerPersistenceService).GetTypeInfo().Assembly.GetManifestResourceStream("OpenIZ.Persistence.Data.MSSQL.Data.ModelMap.xml")); foreach (var itm in map.Class) { // Is there a persistence service? var idpType = typeof(IDataPersistenceService <>); Type modelClassType = Type.GetType(itm.ModelClass), domainClassType = Type.GetType(itm.DomainClass); idpType = idpType.MakeGenericType(modelClassType); if (modelClassType.IsAbstract || domainClassType.IsAbstract) { continue; } // Already created if (ApplicationContext.Current.GetService(idpType) != null) { continue; } this.m_tracer.TraceEvent(TraceEventType.Verbose, 0, "Creating map {0} > {1}", modelClassType, domainClassType); if (modelClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IBaseEntityData)) && domainClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IDbBaseData))) { // Construct a type var pclass = typeof(GenericBasePersistenceService <,>); pclass = pclass.MakeGenericType(modelClassType, domainClassType); ApplicationContext.Current.AddServiceProvider(pclass); } else if (modelClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IIdentifiedEntity)) && domainClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IDbIdentified))) { // Construct a type var pclass = typeof(GenericIdentityPersistenceService <,>); pclass = pclass.MakeGenericType(modelClassType, domainClassType); ApplicationContext.Current.AddServiceProvider(pclass); } } } catch (Exception e) { this.m_tracer.TraceEvent(TraceEventType.Error, e.HResult, "Error initializing local persistence: {0}", e); throw e; } // Bind some basic service stuff ApplicationContext.Current.GetService <IDataPersistenceService <Core.Model.Security.SecurityUser> >().Inserting += (o, e) => { if (String.IsNullOrEmpty(e.Data.SecurityHash)) { e.Data.SecurityHash = Guid.NewGuid().ToString(); } }; ApplicationContext.Current.GetService <IDataPersistenceService <Core.Model.Security.SecurityUser> >().Updating += (o, e) => { e.Data.SecurityHash = Guid.NewGuid().ToString(); }; // Attempt to cache concepts this.m_tracer.TraceEvent(TraceEventType.Verbose, 0, "Caching concept dictionary..."); if (ApplicationContext.Current.GetService <IDataCachingService>() != null) { new Thread((o) => { int t; ApplicationContext.Current.GetService <IDataPersistenceService <Core.Model.DataTypes.Concept> >().Query(c => c.Key == c.Key, 0, 10000, null, out t); ApplicationContext.Current.GetService <IDataPersistenceService <Core.Model.DataTypes.ConceptSet> >().Query(c => c.Key == c.Key, 0, 1000, null, out t); }).Start(); } this.m_running = true; this.Started?.Invoke(this, EventArgs.Empty); return(true); }
/// <summary> /// Try to create service <paramref name="serviceType"/> /// </summary> public bool TryCreateService(Type serviceType, out object serviceInstance) { // Look for concrete services var st = AppDomain.CurrentDomain.GetAllTypes().FirstOrDefault(o => typeof(IAdoPersistenceService).IsAssignableFrom(o) && serviceType.IsAssignableFrom(o)); if (st != null) { var adoManager = this.m_serviceManager.GetService(typeof(AdoPersistenceService)); serviceInstance = Activator.CreateInstance(st, adoManager); return(true); } else if (st == null && serviceType.IsGenericType && (serviceType.GetGenericTypeDefinition() == typeof(IDataPersistenceService <>) || typeof(IDataPersistenceService).IsAssignableFrom(serviceType))) { var adoManager = this.m_serviceManager.GetService(typeof(AdoPersistenceService)); serviceInstance = null; var wrappedType = serviceType.GetGenericArguments()[0]; var map = ModelMap.Load(typeof(AdoPersistenceService).Assembly.GetManifestResourceStream(AdoDataConstants.MapResourceName)); var classMapData = map.Class.FirstOrDefault(m => m.ModelType == wrappedType); if (classMapData != null) // We have a map!!! { // Is there a persistence service? var idpType = typeof(IDataPersistenceService <>); Type modelClassType = Type.GetType(classMapData.ModelClass), domainClassType = Type.GetType(classMapData.DomainClass); idpType = idpType.MakeGenericType(modelClassType); if (modelClassType.IsAbstract || domainClassType.IsAbstract) { return(false); } this.m_tracer.TraceVerbose("Creating map {0} > {1}", modelClassType, domainClassType); if (this.m_persistenceCache.ContainsKey(modelClassType)) { this.m_tracer.TraceWarning("Duplicate initialization of {0}", modelClassType); } else if (modelClassType.Implements(typeof(IBaseEntityData)) && domainClassType.Implements(typeof(IDbBaseData))) { // Construct a type Type pclass = null; if (modelClassType.Implements(typeof(IVersionedAssociation))) { pclass = typeof(GenericBaseVersionedAssociationPersistenceService <,>); } else if (modelClassType.Implements(typeof(ISimpleAssociation))) { pclass = typeof(GenericBaseAssociationPersistenceService <,>); } else { pclass = typeof(GenericBasePersistenceService <,>); } pclass = pclass.MakeGenericType(modelClassType, domainClassType); serviceInstance = Activator.CreateInstance(pclass, adoManager); // Add to cache since we're here anyways this.m_persistenceCache.Add(modelClassType, serviceInstance as IAdoPersistenceService); return(true); } else if (modelClassType.Implements(typeof(IIdentifiedEntity)) && domainClassType.Implements(typeof(IDbIdentified))) { // Construct a type Type pclass = null; if (modelClassType.Implements(typeof(IVersionedAssociation))) { pclass = typeof(GenericIdentityVersionedAssociationPersistenceService <,>); } else if (modelClassType.Implements(typeof(ISimpleAssociation))) { pclass = typeof(GenericIdentityAssociationPersistenceService <,>); } else { pclass = typeof(GenericIdentityPersistenceService <,>); } pclass = pclass.MakeGenericType(modelClassType, domainClassType); serviceInstance = Activator.CreateInstance(pclass, adoManager); this.m_persistenceCache.Add(modelClassType, serviceInstance as IAdoPersistenceService); return(true); } else { this.m_tracer.TraceWarning("Classmap {0}>{1} cannot be created, ignoring", modelClassType, domainClassType); return(false); } } } serviceInstance = null; return(false); }
/// <summary> /// Construct persistence service for SQLite and register subordinate services /// </summary> public SQLitePersistenceService() { this.m_tracer.TraceInfo("Starting local persistence services..."); // Iterate the persistence services foreach (var t in typeof(SQLitePersistenceService).GetTypeInfo().Assembly.ExportedTypes.Where(o => o.Namespace == "SanteDB.DisconnectedClient.SQLite.Persistence" && !o.GetTypeInfo().IsAbstract&& !o.GetTypeInfo().IsGenericTypeDefinition)) { try { this.m_tracer.TraceVerbose("Loading {0}...", t.AssemblyQualifiedName); ApplicationServiceContext.Current.GetService <IServiceManager>().AddServiceProvider(t); } catch (Exception e) { this.m_tracer.TraceError("Error adding service {0} : {1}", t.AssemblyQualifiedName, e); } } // Now iterate through the map file and ensure we have all the mappings, if a class does not exist create it try { this.m_tracer.TraceVerbose("Creating secondary model maps..."); var map = ModelMap.Load(typeof(SQLitePersistenceService).GetTypeInfo().Assembly.GetManifestResourceStream("SanteDB.DisconnectedClient.SQLite.Map.ModelMap.xml")); foreach (var itm in map.Class) { // Is there a persistence service? var idpType = typeof(IDataPersistenceService <>); Type modelClassType = Type.GetType(itm.ModelClass), domainClassType = Type.GetType(itm.DomainClass); idpType = idpType.MakeGenericType(modelClassType); // Already created if (ApplicationContext.Current.GetService(idpType) != null) { continue; } this.m_tracer.TraceVerbose("Creating map {0} > {1}", modelClassType, domainClassType); // Is the model class a Versioned entity? if (modelClassType.GetRuntimeProperty("VersionKey") != null && typeof(DbVersionedData).GetTypeInfo().IsAssignableFrom(domainClassType.GetTypeInfo())) { // Construct a type var pclass = typeof(GenericVersionedPersistenceService <,>); pclass = pclass.MakeGenericType(modelClassType, domainClassType); ApplicationServiceContext.Current.GetService <IServiceManager>().AddServiceProvider(pclass); } else if (modelClassType.GetRuntimeProperty("CreatedByKey") != null && typeof(DbBaseData).GetTypeInfo().IsAssignableFrom(domainClassType.GetTypeInfo())) { // Construct a type var pclass = typeof(GenericBasePersistenceService <,>); pclass = pclass.MakeGenericType(modelClassType, domainClassType); ApplicationServiceContext.Current.GetService <IServiceManager>().AddServiceProvider(pclass); } else { // Construct a type Type pclass = null; if (modelClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IVersionedAssociation))) { pclass = typeof(GenericIdentityAssociationPersistenceService <,>); } else if (modelClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(ISimpleAssociation))) { pclass = typeof(GenericIdentityAssociationPersistenceService <,>); } else { pclass = typeof(GenericIdentityPersistenceService <,>); } pclass = pclass.MakeGenericType(modelClassType, domainClassType); ApplicationServiceContext.Current.GetService <IServiceManager>().AddServiceProvider(pclass); } } } catch (Exception e) { this.m_tracer.TraceError("Error initializing local persistence: {0}", e); throw; } ApplicationServiceContext.Current.Started += (o, e) => { // TODO: Subscriptions on SQLite //ApplicationServiceContext.Current.GetService<IServiceManager>().AddServiceProvider(typeof(AdoSubscriptionExector)); // Bind BI stuff ApplicationServiceContext.Current.GetService <IBiMetadataRepository>()?.Insert(new SanteDB.BI.Model.BiDataSourceDefinition() { MetaData = new BiMetadata() { Version = typeof(SQLitePersistenceService).GetTypeInfo().Assembly.GetName().Version.ToString(), Status = BiDefinitionStatus.Active, }, Id = "org.santedb.bi.dataSource.main", Name = "main", ConnectionString = m_configuration.MainDataSourceConnectionStringName, ProviderType = typeof(SQLiteBiDataSource), IsSystemObject = true }); }; }
/// <summary> /// Starts the service which scans this ASM for persistence and adds any missing persisters to the application context /// </summary> /// <returns>True if the service was started successfully</returns> public bool Start() { if (this.IsRunning) { return(true); /// Already registered } this.Starting?.Invoke(this, EventArgs.Empty); // Iterate the persistence services foreach (var t in typeof(AdoAuditPersistenceService).Assembly.ExportedTypes.Where(o => typeof(IAdoPersistenceService).IsAssignableFrom(o) && !o.GetTypeInfo().IsAbstract&& !o.IsGenericTypeDefinition)) { try { s_tracer.TraceInfo("Loading {0}...", t.AssemblyQualifiedName); (ApplicationServiceContext.Current as IServiceManager).AddServiceProvider(t); } catch (Exception e) { s_tracer.TraceError("Error adding service {0} : {1}", t.AssemblyQualifiedName, e); } } // Now iterate through the map file and ensure we have all the mappings, if a class does not exist create it try { s_tracer.TraceInfo("Creating secondary model maps..."); var map = ModelMap.Load(typeof(AdoAuditPersistenceService).Assembly.GetManifestResourceStream("SanteGuard.Persistence.Ado.Data.Map.ModelMap.xml")); foreach (var itm in map.Class) { // Is there a persistence service? var idpType = typeof(IDataPersistenceService <>); Type modelClassType = Type.GetType(itm.ModelClass), domainClassType = Type.GetType(itm.DomainClass); idpType = idpType.MakeGenericType(modelClassType); if (modelClassType.IsAbstract || domainClassType.IsAbstract) { continue; } // Already created if (ApplicationServiceContext.Current.GetService(idpType) != null) { continue; } s_tracer.TraceVerbose("Creating map {0} > {1}", modelClassType, domainClassType); if (modelClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IBaseEntityData)) && domainClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IDbBaseData))) { // Construct a type Type pclass = null; if (modelClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IVersionedAssociation))) { pclass = typeof(GenericBaseVersionedAssociationPersistenceService <,>); } else if (modelClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(ISimpleAssociation))) { pclass = typeof(GenericBaseAssociationPersistenceService <,>); } else { pclass = typeof(GenericBasePersistenceService <,>); } pclass = pclass.MakeGenericType(modelClassType, domainClassType); (ApplicationServiceContext.Current as IServiceManager).AddServiceProvider(pclass); // Add to cache since we're here anyways s_persistenceCache.Add(modelClassType, Activator.CreateInstance(pclass) as IAdoPersistenceService); } else if (modelClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IIdentifiedEntity)) && domainClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IDbIdentified))) { // Construct a type Type pclass = null; if (modelClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IVersionedAssociation))) { pclass = typeof(GenericIdentityVersionedAssociationPersistenceService <,>); } else if (modelClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(ISimpleAssociation))) { pclass = typeof(GenericIdentityAssociationPersistenceService <,>); } else { pclass = typeof(GenericIdentityPersistenceService <,>); } pclass = pclass.MakeGenericType(modelClassType, domainClassType); (ApplicationServiceContext.Current as IServiceManager).AddServiceProvider(pclass); s_persistenceCache.Add(modelClassType, Activator.CreateInstance(pclass) as IAdoPersistenceService); } else { s_tracer.TraceWarning("Classmap {0} > {1} cannot be created, ignoring", modelClassType, domainClassType); } } } catch (Exception e) { s_tracer.TraceError("Error initializing local persistence: {0}", e); throw e; } this.Started?.Invoke(this, EventArgs.Empty); return(true); }
/// <summary> /// Start the service and bind all of the sub-services /// </summary> public bool Start() { // notify startup this.Starting?.Invoke(this, EventArgs.Empty); if (this.m_running) { return(true); } try { // Verify schema version using (DataContext mdc = s_configuration.Provider.GetReadonlyConnection()) { mdc.Open(); Version dbVer = new Version(mdc.FirstOrDefault <String>("get_sch_vrsn")), oizVer = typeof(AdoPersistenceService).Assembly.GetName().Version; if (oizVer < dbVer) { throw new InvalidOperationException(String.Format("Invalid Schema Version. OpenIZ version {0} is older than the database schema version {1}", oizVer, dbVer)); } this.m_tracer.TraceInformation("OpenIZ Schema Version {0}", dbVer); } } catch (Exception e) { this.m_tracer.TraceEvent(TraceEventType.Error, e.HResult, "Error starting ADO provider: {0}", e); throw; } // Iterate the persistence services foreach (var t in typeof(AdoPersistenceService).GetTypeInfo().Assembly.ExportedTypes.Where(o => o.Namespace == "OpenIZ.Persistence.Data.ADO.Services.Persistence" && !o.GetTypeInfo().IsAbstract&& !o.IsGenericTypeDefinition)) { try { this.m_tracer.TraceEvent(TraceEventType.Information, 0, "Loading {0}...", t.AssemblyQualifiedName); ApplicationContext.Current.AddServiceProvider(t); // Add to cache since we're here anyways //s_persistenceCache.Add(t.GetGenericArguments()[0], Activator.CreateInstance(t) as IAdoPersistenceService); } catch (Exception e) { this.m_tracer.TraceEvent(TraceEventType.Error, e.HResult, "Error adding service {0} : {1}", t.AssemblyQualifiedName, e); } } // Now iterate through the map file and ensure we have all the mappings, if a class does not exist create it try { this.m_tracer.TraceEvent(TraceEventType.Information, 0, "Creating secondary model maps..."); var map = ModelMap.Load(typeof(AdoPersistenceService).GetTypeInfo().Assembly.GetManifestResourceStream(AdoDataConstants.MapResourceName)); foreach (var itm in map.Class) { // Is there a persistence service? var idpType = typeof(IDataPersistenceService <>); Type modelClassType = Type.GetType(itm.ModelClass), domainClassType = Type.GetType(itm.DomainClass); idpType = idpType.MakeGenericType(modelClassType); if (modelClassType.IsAbstract || domainClassType.IsAbstract) { continue; } // Already created if (ApplicationContext.Current.GetService(idpType) != null) { continue; } this.m_tracer.TraceEvent(TraceEventType.Verbose, 0, "Creating map {0} > {1}", modelClassType, domainClassType); if (modelClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IBaseEntityData)) && domainClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IDbBaseData))) { // Construct a type Type pclass = null; if (modelClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IVersionedAssociation))) { pclass = typeof(GenericBaseVersionedAssociationPersistenceService <,>); } else if (modelClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(ISimpleAssociation))) { pclass = typeof(GenericBaseAssociationPersistenceService <,>); } else { pclass = typeof(GenericBasePersistenceService <,>); } pclass = pclass.MakeGenericType(modelClassType, domainClassType); ApplicationContext.Current.AddServiceProvider(pclass); // Add to cache since we're here anyways s_persistenceCache.Add(modelClassType, Activator.CreateInstance(pclass) as IAdoPersistenceService); } else if (modelClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IIdentifiedEntity)) && domainClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IDbIdentified))) { // Construct a type Type pclass = null; if (modelClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IVersionedAssociation))) { pclass = typeof(GenericIdentityVersionedAssociationPersistenceService <,>); } else if (modelClassType.GetTypeInfo().ImplementedInterfaces.Contains(typeof(ISimpleAssociation))) { pclass = typeof(GenericIdentityAssociationPersistenceService <,>); } else { pclass = typeof(GenericIdentityPersistenceService <,>); } pclass = pclass.MakeGenericType(modelClassType, domainClassType); ApplicationContext.Current.AddServiceProvider(pclass); s_persistenceCache.Add(modelClassType, Activator.CreateInstance(pclass) as IAdoPersistenceService); } else { this.m_tracer.TraceEvent(TraceEventType.Warning, 0, "Classmap {0}>{1} cannot be created, ignoring", modelClassType, domainClassType); } } } catch (Exception e) { this.m_tracer.TraceEvent(TraceEventType.Error, e.HResult, "Error initializing local persistence: {0}", e); throw e; } // Bind some basic service stuff ApplicationContext.Current.GetService <IDataPersistenceService <Core.Model.Security.SecurityUser> >().Inserting += (o, e) => { if (String.IsNullOrEmpty(e.Data.SecurityHash)) { e.Data.SecurityHash = Guid.NewGuid().ToString(); } }; ApplicationContext.Current.GetService <IDataPersistenceService <Core.Model.Security.SecurityUser> >().Updating += (o, e) => { e.Data.SecurityHash = Guid.NewGuid().ToString(); }; // Attempt to cache concepts this.m_tracer.TraceEvent(TraceEventType.Verbose, 0, "Caching concept dictionary..."); this.m_running = true; this.Started?.Invoke(this, EventArgs.Empty); return(true); }