public Type GetType(string fullName) { Type compiledType = CodeGenerationManager.GetCompiledType(fullName); if (compiledType != null) { return(compiledType); } string name = fullName; bool hasObsoletePrefix = name.StartsWith(_prefix); if (hasObsoletePrefix) { name = name.Substring(_prefix.Length); } if (!name.Contains(",") && (hasObsoletePrefix || !HostingEnvironment.IsHosted)) { return(Type.GetType(name + ", Composite.Generated")); } return(null); }
/// <summary> /// This method will return type given by the dataTypeDescriptor. /// If the data type does not exist, one will be dynamically /// runtime code generated. /// </summary> /// <param name="dataTypeDescriptor"></param> /// <param name="forceReCompilation">If this is true a new type will be compiled regardless if one already exists.</param> /// <returns></returns> public static Type GetType(DataTypeDescriptor dataTypeDescriptor, bool forceReCompilation = false) { bool codeGenerationNeeded; Type type = TryGetType(dataTypeDescriptor, forceReCompilation, out codeGenerationNeeded); if (type != null) { return(type); } if (codeGenerationNeeded) { lock (_lock) { type = TypeManager.TryGetType(dataTypeDescriptor.GetFullInterfaceName()); if (type != null) { return(type); } var codeGenerationBuilder = new CodeGenerationBuilder("DataInterface: " + dataTypeDescriptor.Name); InterfaceCodeGenerator.AddAssemblyReferences(codeGenerationBuilder, dataTypeDescriptor); InterfaceCodeGenerator.AddInterfaceTypeCode(codeGenerationBuilder, dataTypeDescriptor); IEnumerable <Type> types = CodeGenerationManager.CompileRuntimeTempTypes(codeGenerationBuilder); return(types.Single()); } } return(null); }
public void GenerateNewTypes(string providerName, IReadOnlyCollection <DataTypeDescriptor> dataTypeDescriptors, bool makeAFlush) { Verify.ArgumentNotNullOrEmpty(providerName, "providerName"); Verify.ArgumentNotNull(dataTypeDescriptors, "dataTypeDescriptors"); foreach (var dataTypeDescriptor in dataTypeDescriptors) { UpdateWithNewMetaDataForeignKeySystem(dataTypeDescriptor); UpdateWithNewPageFolderForeignKeySystem(dataTypeDescriptor, false); } var types = DataTypeTypesManager.GetDataTypes(dataTypeDescriptors); foreach (var dataTypeDescriptor in dataTypeDescriptors) { dataTypeDescriptor.TypeManagerTypeName = TypeManager.SerializeType(types[dataTypeDescriptor.DataTypeId]); } DynamicTypeManager.CreateStores(providerName, dataTypeDescriptors, makeAFlush); if (makeAFlush && dataTypeDescriptors.Any(d => d.IsCodeGenerated)) { CodeGenerationManager.GenerateCompositeGeneratedAssembly(true); } }
public static Type GetDataWrapperType(DataTypeDescriptor dataTypeDescriptor) { Type wrapperType = TryGetWrapperType(dataTypeDescriptor.GetFullInterfaceName()); if (wrapperType != null) { return(wrapperType); } lock (_compilationLock) { wrapperType = TryGetWrapperType(dataTypeDescriptor.GetFullInterfaceName()); if (wrapperType != null) { return(wrapperType); } var codeGenerationBuilder = new CodeGenerationBuilder("DataWrapper:" + dataTypeDescriptor.GetFullInterfaceName()); DataWrapperCodeGenerator.AddDataWrapperClassCode(codeGenerationBuilder, dataTypeDescriptor); IEnumerable <Type> types = CodeGenerationManager.CompileRuntimeTempTypes(codeGenerationBuilder); return(types.Single()); } }
/// <summary> /// For internal use only!!! /// This method will create the store if the interfaceType has not been configured. /// </summary> /// <param name="interfaceType"></param> /// <param name="providerName"></param> // Helper public static void EnsureCreateStore(Type interfaceType, string providerName) { IEnumerable <string> dynamicProviderNames; if (providerName == null) { // Checking if any of existing dynamic data providers already has a store for the specified interface type providerName = DataProviderRegistry.DefaultDynamicTypeDataProviderName; dynamicProviderNames = DataProviderRegistry.DynamicDataProviderNames; } else { dynamicProviderNames = new[] { providerName }; } if (dynamicProviderNames .Select(DataProviderPluginFacade.GetDataProvider) .Cast <IDynamicDataProvider>() .Any(dynamicDataProvider => dynamicDataProvider.GetKnownInterfaces().Contains(interfaceType))) { return; } var dataTypeDescriptor = BuildNewDataTypeDescriptor(interfaceType); CreateStore(providerName, dataTypeDescriptor, true); CodeGenerationManager.GenerateCompositeGeneratedAssembly(true); }
private void CompileMissingTypes(IList <GeneratedTypesInfo> typesInfo) { // Compiling missing classes if (typesInfo.Any(s => s.CompilationNeeded)) { var codeGenerationBuilder = new CodeGenerationBuilder(_dataProviderContext.ProviderName + ":DataId and helper classes"); var codeBuilder = new XmlDataProviderCodeBuilder(_dataProviderContext.ProviderName, codeGenerationBuilder); foreach (var storeToLoad in typesInfo.Where(s => s.CompilationNeeded)) { codeBuilder.AddDataType(storeToLoad.DataTypeDescriptor); // Compiling some other classes for optimization DataWrapperCodeGenerator.AddDataWrapperClassCode(codeGenerationBuilder, storeToLoad.DataTypeDescriptor); EmptyDataClassCodeGenerator.AddEmptyDataClassTypeCode(codeGenerationBuilder, storeToLoad.DataTypeDescriptor); } var types = CodeGenerationManager.CompileRuntimeTempTypes(codeGenerationBuilder, false).ToDictionary(type => type.FullName); foreach (var storeToLoad in typesInfo.Where(s => s.CompilationNeeded)) { storeToLoad.DataIdClass = types[storeToLoad.DataIdClassName]; storeToLoad.DataProviderHelperClass = types[storeToLoad.DataProviderHelperClassName]; } } }
public static Type GetDataWrapperType(Type interfaceType) { return(_dataWrappersCache.GetOrAdd(interfaceType, type => { Type wrapperType = TryGetWrapperType(type.FullName); if (wrapperType != null) { return wrapperType; } lock (_compilationLock) { wrapperType = TryGetWrapperType(type.FullName); if (wrapperType != null) { return wrapperType; } var codeGenerationBuilder = new CodeGenerationBuilder("DataWrapper:" + type.FullName); DataWrapperCodeGenerator.AddDataWrapperClassCode(codeGenerationBuilder, type); IEnumerable <Type> types = CodeGenerationManager.CompileRuntimeTempTypes(codeGenerationBuilder); return types.Single(); } })); }
public static Type GetDataWrapperType(Type interfaceType) { Type wrapperType = TryGetWrapperType(interfaceType.FullName); if (wrapperType != null) { return(wrapperType); } lock (_lock) { wrapperType = TryGetWrapperType(interfaceType.FullName); if (wrapperType != null) { return(wrapperType); } var codeGenerationBuilder = new CodeGenerationBuilder("DataWrapper:" + interfaceType.FullName); DataWrapperCodeGenerator.AddDataWrapperClassCode(codeGenerationBuilder, interfaceType); IEnumerable <Type> types = CodeGenerationManager.CompileRuntimeTempTypes(codeGenerationBuilder); return(types.Single()); } }
private static Type CodeGeneratePropertySerializer(Type propertyClassType) { CodeGenerationBuilder codeGenerationBuilder = new CodeGenerationBuilder("PropertySerializer: " + propertyClassType.FullName); PropertySerializerTypeCodeGenerator.AddPropertySerializerTypeCode(codeGenerationBuilder, propertyClassType); IEnumerable <Type> types = CodeGenerationManager.CompileRuntimeTempTypes(codeGenerationBuilder); return(types.Single()); }
internal static Type CreateEmptyDataClassType(DataTypeDescriptor dataTypeDescriptor, Type baseClassType = null, CodeAttributeDeclaration codeAttributeDeclaration = null) { CodeGenerationBuilder codeGenerationBuilder = new CodeGenerationBuilder("EmptyDataClass: " + dataTypeDescriptor.Name); EmptyDataClassCodeGenerator.AddAssemblyReferences(codeGenerationBuilder, dataTypeDescriptor); EmptyDataClassCodeGenerator.AddEmptyDataClassTypeCode(codeGenerationBuilder, dataTypeDescriptor, baseClassType, codeAttributeDeclaration); IEnumerable <Type> types = CodeGenerationManager.CompileRuntimeTempTypes(codeGenerationBuilder); return(types.Single()); }
/// <exclude /> public static void Application_End(object sender, EventArgs e) { if (RuntimeInformation.IsDebugBuild) { Log.LogInformation(_verboseLogEntryTitle, "AppDomain {0} ended at {1} in process {2}", AppDomain.CurrentDomain.Id, DateTime.Now.ToString("HH:mm:ss:ff"), Process.GetCurrentProcess().Id); } if (!SystemSetupFacade.IsSystemFirstTimeInitialized) { return; } using (ThreadDataManager.Initialize()) { try { CodeGenerationManager.ValidateCompositeGenerate(_startTime); CodeGenerationManager.GenerateCompositeGeneratedAssembly(); } catch (Exception ex) { Log.LogCritical("Global.asax", "Error updating Composite.Generated.dll"); Log.LogCritical("Global.asax", ex); } try { GlobalEventSystemFacade.PrepareForShutDown(); if (RuntimeInformation.IsDebugBuild) { LogShutDownReason(); } GlobalEventSystemFacade.ShutDownTheSystem(); TempDirectoryFacade.OnApplicationEnd(); } catch (Exception ex) { Log.LogCritical("Global.asax", ex); throw; } Log.LogVerbose("Global.asax", string.Format("--- Web Application End, {0} Id = {1}---", DateTime.Now.ToLongTimeString(), AppDomain.CurrentDomain.Id)); } }
internal static Dictionary <Guid, Type> GetDataTypes(IReadOnlyCollection <DataTypeDescriptor> dataTypeDescriptors) { var result = new Dictionary <Guid, Type>(); var toCompile = new List <DataTypeDescriptor>(); foreach (var dataTypeDescriptor in dataTypeDescriptors) { string typeFullName = dataTypeDescriptor.GetFullInterfaceName(); Type type = _LoadedDataTypes.FirstOrDefault(f => f.FullName == typeFullName); if (type == null) { bool compilationNeeded; type = InterfaceCodeManager.TryGetType(dataTypeDescriptor, false, out compilationNeeded); if (compilationNeeded) { toCompile.Add(dataTypeDescriptor); } } if (type != null) { result[dataTypeDescriptor.DataTypeId] = type; } } if (toCompile.Any()) { var codeGenerationBuilder = new CodeGenerationBuilder("DataTypeTypesManager:compiling missing interfaces"); foreach (var dataTypeDescriptor in toCompile) { InterfaceCodeGenerator.AddAssemblyReferences(codeGenerationBuilder, dataTypeDescriptor); InterfaceCodeGenerator.AddInterfaceTypeCode(codeGenerationBuilder, dataTypeDescriptor); } var types = CodeGenerationManager.CompileRuntimeTempTypes(codeGenerationBuilder); var typesMap = types.ToDictionary(type => type.FullName); foreach (var dataTypeDescriptor in toCompile) { var type = typesMap[dataTypeDescriptor.GetFullInterfaceName()]; result[dataTypeDescriptor.DataTypeId] = type; } } return(result); }
/// <exclude /> public void AddLocale(string providerName, CultureInfo cultureInfo) { Verify.ArgumentNotNullOrEmpty(providerName, nameof(providerName)); Verify.ArgumentNotNull(cultureInfo, nameof(cultureInfo)); using (var transactionScope = TransactionsFacade.CreateNewScope()) { DataProviderPluginFacade.AddLocale(providerName, cultureInfo); transactionScope.Complete(); } if (!SystemSetupFacade.SetupIsRunning) { CodeGenerationManager.GenerateCompositeGeneratedAssembly(true); } }
public void DeleteType(string providerName, DataTypeDescriptor dataTypeDescriptor, bool makeAFlush) { Verify.ArgumentNotNullOrEmpty(providerName, "providerName"); Verify.ArgumentNotNull(dataTypeDescriptor, "dataTypeDescriptor"); using (TransactionScope transactionScope = TransactionsFacade.CreateNewScope()) { DynamicTypeManager.DropStore(providerName, dataTypeDescriptor, makeAFlush); transactionScope.Complete(); } if (makeAFlush && dataTypeDescriptor.IsCodeGenerated) { CodeGenerationManager.GenerateCompositeGeneratedAssembly(true); } }
/// <summary> /// This method will return the type of the empty data class type. /// If the type does not exist, one will be runtime code generated /// using the <paramref name="dataTypeDescriptor"/>. /// </summary> /// <param name="dataTypeDescriptor"> /// The data type descriptor for the data type to get /// the empty class type for. /// </param> /// <param name="forceReCompilation"> /// If this is true a new empty class will be /// compiled at runtime regardless if it exists or not. /// Use with caution! /// </param> /// <returns>The empty class type for the given data interface type.</returns> public static Type GetEmptyDataClassType(DataTypeDescriptor dataTypeDescriptor, bool forceReCompilation = false) { if (!string.IsNullOrEmpty(dataTypeDescriptor.BuildNewHandlerTypeName)) { return(GetEmptyClassFromBuildNewHandler(dataTypeDescriptor)); } if (forceReCompilation) { return(CreateEmptyDataClassType(dataTypeDescriptor)); } Type interfaceType = TypeManager.TryGetType(dataTypeDescriptor.GetFullInterfaceName()); string emptyClassFullName = EmptyDataClassCodeGenerator.GetEmptyClassTypeFullName(dataTypeDescriptor); Type emptyClassType = TypeManager.TryGetType(emptyClassFullName); bool isRecompileNeeded = true; if (interfaceType != null) { isRecompileNeeded = CodeGenerationManager.IsRecompileNeeded(interfaceType, new[] { emptyClassType }); } if (isRecompileNeeded) { lock (_lock) { interfaceType = TypeManager.TryGetType(dataTypeDescriptor.GetFullInterfaceName()); emptyClassType = TypeManager.TryGetType(emptyClassFullName); if (interfaceType != null) { isRecompileNeeded = CodeGenerationManager.IsRecompileNeeded(interfaceType, new[] { emptyClassType }); } if (isRecompileNeeded) { emptyClassType = CreateEmptyDataClassType(dataTypeDescriptor); } } } return(emptyClassType); }
private bool EnsureNeededTypes(DataTypeDescriptor dataTypeDescriptor, out Type dataProviderHelperType, out Type dataIdClassType, bool forceCompile = false) { lock (_lock) { // Getting the interface (ensuring that it exists) Type interfaceType = DataTypeTypesManager.GetDataType(dataTypeDescriptor); if (interfaceType == null) { dataProviderHelperType = null; dataIdClassType = null; return(false); } string dataProviderHelperClassFullName, dataIdClassFullName; GetGeneratedClassNames(dataTypeDescriptor, out dataProviderHelperClassFullName, out dataIdClassFullName); dataProviderHelperType = TypeManager.TryGetType(dataProviderHelperClassFullName); dataIdClassType = TypeManager.TryGetType(dataIdClassFullName); if (!forceCompile) { forceCompile = CodeGenerationManager.IsRecompileNeeded(interfaceType, new[] { dataProviderHelperType, dataIdClassType }); } if (forceCompile) { var codeGenerationBuilder = new CodeGenerationBuilder(_dataProviderContext.ProviderName + ":" + dataTypeDescriptor.Name); // XmlDataProvider types var codeBuilder = new XmlDataProviderCodeBuilder(_dataProviderContext.ProviderName, codeGenerationBuilder); codeBuilder.AddDataType(dataTypeDescriptor); DataWrapperCodeGenerator.AddDataWrapperClassCode(codeGenerationBuilder, dataTypeDescriptor); IEnumerable <Type> types = CodeGenerationManager.CompileRuntimeTempTypes(codeGenerationBuilder, false); dataProviderHelperType = types.Single(f => f.FullName == dataProviderHelperClassFullName); dataIdClassType = types.Single(f => f.FullName == dataIdClassFullName); } return(true); } }
public override void GenerateCode(CodeGenerationManager manager, object obj) { WebServiceInputActivity activity = obj as WebServiceInputActivity; if (manager == null) { throw new ArgumentNullException("manager"); } if (obj == null) { throw new ArgumentNullException("obj"); } if (activity != null) { if (!(manager.GetService(typeof(ITypeProvider)) is ITypeProvider)) { throw new InvalidOperationException(SR.GetString("General_MissingService", new object[] { typeof(ITypeProvider).FullName })); } CodeNamespaceCollection codeNamespaceCollection = manager.Context[typeof(CodeNamespaceCollection)] as CodeNamespaceCollection; if (codeNamespaceCollection == null) { throw new InvalidOperationException(SR.GetString("Error_ContextStackItemMissing", new object[] { typeof(CodeNamespaceCollection).Name })); } CodeTypeDeclaration declaration = this.CreateOrGetServiceDeclaration(Helpers.GetRootActivity(activity), codeNamespaceCollection); if (activity.InterfaceType != null) { bool flag = false; MethodInfo interfaceMethod = Helpers.GetInterfaceMethod(activity.InterfaceType, activity.MethodName); System.Workflow.Activities.Common.SupportedLanguages supportedLanguage = System.Workflow.Activities.Common.CompilerHelpers.GetSupportedLanguage(manager); foreach (CodeTypeMember member in declaration.Members) { if ((member is CodeMemberMethod) && (string.Compare(member.Name, interfaceMethod.Name, (supportedLanguage == System.Workflow.Activities.Common.SupportedLanguages.CSharp) ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase) == 0)) { flag = true; } } if (!flag) { declaration.Members.Add(this.GetWebServiceMethodDeclaraion(interfaceMethod, activity.IsActivating, supportedLanguage)); } } base.GenerateCode(manager, obj); } }
/// <exclude /> public override void Uninstall() { Verify.IsNotNull(_dataTypeDescriptorsToDelete, "DynamicDataTypePackageFragmentUninstaller has not been validated"); bool flushTheSystem = false; foreach (DataTypeDescriptor dataTypeDescriptor in _dataTypeDescriptorsToDelete) { Log.LogVerbose(this.GetType().Name, "Uninstalling the type '{0}'", dataTypeDescriptor); GeneratedTypesFacade.DeleteType(dataTypeDescriptor, false); flushTheSystem = true; } if (flushTheSystem) { CodeGenerationManager.GenerateCompositeGeneratedAssembly(true); } }
/// <exclude /> public void RemoveLocale(string providerName, CultureInfo cultureInfo) { if (string.IsNullOrEmpty(providerName)) { throw new ArgumentNullException("providerName"); } if (cultureInfo == null) { throw new ArgumentNullException("cultureInfo"); } using (var transactionScope = TransactionsFacade.CreateNewScope()) { DataProviderPluginFacade.RemoveLocale(providerName, cultureInfo); transactionScope.Complete(); } CodeGenerationManager.GenerateCompositeGeneratedAssembly(true); }
/// <summary> /// For internal use only!!! /// This method will create the store if the interfaceType has not been configured. /// </summary> /// <param name="interfaceType"></param> /// <param name="providerName"></param> // Helper public static void EnsureCreateStore(Type interfaceType, string providerName) { IEnumerable <string> dynamicProviderNames; if (providerName == null) { // Checking if any of existing dynamic data providers already has a store for the specified interface type providerName = DataProviderRegistry.DefaultDynamicTypeDataProviderName; dynamicProviderNames = DataProviderRegistry.DynamicDataProviderNames; } else { dynamicProviderNames = new[] { providerName }; } var possibleMatches = dynamicProviderNames .Select(DataProviderPluginFacade.GetDataProvider) .Cast <IDynamicDataProvider>() .SelectMany(dynamicDataProvider => dynamicDataProvider.GetKnownInterfaces()) .Where(i => i.FullName == interfaceType.FullName); foreach (var match in possibleMatches) { if (match == interfaceType) { return; } if (match.GetImmutableTypeId() == interfaceType.GetImmutableTypeId()) { throw new InvalidOperationException($"The same type '{match.FullName}' is loaded in memory twice. Location 1: '{match.Assembly.Location}', location 2: {interfaceType.Assembly.Location}"); } } var dataTypeDescriptor = BuildNewDataTypeDescriptor(interfaceType); CreateStore(providerName, dataTypeDescriptor, true); if (!SystemSetupFacade.SetupIsRunning) { CodeGenerationManager.GenerateCompositeGeneratedAssembly(true); } }
public Type GetType(string fullName) { Type compiledType = CodeGenerationManager.GetCompiledType(fullName); if (compiledType != null) { return(compiledType); } if (fullName.StartsWith(_prefix) && !fullName.Contains(",")) { string name = fullName.Remove(0, _prefix.Length); Type resultType = Type.GetType(name + ", Composite.Generated"); return(resultType); } return(null); }
public void TurnApplicationOnline() { Verify.IsFalse(this.IsApplicationOnline, "The application is already online"); Log.LogVerbose("ApplicationOnlineHandlerFacade", "Turning on the application"); if (_recompileCompositeGenerated) { try { CodeGenerationManager.GenerateCompositeGeneratedAssembly(); } catch (Exception ex) { Log.LogError(LogTitle, "Failed to recompile Composite.Generated.dll"); Log.LogError(LogTitle, ex); } } try { if (_wasLastTurnOffSoft == false) { ApplicationOnlineHandlerPluginFacade.TurnApplicationOnline(); Verify.IsTrue(ApplicationOnlineHandlerPluginFacade.IsApplicationOnline(), "Plugin failed to turn the application online"); } } finally { // Adding a sleep, so delayed notification from FileWatcher will not kill a newly spawned AppDomain Thread.Sleep(250); _shutdownGuard.Dispose(); _shutdownGuard = null; if (HostingEnvironment.IsHosted) { HostingEnvironment.InitiateShutdown(); } } _isApplicationOnline = true; }
private Type TryGetGeneratedType(string typeName) { Type compiledType = CodeGenerationManager.GetCompiledType(typeName); if (compiledType != null) { return(compiledType); } if (_compositeGeneratedAssembly != null) { Type result = _compositeGeneratedAssembly.GetType(typeName, false); if (result != null) { return(result); } } return(TypeManager.TryGetType(typeName)); }
private InterfaceGeneratedClassesInfo InitializeStoreTypes(InterfaceConfigurationElement element, Dictionary <DataTypeDescriptor, IEnumerable <SqlDataTypeStoreDataScope> > allSqlDataTypeStoreDataScopes, Type dataContextClass, bool forceCompile, ref bool dataContextRecompilationNeeded) { HelperClassesGenerationInfo toCompile = null; var result = InitializeStoreTypes(element, allSqlDataTypeStoreDataScopes, dataContextClass, null, forceCompile, ref dataContextRecompilationNeeded, ref toCompile); if (result != null && toCompile != null) { var codeGenerationBuilder = new CodeGenerationBuilder(_dataProviderContext.ProviderName + ":" + result.InterfaceType.FullName); toCompile.GenerateCodeAction(codeGenerationBuilder); var types = CodeGenerationManager.CompileRuntimeTempTypes(codeGenerationBuilder, false).ToArray(); toCompile.PopulateFieldsAction(types); } return(result); }
/// <summary> /// Builds a <see cref="GeneratedTypesInfo"/> object that describes information about helper types generation /// </summary> private GeneratedTypesInfo BuildGeneratedTypesInfo(DataTypeDescriptor dataTypeDescriptor, Type interfaceType, XmlProviderInterfaceConfigurationElement element = null) { string dataProviderHelperClassFullName, dataIdClassFullName; GetGeneratedClassNames(dataTypeDescriptor, out dataProviderHelperClassFullName, out dataIdClassFullName); Type dataProviderHelperClass = TypeManager.TryGetType(dataProviderHelperClassFullName); Type dataIdClass = TypeManager.TryGetType(dataIdClassFullName); bool compilationNeeded = CodeGenerationManager.IsRecompileNeeded(interfaceType, new[] { dataProviderHelperClass, dataIdClass }); return(new GeneratedTypesInfo { Element = element, DataTypeDescriptor = dataTypeDescriptor, InterfaceType = interfaceType, DataIdClass = dataIdClass, DataIdClassName = dataIdClassFullName, DataProviderHelperClass = dataProviderHelperClass, DataProviderHelperClassName = dataProviderHelperClassFullName, CompilationNeeded = compilationNeeded }); }
public void UpdateType(UpdateDataTypeDescriptor updateDataTypeDescriptor) { Verify.ArgumentNotNullOrEmpty(updateDataTypeDescriptor.ProviderName, "providerName"); Verify.ArgumentNotNull(updateDataTypeDescriptor.OldDataTypeDescriptor, "oldDataTypeDescriptor"); Verify.ArgumentNotNull(updateDataTypeDescriptor.NewDataTypeDescriptor, "newDataTypeDescriptor"); Type interfaceType = null; if (updateDataTypeDescriptor.OldDataTypeDescriptor.IsCodeGenerated) { interfaceType = InterfaceCodeManager.GetType(updateDataTypeDescriptor.NewDataTypeDescriptor, true); } else { interfaceType = DataTypeTypesManager.GetDataType(updateDataTypeDescriptor.NewDataTypeDescriptor); } updateDataTypeDescriptor.NewDataTypeDescriptor.TypeManagerTypeName = TypeManager.SerializeType(interfaceType); DynamicTypeManager.AlterStore(updateDataTypeDescriptor, false); CodeGenerationManager.GenerateCompositeGeneratedAssembly(true); }
/// <summary> /// Checks that tables related to specified data type included in current DataContext class, if not - compiles a new version of DataContext that contains them /// </summary> private HelperClassesGenerationInfo EnsureNeededTypes( DataTypeDescriptor dataTypeDescriptor, IEnumerable <SqlDataTypeStoreDataScope> sqlDataTypeStoreDataScopes, Dictionary <DataTypeDescriptor, IEnumerable <SqlDataTypeStoreDataScope> > allSqlDataTypeStoreDataScopes, Type dataContextClassType, out Dictionary <SqlDataTypeStoreTableKey, StoreTypeInfo> fields, ref bool dataContextRecompileNeeded, bool forceCompile = false) { lock (_lock) { // Getting the interface (ensuring that it exists) Type interfaceType = DataTypeTypesManager.GetDataType(dataTypeDescriptor); var storeDataScopesToCompile = new List <SqlDataTypeStoreDataScope>(); var storeDataScopesAlreadyCompiled = new List <SqlDataTypeStoreDataScope>(); fields = new Dictionary <SqlDataTypeStoreTableKey, StoreTypeInfo>(); foreach (SqlDataTypeStoreDataScope storeDataScope in sqlDataTypeStoreDataScopes) { string dataContextFieldName = NamesCreator.MakeDataContextFieldName(storeDataScope.TableName); FieldInfo dataContextFieldInfo = null; if (dataContextClassType != null) { dataContextFieldInfo = dataContextClassType.GetFields(BindingFlags.Public | BindingFlags.Instance) .SingleOrDefault(f => f.Name == dataContextFieldName); } string sqlDataProviderHelperClassFullName = NamesCreator.MakeSqlDataProviderHelperClassFullName(dataTypeDescriptor, storeDataScope.DataScopeName, storeDataScope.CultureName, _dataProviderContext.ProviderName); string entityClassName = NamesCreator.MakeEntityClassFullName(dataTypeDescriptor, storeDataScope.DataScopeName, storeDataScope.CultureName, _dataProviderContext.ProviderName); Type sqlDataProviderHelperClass = null, entityClass = null; try { sqlDataProviderHelperClass = TryGetGeneratedType(sqlDataProviderHelperClassFullName); entityClass = TryGetGeneratedType(entityClassName); forceCompile = forceCompile || CodeGenerationManager.IsRecompileNeeded(interfaceType, new[] { sqlDataProviderHelperClass, entityClass }); } catch (TypeLoadException) { forceCompile = true; } if (!forceCompile) { var storeTypeInfo = new StoreTypeInfo(dataContextFieldName, entityClass, sqlDataProviderHelperClass) { DataContextField = dataContextFieldInfo }; fields.Add(new SqlDataTypeStoreTableKey(storeDataScope.DataScopeName, storeDataScope.CultureName), storeTypeInfo); } if (dataContextFieldInfo == null) { dataContextRecompileNeeded = true; } if (forceCompile) { storeDataScopesToCompile.Add(storeDataScope); } else { storeDataScopesAlreadyCompiled.Add(storeDataScope); } } if (storeDataScopesToCompile.Any()) { dataContextRecompileNeeded = true; if (!dataTypeDescriptor.IsCodeGenerated) { // Building a new descriptor so generated classes take in account field changes dataTypeDescriptor = DynamicTypeManager.BuildNewDataTypeDescriptor(interfaceType); } return(CompileMissingClasses(dataTypeDescriptor, allSqlDataTypeStoreDataScopes, fields, storeDataScopesToCompile, storeDataScopesAlreadyCompiled)); } } return(null); }
public void CreateStores(IReadOnlyCollection <DataTypeDescriptor> dataTypeDescriptors) { var types = DataTypeTypesManager.GetDataTypes(dataTypeDescriptors); foreach (var dataTypeDescriptor in dataTypeDescriptors) { if (InterfaceConfigurationManipulator.ConfigurationExists(_dataProviderContext.ProviderName, dataTypeDescriptor)) { var filePath = InterfaceConfigurationManipulator.GetConfigurationFilePath(_dataProviderContext.ProviderName); throw new InvalidOperationException( $"SqlDataProvider configuration already contains a interface named '{dataTypeDescriptor.TypeManagerTypeName}', Id: '{dataTypeDescriptor.DataTypeId}'. Remove it from the configuration file '{filePath}' and restart the application."); } } // Creating Sql tables and adding to the configuration var configElements = new Dictionary <DataTypeDescriptor, InterfaceConfigurationElement>(); foreach (var dataTypeDescriptor in dataTypeDescriptors) { Type type = types[dataTypeDescriptor.DataTypeId]; Action <string> existingTablesValidator = tableName => { var errors = new StringBuilder(); var interfaceType = type; if (!ValidateTable(interfaceType, tableName, errors)) { throw new InvalidOperationException("Table '{0}' already exist but isn't valid: {1}".FormatWith(tableName, errors.ToString())); } }; SqlStoreManipulator.CreateStoresForType(dataTypeDescriptor, existingTablesValidator); InterfaceConfigurationElement element = InterfaceConfigurationManipulator.AddNew(_dataProviderContext.ProviderName, dataTypeDescriptor); _interfaceConfigurationElements.Add(element); configElements.Add(dataTypeDescriptor, element); } // Generating necessary classes and performing validation Dictionary <DataTypeDescriptor, IEnumerable <SqlDataTypeStoreDataScope> > allSqlDataTypeStoreDataScopes = BuildAllExistingDataTypeStoreDataScopes(); bool dataContextRecompilationNeeded = false; var toCompileList = new List <HelperClassesGenerationInfo>(); var generatedClassesInfo = new Dictionary <DataTypeDescriptor, InterfaceGeneratedClassesInfo>(); Type dataContextClass = _sqlDataTypeStoresContainer.DataContextClass; foreach (var dataTypeDescriptor in dataTypeDescriptors) { var element = configElements[dataTypeDescriptor]; // InitializeStoreResult initializeStoreResult = InitializeStore(element, allSqlDataTypeStoreDataScopes); HelperClassesGenerationInfo toCompile = null; var classesInfo = InitializeStoreTypes(element, allSqlDataTypeStoreDataScopes, dataContextClass, null, false, ref dataContextRecompilationNeeded, ref toCompile); if (classesInfo != null && toCompile != null) { toCompileList.Add(toCompile); generatedClassesInfo[dataTypeDescriptor] = classesInfo; } } // Compiling missing classes if (toCompileList.Any()) { var codeGenerationBuilder = new CodeGenerationBuilder(_dataProviderContext.ProviderName + ":CreateStores"); foreach (var toCompile in toCompileList) { toCompile.GenerateCodeAction(codeGenerationBuilder); } var generatedHelperClasses = CodeGenerationManager.CompileRuntimeTempTypes(codeGenerationBuilder, false).ToArray(); foreach (var toCompile in toCompileList) { toCompile.PopulateFieldsAction(generatedHelperClasses); } } // Emitting a new DataContext class if (dataContextRecompilationNeeded) { var newDataTypeIds = new HashSet <Guid>(dataTypeDescriptors.Select(d => d.DataTypeId)); _createdSqlDataTypeStoreTables.RemoveAll(f => newDataTypeIds.Contains(f.DataTypeId)); var fields = _createdSqlDataTypeStoreTables.Select(s => new Tuple <string, Type>(s.DataContextFieldName, s.DataContextFieldType)).ToList(); foreach (var classesInfo in generatedClassesInfo.Values) { fields.AddRange(classesInfo.Fields.Select(f => new Tuple <string, Type>(f.Value.FieldName, f.Value.FieldType))); } dataContextClass = DataContextAssembler.EmitDataContextClass(fields); UpdateCreatedSqlDataTypeStoreTables(dataContextClass); } _sqlDataTypeStoresContainer.DataContextClass = dataContextClass; // Registering the new type/tables foreach (var dataTypeDescriptor in dataTypeDescriptors) { InterfaceGeneratedClassesInfo classesInfo; if (!generatedClassesInfo.TryGetValue(dataTypeDescriptor, out classesInfo)) { throw new InvalidOperationException("No generated classes for data type '{0}' found".FormatWith(dataTypeDescriptor.Name)); } InitializeStoreResult initInfo = EmbedDataContextInfo(classesInfo, dataContextClass); AddDataTypeStore(initInfo, false); } }
private void InitializeExistingStores() { _compositeGeneratedAssembly = _compositeGeneratedAssembly ?? AssemblyFacade.GetGeneratedAssemblyFromBin(); _sqlDataTypeStoresContainer = new SqlDataTypeStoresContainer(_dataProviderContext.ProviderName, _connectionString, _sqlLoggingContext); Dictionary <DataTypeDescriptor, IEnumerable <SqlDataTypeStoreDataScope> > allSqlDataTypeStoreDataScopes = BuildAllExistingDataTypeStoreDataScopes(); var initializedStores = new List <InterfaceGeneratedClassesInfo>(); bool dataContextRecompilationNeeded = false; Type dataContextClass = TryLoadDataContext(ref dataContextRecompilationNeeded); var dataTypes = LoadDataTypes(_interfaceConfigurationElements); var compilationData = new List <HelperClassesGenerationInfo>(); foreach (InterfaceConfigurationElement element in _interfaceConfigurationElements) { HelperClassesGenerationInfo toCompile = null; var generatedClassesInfo = InitializeStoreTypes(element, allSqlDataTypeStoreDataScopes, dataContextClass, dataTypes, false, ref dataContextRecompilationNeeded, ref toCompile); if (generatedClassesInfo == null) { continue; } if (toCompile != null) { compilationData.Add(toCompile); } initializedStores.Add(generatedClassesInfo); } if (compilationData.Any()) { var codeGenerationBuilder = new CodeGenerationBuilder(_dataProviderContext.ProviderName + " : compiling missing classes"); foreach (var toCompile in compilationData) { toCompile.GenerateCodeAction(codeGenerationBuilder); } // Precompiling DataWrapper classes as well to improve loading time foreach (var interfaceType in dataTypes.Values) { if (DataWrapperTypeManager.TryGetWrapperType(interfaceType.FullName) == null) { DataWrapperCodeGenerator.AddDataWrapperClassCode(codeGenerationBuilder, interfaceType); } } var types = CodeGenerationManager.CompileRuntimeTempTypes(codeGenerationBuilder, false).ToArray(); foreach (var toCompile in compilationData) { toCompile.PopulateFieldsAction(types); } } if (dataContextRecompilationNeeded) { dataContextClass = DataContextAssembler.EmitDataContextClass( initializedStores .Where(s => s.Fields != null) .SelectMany(s => s.Fields.Values).Evaluate()); } _sqlDataTypeStoresContainer.DataContextClass = dataContextClass; foreach (var typeInfo in initializedStores) { var store = EmbedDataContextInfo(typeInfo, dataContextClass); AddDataTypeStore(store, true, true); } }
private static void DoInitialize() { int startTime = Environment.TickCount; Guid installationId = InstallationInformationFacade.InstallationId; Log.LogVerbose(LogTitle, "Initializing the system core - installation id = " + installationId); using (new LogExecutionTime(LogTitle, "Initialization of the static data types")) { DataProviderRegistry.InitializeDataTypes(); } using (new LogExecutionTime(LogTitle, "Auto update of static data types")) { bool typesUpdated = AutoUpdateStaticDataTypes(); if (typesUpdated) { using (new LogExecutionTime(LogTitle, "Reinitialization of the static data types")) { SqlTableInformationStore.Flush(); DataProviderRegistry.Flush(); DataProviderPluginFacade.Flush(); DataProviderRegistry.InitializeDataTypes(); } CodeGenerationManager.GenerateCompositeGeneratedAssembly(true); } } using (new LogExecutionTime(LogTitle, "Ensure data stores")) { bool dataStoresCreated = DataStoreExistenceVerifier.EnsureDataStores(); if (dataStoresCreated) { Log.LogVerbose(LogTitle, "Initialization of the system was halted, performing a flush"); _initializing = false; GlobalEventSystemFacade.FlushTheSystem(); return; } } using (new LogExecutionTime(LogTitle, "Initializing data process controllers")) { ProcessControllerFacade.Initialize_PostDataTypes(); } using (new LogExecutionTime(LogTitle, "Initializing data type references")) { DataReferenceRegistry.Initialize_PostDataTypes(); } using (new LogExecutionTime(LogTitle, "Initializing data type associations")) { DataAssociationRegistry.Initialize_PostDataTypes(); } using (new LogExecutionTime(LogTitle, "Initializing internal urls")) { InternalUrls.Initialize_PostDataTypes(); } using (new LogExecutionTime(LogTitle, "Initializing functions")) { MetaFunctionProviderRegistry.Initialize_PostDataTypes(); } Log.LogVerbose(LogTitle, "Starting initialization of administrative secondaries"); if (SystemSetupFacade.IsSystemFirstTimeInitialized && !SystemSetupFacade.SetupIsRunning) { using (new LogExecutionTime(LogTitle, "Initializing workflow runtime")) { WorkflowFacade.EnsureInitialization(); } } if (!RuntimeInformation.IsUnittest) { using (new LogExecutionTime(LogTitle, "Initializing flow system")) { FlowControllerFacade.Initialize(); } using (new LogExecutionTime(LogTitle, "Initializing console system")) { ConsoleFacade.Initialize(); } } using (new LogExecutionTime(LogTitle, "Auto installing packages")) { DoAutoInstallPackages(); } using (new LogExecutionTime(LogTitle, "Loading element providers")) { ElementProviderLoader.LoadAllProviders(); } int executionTime = Environment.TickCount - startTime; Log.LogVerbose(LogTitle, "Done initializing of the system core. ({0} ms)".FormatWith(executionTime)); }