示例#1
0
        internal static InterfaceConfigurationElement AddNew(string providerName, DataTypeDescriptor dataTypeDescriptor)
        {
            lock (_syncRoot)
            {
                var configuration = new SqlDataProviderConfiguration(providerName);

                if (configuration.Section.Interfaces.ContainsInterfaceType(dataTypeDescriptor.DataTypeId))
                {
                    Log.LogWarning(LogTitle,
                                   "Configuration file '{0}' already contains an interface with data type ID '{1}', type name '{2}'. "
                                   + "Possibly there are multiple AppDomain-s running.",
                                   configuration.ConfigurationFilePath,
                                   dataTypeDescriptor.DataTypeId,
                                   dataTypeDescriptor);

                    return(configuration.Section.Interfaces.Get(dataTypeDescriptor));
                }

                InterfaceConfigurationElement interfaceConfig = BuildInterfaceConfigurationElement(dataTypeDescriptor);

                configuration.Section.Interfaces.Add(interfaceConfig);

                configuration.Save();

                return(interfaceConfig);
            }
        }
示例#2
0
        private static InterfaceConfigurationElement BuildInterfaceConfigurationElement(
            DataTypeDescriptor dataTypeDescriptor,
            InterfaceConfigurationElement existingElement = null,
            bool updateTableNames = false)
        {
            var propertyMappings = new PropertyNameMappingConfigurationElementCollection();
            //foreach (DataFieldDescriptor field in dataTypeDescriptor.Fields)
            //{
            //    propertyMappings.Add(field.Name, field.Name);
            //}

            var keyInfo = new SimpleNameTypeConfigurationElementCollection();

            foreach (DataFieldDescriptor field in dataTypeDescriptor.KeyFields)
            {
                keyInfo.Add(field.Name, field.InstanceType);
            }

            var stores = new StoreConfigurationElementCollection();

            // Fix logic for the case of a localized interface without languages
            foreach (DataScopeIdentifier dataScope in dataTypeDescriptor.DataScopes)
            {
                foreach (var culture in SqlDataProviderStoreManipulator.GetCultures(dataTypeDescriptor))
                {
                    string tableName = null;

                    if (!updateTableNames && existingElement != null)
                    {
                        foreach (StoreConfigurationElement table  in existingElement.ConfigurationStores)
                        {
                            if (table.DataScope == dataScope.Name && table.CultureName == culture.Name)
                            {
                                tableName = table.TableName;
                                break;
                            }
                        }
                    }

                    tableName = tableName ?? DynamicTypesCommon.GenerateTableName(dataTypeDescriptor, dataScope, culture);

                    stores.Add(new StoreConfigurationElement {
                        TableName = tableName, DataScope = dataScope.Name, CultureName = culture.Name
                    });
                }
            }

            return(new InterfaceConfigurationElement
            {
                DataTypeId = dataTypeDescriptor.DataTypeId,
                IsGeneratedType = dataTypeDescriptor.IsCodeGenerated,
                ConfigurationStores = stores,
                ConfigurationPropertyNameMappings = propertyMappings,
                ConfigurationDataIdProperties = keyInfo,
                ConfigurationPropertyInitializers = new SimpleNameTypeConfigurationElementCollection()
            });
        }
示例#3
0
        internal static bool ConfigurationExists(string providerName, DataTypeDescriptor dataTypeDescriptor)
        {
            lock (_syncRoot)
            {
                var configuration = new SqlDataProviderConfiguration(providerName);

                InterfaceConfigurationElement interfaceConfig = BuildInterfaceConfigurationElement(dataTypeDescriptor);

                return(configuration.Section.Interfaces.ContainsInterfaceType(interfaceConfig));
            }
        }
