Exemple #1
0
        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);
            }
        }
        private void InitializeExistingStores()
        {
            var xmlDataTypeStoreCreator = new XmlDataTypeStoreCreator(_fileStoreDirectory);

            _xmlDataTypeStoresContainer = new XmlDataTypeStoresContainer(_dataProviderContext.ProviderName);

            var dataTypes = LoadDataTypes(_dataTypeConfigurationElements);

            var storesToLoad = new List <GeneratedTypesInfo>();

            foreach (XmlProviderInterfaceConfigurationElement element in _dataTypeConfigurationElements)
            {
                var dataTypeDescriptor = GetDataTypeDescriptorNotNull(element);

                Type interfaceType = null;

                try
                {
                    if (!dataTypes.TryGetValue(dataTypeDescriptor.DataTypeId, out interfaceType) || interfaceType == null)
                    {
                        Log.LogWarning(LogTitle, "The data interface type '{0}' does not exist and is not code generated. It will not be usable", dataTypeDescriptor.TypeManagerTypeName);
                        continue;
                    }

                    storesToLoad.Add(BuildGeneratedTypesInfo(dataTypeDescriptor, interfaceType, element));
                }
                catch (Exception ex)
                {
                    if (interfaceType != null)
                    {
                        DataProviderRegistry.AddKnownDataType(interfaceType, _dataProviderContext.ProviderName);
                    }
                    Log.LogError(LogTitle, "Failed initialization for the datatype {{{0}}}, {1}", dataTypeDescriptor.DataTypeId, dataTypeDescriptor.TypeManagerTypeName);
                    Log.LogError(LogTitle, ex);
                }
            }

            CompileMissingTypes(storesToLoad);

            foreach (var storeToLoad in storesToLoad)
            {
                try
                {
                    var xmlDataTypeStoreDataScopes = new List <XmlDataTypeStoreDataScope>();
                    foreach (DataScopeConfigurationElement dataScopeConfigurationElement in storeToLoad.Element.ConfigurationStores)
                    {
                        var xmlDataTypeStoreDataScope = new XmlDataTypeStoreDataScope
                        {
                            DataScopeName = dataScopeConfigurationElement.DataScope,
                            CultureName   = dataScopeConfigurationElement.CultureName,
                            ElementName   = dataScopeConfigurationElement.ElementName,
                            Filename      = Path.Combine(_fileStoreDirectory, dataScopeConfigurationElement.Filename)
                        };

                        xmlDataTypeStoreDataScopes.Add(xmlDataTypeStoreDataScope);
                    }

                    XmlDataTypeStore xmlDateTypeStore = xmlDataTypeStoreCreator.CreateStoreResult(storeToLoad.DataTypeDescriptor,
                                                                                                  storeToLoad.DataProviderHelperClass, storeToLoad.DataIdClass, xmlDataTypeStoreDataScopes);

                    AddDataTypeStore(storeToLoad.DataTypeDescriptor, storeToLoad.InterfaceType, xmlDateTypeStore);
                }
                catch (Exception ex)
                {
                    DataProviderRegistry.AddKnownDataType(storeToLoad.InterfaceType, _dataProviderContext.ProviderName);

                    Log.LogError(LogTitle, "Failed initialization for the datatype {{{0}}}, {1}", storeToLoad.DataTypeDescriptor.DataTypeId, storeToLoad.DataTypeDescriptor.TypeManagerTypeName);
                    Log.LogError(LogTitle, ex);
                }
            }
        }