internal void AddDataType(DataTypeDescriptor dataTypeDescriptor) { Type interfaceType = DataTypeTypesManager.GetDataType(dataTypeDescriptor); if (interfaceType == null) { return; } XmlProviderCodeGenerator codeGenerator = new XmlProviderCodeGenerator(dataTypeDescriptor, _namespaceName); IEnumerable <CodeTypeDeclaration> codeTypeDeclarations = codeGenerator.CreateCodeDOMs(); codeTypeDeclarations.ForEach(f => _codeGenerationBuilder.AddType(_namespaceName, f)); // Property serializer for entity tokens and more var keyPropertiesDictionary = new Dictionary <string, Type>(); var keyPropertiesList = new List <Tuple <string, Type> >(); foreach (var keyField in dataTypeDescriptor.PhysicalKeyFields) { Verify.That(!keyPropertiesDictionary.ContainsKey(keyField.Name), "Key field with name '{0}' already present. Data type: {1}. Check for multiple [KeyPropertyName(...)] attributes.", keyField.Name, dataTypeDescriptor.Namespace + "." + dataTypeDescriptor.Name); keyPropertiesDictionary.Add(keyField.Name, keyField.InstanceType); keyPropertiesList.Add(new Tuple <string, Type>(keyField.Name, keyField.InstanceType)); } PropertySerializerTypeCodeGenerator.AddPropertySerializerTypeCode(_codeGenerationBuilder, codeGenerator.DataIdClassFullName, keyPropertiesList); _codeGenerationBuilder.AddReference(interfaceType.Assembly); }
/// <summary> /// Returns the CLT Type for this data type description. /// </summary> /// <returns></returns> public Type GetInterfaceType() { if (this.TypeManagerTypeName == null) { throw new InvalidOperationException("The TypeManagerTypeName has not been set"); } return(DataTypeTypesManager.GetDataType(this)); }
private static Type GetEmptyClassFromBuildNewHandler(DataTypeDescriptor dataTypeDescriptor) { Type buildNewHandlerType = TypeManager.GetType(dataTypeDescriptor.BuildNewHandlerTypeName); IBuildNewHandler buildNewHandler = (IBuildNewHandler)Activator.CreateInstance(buildNewHandlerType); Type dataType = DataTypeTypesManager.GetDataType(dataTypeDescriptor); VerifyAssemblyLocation(dataType); return(buildNewHandler.GetTypeToBuild(dataType)); }
private static Type GetEmptyClassFromBuildNewHandler(DataTypeDescriptor dataTypeDescriptor) { Type buildNewHandlerType = TypeManager.GetType(dataTypeDescriptor.BuildNewHandlerTypeName); IBuildNewHandler buildNewHandler = (IBuildNewHandler)Activator.CreateInstance(buildNewHandlerType); Type dataType = DataTypeTypesManager.GetDataType(dataTypeDescriptor); if (!DataTypeTypesManager.IsAllowedDataTypeAssembly(dataType)) { string message = string.Format("The data interface '{0}' is not located in an assembly in the website Bin folder. Please move it to that location", dataType); Log.LogError("EmptyDataClassTypeManager", message); throw new InvalidOperationException(message); } return(buildNewHandler.GetTypeToBuild(dataType)); }
internal static void AddDataWrapperClassCode(CodeGenerationBuilder codeGenerationBuilder, DataTypeDescriptor dataTypeDescriptor) { Type interfaceType = DataTypeTypesManager.GetDataType(dataTypeDescriptor); if (interfaceType == null) { return; } codeGenerationBuilder.AddReference(typeof(IDataWrapper).Assembly); codeGenerationBuilder.AddReference(typeof(EditorBrowsableAttribute).Assembly); CodeTypeDeclaration codeTypeDeclaration = CreateCodeTypeDeclaration(dataTypeDescriptor); codeGenerationBuilder.AddType(NamespaceName, codeTypeDeclaration); }
internal static void AddEmptyDataClassTypeCode(CodeGenerationBuilder codeGenerationBuilder, DataTypeDescriptor dataTypeDescriptor, Type baseClassType = null, CodeAttributeDeclaration codeAttributeDeclaration = null) { Type interfaceType = DataTypeTypesManager.GetDataType(dataTypeDescriptor); if (interfaceType == null) { return; } if (baseClassType == null) { baseClassType = typeof(EmptyDataClassBase); } CodeTypeDeclaration codeTypeDeclaration = CreateCodeTypeDeclaration(dataTypeDescriptor, baseClassType, codeAttributeDeclaration); codeGenerationBuilder.AddType(NamespaceName, codeTypeDeclaration); }
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); } }
internal static CodeTypeDeclaration CreateCodeTypeDeclaration(DataTypeDescriptor dataTypeDescriptor, Type baseClass, CodeAttributeDeclaration codeAttributeDeclaration) { Type interfaceType = DataTypeTypesManager.GetDataType(dataTypeDescriptor); string interfaceTypeFullName = interfaceType.FullName; CodeTypeDeclaration declaration = new CodeTypeDeclaration(); declaration.Name = CreateClassName(interfaceTypeFullName); declaration.IsClass = true; declaration.TypeAttributes = TypeAttributes.Public | TypeAttributes.Sealed; declaration.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(SerializableAttribute)))); declaration.CustomAttributes.Add( new CodeAttributeDeclaration( new CodeTypeReference(typeof(EditorBrowsableAttribute)), new CodeAttributeArgument( new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(typeof(EditorBrowsableState)), EditorBrowsableState.Never.ToString() ) ) ) ); if (baseClass != null) { declaration.BaseTypes.Add(baseClass); } declaration.BaseTypes.Add(new CodeTypeReference(interfaceType, CodeTypeReferenceOptions.GlobalReference)); if (codeAttributeDeclaration != null) { declaration.CustomAttributes.Add(codeAttributeDeclaration); } AddConstructor(declaration); AddInterfaceProperties(declaration, dataTypeDescriptor.Fields); AddInterfaceTypeProperty(declaration, interfaceTypeFullName); return(declaration); }
internal static void AddAssemblyReferences(CodeGenerationBuilder codeGenerationBuilder, DataTypeDescriptor dataTypeDescriptor) { Type interfaceType = DataTypeTypesManager.GetDataType(dataTypeDescriptor); if (interfaceType == null) { return; } codeGenerationBuilder.AddReference(typeof(EmptyDataClassBase).Assembly); codeGenerationBuilder.AddReference(typeof(EditorBrowsableAttribute).Assembly); codeGenerationBuilder.AddReference(interfaceType.Assembly); if (!string.IsNullOrEmpty(dataTypeDescriptor.BuildNewHandlerTypeName)) { Type buildeNewHandlerType = TypeManager.GetType(dataTypeDescriptor.BuildNewHandlerTypeName); codeGenerationBuilder.AddReference(buildeNewHandlerType.Assembly); } }
public void AlterStore(UpdateDataTypeDescriptor updateDataTypeDescriptor, bool forceCompile) { XmlDataProviderDocumentCache.ClearCache(); XmlProviderInterfaceConfigurationElement element = InterfaceConfigurationManipulator.Change(updateDataTypeDescriptor); if (forceCompile) { DataTypeDescriptor dataTypeDescriptor = updateDataTypeDescriptor.NewDataTypeDescriptor; Type dataProviderHelperType; Type dataIdClassType; bool typesExists = EnsureNeededTypes(dataTypeDescriptor, out dataProviderHelperType, out dataIdClassType, true); Verify.That(typesExists, "Could not find or code generated the type '{0}' or one of the needed helper types", dataTypeDescriptor.GetFullInterfaceName()); var xmlDataTypeStoreDataScopes = new List <XmlDataTypeStoreDataScope>(); foreach (DataScopeConfigurationElement dataScopeConfigurationElement in element.ConfigurationStores) { var xmlDataTypeStoreDataScope = new XmlDataTypeStoreDataScope { DataScopeName = dataScopeConfigurationElement.DataScope, CultureName = dataScopeConfigurationElement.CultureName, ElementName = dataScopeConfigurationElement.ElementName, Filename = Path.Combine(_fileStoreDirectory, dataScopeConfigurationElement.Filename) }; xmlDataTypeStoreDataScopes.Add(xmlDataTypeStoreDataScope); } var xmlDataTypeStoreCreator = new XmlDataTypeStoreCreator(_fileStoreDirectory); XmlDataTypeStore xmlDateTypeStore = xmlDataTypeStoreCreator.CreateStoreResult(dataTypeDescriptor, dataProviderHelperType, dataIdClassType, xmlDataTypeStoreDataScopes); Type interfaceType = DataTypeTypesManager.GetDataType(dataTypeDescriptor); UpdateDataTypeStore(dataTypeDescriptor, interfaceType, xmlDateTypeStore); } }
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); }
private InterfaceGeneratedClassesInfo InitializeStoreTypes(InterfaceConfigurationElement element, Dictionary <DataTypeDescriptor, IEnumerable <SqlDataTypeStoreDataScope> > allSqlDataTypeStoreDataScopes, Type dataContextClass, Dictionary <Guid, Type> dataTypes, bool forceCompile, ref bool dataContextRecompilationNeeded, ref HelperClassesGenerationInfo helperClassesGenerationInfo) { var result = new InterfaceGeneratedClassesInfo(); var dataScopes = new List <SqlDataTypeStoreDataScope>(); foreach (StorageInformation storageInformation in element.Stores) { var sqlDataTypeStoreDataScope = new SqlDataTypeStoreDataScope { DataScopeName = storageInformation.DataScope, CultureName = storageInformation.CultureName, TableName = storageInformation.TableName }; dataScopes.Add(sqlDataTypeStoreDataScope); } result.DataScopes = dataScopes; Guid dataTypeId = element.DataTypeId; var dataTypeDescriptor = DataMetaDataFacade.GetDataTypeDescriptor(dataTypeId, true); if (dataTypeDescriptor == null) { throw NewConfigurationException(element, "Failed to get a DataTypeDescriptor by id '{0}'".FormatWith(dataTypeId)); } result.DataTypeDescriptor = dataTypeDescriptor; Type interfaceType = null; try { if (dataTypes == null || !dataTypes.TryGetValue(dataTypeId, out interfaceType) || interfaceType == null) { interfaceType = DataTypeTypesManager.GetDataType(dataTypeDescriptor); } if (interfaceType == null) { Log.LogWarning(LogTitle, "The data interface type '{0}' does not exists and is not code generated. It will not be unusable", dataTypeDescriptor.TypeManagerTypeName); return(result); } result.InterfaceType = interfaceType; string validationMessage; bool isValid = DataTypeValidationRegistry.Validate(interfaceType, dataTypeDescriptor, out validationMessage); if (!isValid) { Log.LogCritical(LogTitle, validationMessage); throw new InvalidOperationException(validationMessage); } Dictionary <SqlDataTypeStoreTableKey, StoreTypeInfo> fields; helperClassesGenerationInfo = EnsureNeededTypes(dataTypeDescriptor, dataScopes, allSqlDataTypeStoreDataScopes, dataContextClass, out fields, ref dataContextRecompilationNeeded, forceCompile); result.Fields = fields; return(result); } catch (Exception ex) { if (interfaceType != null) { DataProviderRegistry.RegisterDataTypeInitializationError(interfaceType, ex); DataProviderRegistry.AddKnownDataType(interfaceType, _dataProviderContext.ProviderName); Log.LogError(LogTitle, "Failed initialization for the datatype {0}", dataTypeDescriptor.TypeManagerTypeName); } Log.LogError(LogTitle, ex); result.Fields = new Dictionary <SqlDataTypeStoreTableKey, StoreTypeInfo>(); return(result); } }