示例#4
0
        internal static InterfaceConfigurationElement Change(string providerName, DataTypeChangeDescriptor changeDescriptor, bool localeChanges)
        {
            lock (_syncRoot)
            {
                var originalType = changeDescriptor.OriginalType;
                var alteredType  = changeDescriptor.AlteredType;

                bool typeNameChanged = originalType.Namespace != alteredType.Namespace ||
                                       originalType.Name != alteredType.Name;

                if (!localeChanges &&
                    !changeDescriptor.AddedDataScopes.Any() &&
                    !changeDescriptor.DeletedDataScopes.Any() &&
                    !changeDescriptor.AddedKeyFields.Any() &&
                    !changeDescriptor.DeletedKeyFields.Any() &&
                    !changeDescriptor.KeyFieldsOrderChanged &&
                    !typeNameChanged)
                {
                    // No changes to the config is needed, lets not touch the file.
                    return(null);
                }

                var configuration = new SqlDataProviderConfiguration(providerName);

                Guid dataTypeId = originalType.DataTypeId;

                var existingElement = configuration.Section.Interfaces.Get(originalType);

                Verify.IsNotNull(existingElement, "Configuration does not contain the original interface with id '{0}'", dataTypeId);

                configuration.Section.Interfaces.Remove(originalType);

                InterfaceConfigurationElement newInterfaceConfig = BuildInterfaceConfigurationElement(alteredType, existingElement, typeNameChanged);

                configuration.Section.Interfaces.Add(newInterfaceConfig);

                configuration.Save();

                return(newInterfaceConfig);
            }
        }
        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
            {
                interfaceType = dataTypes != null ? dataTypes[dataTypeId] : DataTypeTypesManager.GetDataType(dataTypeDescriptor);

                if (interfaceType == null)
                {
                    Log.LogError(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;
            }
        }
        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;
        }
        private InitializeStoreResult InitializeStore(InterfaceConfigurationElement element,
            Dictionary<DataTypeDescriptor, IEnumerable<SqlDataTypeStoreDataScope>> allSqlDataTypeStoreDataScopes,
            bool forceCompile = false)
        {
            bool dataContextRecompilationNeeded = false;

            Type dataContextClass = _sqlDataTypeStoresContainer.DataContextClass;

            var initInfo = InitializeStoreTypes(element, allSqlDataTypeStoreDataScopes, dataContextClass, forceCompile, ref dataContextRecompilationNeeded);

            if (initInfo.InterfaceType == null)
            {
                return new InitializeStoreResult();
            }

            if (dataContextRecompilationNeeded)
            {
                _createdSqlDataTypeStoreTables.RemoveAll(f => f.DataTypeId == initInfo.DataTypeDescriptor.DataTypeId);

                var existingFields = _createdSqlDataTypeStoreTables.Select(
                                      s => new Tuple<string, Type>(s.DataContextFieldName, s.DataContextFieldType));
                var newFields = initInfo.Fields.Select(f => new Tuple<string, Type>(f.Value.FieldName, f.Value.FieldType));

                dataContextClass = DataContextAssembler.EmitDataContextClass(existingFields.Concat(newFields).Evaluate());

                UpdateCreatedSqlDataTypeStoreTables(dataContextClass);
            }

            _sqlDataTypeStoresContainer.DataContextClass = dataContextClass;

            return EmbedDataContextInfo(initInfo, dataContextClass);
        }
        private static InterfaceConfigurationElement BuildInterfaceConfigurationElement(
            DataTypeDescriptor dataTypeDescriptor,
            InterfaceConfigurationElement existingElement = null,
            bool updateTableNames = false)
        {
            var propertyMappings = new PropertyNameMappingConfigurationElementCollection();
            //foreach (DataFieldDescriptor field in dataTypeDescriptor.Fields)
            //{
            //    propertyMappings.Add(field.Name, field.Name);
            //}

            var keyInfo = new SimpleNameTypeConfigurationElementCollection();
            foreach (DataFieldDescriptor field in dataTypeDescriptor.KeyFields)
            {
                keyInfo.Add(field.Name, field.InstanceType);
            }

            var stores = new StoreConfigurationElementCollection();
            // Fix logic for the case of a localized interface without languages
            foreach (DataScopeIdentifier dataScope in dataTypeDescriptor.DataScopes)
            {
                foreach (var culture in SqlDataProviderStoreManipulator.GetCultures(dataTypeDescriptor))
                {
                    string tableName = null;

                    if (!updateTableNames && existingElement != null)
                    {
                        foreach (StoreConfigurationElement table  in existingElement.ConfigurationStores)
                        {
                            if (table.DataScope == dataScope.Name && table.CultureName == culture.Name)
                            {
                                tableName = table.TableName;
                                break;
                            }
                        }

                    }

                    tableName = tableName ?? DynamicTypesCommon.GenerateTableName(dataTypeDescriptor, dataScope, culture);

                    stores.Add(new StoreConfigurationElement {TableName = tableName, DataScope = dataScope.Name, CultureName = culture.Name});
                }
            }

            return new InterfaceConfigurationElement
            {
                DataTypeId = dataTypeDescriptor.DataTypeId,
                IsGeneratedType = dataTypeDescriptor.IsCodeGenerated,
                ConfigurationStores = stores,
                ConfigurationPropertyNameMappings = propertyMappings,
                ConfigurationDataIdProperties = keyInfo,
                ConfigurationPropertyInitializers = new SimpleNameTypeConfigurationElementCollection()
            };
        }