コード例 #1
0
        public IEnumerable <CodeTypeDeclaration> CreateCodeDOMs()
        {
            string dataProviderHelperClassName = NamesCreator.MakeDataProviderHelperClassName(_dataTypeDescriptor);
            string wrapperClassName            = NamesCreator.MakeWrapperClassName(_dataTypeDescriptor);
            string dataIdClassName             = NamesCreator.MakeDataIdClassName(_dataTypeDescriptor);


            DataProviderHelperClassGenerator classGenerator = new DataProviderHelperClassGenerator(
                dataProviderHelperClassName,
                wrapperClassName,
                dataIdClassName,
                _dataTypeDescriptor
                );
            CodeTypeDeclaration dataHelperClassCodeTypeDeclaration = classGenerator.CreateClass();

            yield return(dataHelperClassCodeTypeDeclaration);


            DataIdClassGenerator dataIdClassGenerator           = new DataIdClassGenerator(dataIdClassName, _dataTypeDescriptor);
            CodeTypeDeclaration  dataIdClassCodeTypeDeclaration = dataIdClassGenerator.CreateClass();

            yield return(dataIdClassCodeTypeDeclaration);


            DataWrapperClassGenerator dataWrapperClassGenerator           = new DataWrapperClassGenerator(wrapperClassName, _dataTypeDescriptor);
            CodeTypeDeclaration       dataWrapperClassCodeTypeDeclaration = dataWrapperClassGenerator.CreateClass();

            yield return(dataWrapperClassCodeTypeDeclaration);
        }
コード例 #2
0
        private void GetGeneratedClassNames(DataTypeDescriptor dataTypeDescriptor, out string dataProviderHelperClassFullName, out string dataIdClassFullName)
        {
            string namespaceName = NamesCreator.MakeNamespaceName(_dataProviderContext.ProviderName);

            dataProviderHelperClassFullName = namespaceName + "." + NamesCreator.MakeDataProviderHelperClassName(dataTypeDescriptor);
            dataIdClassFullName             = namespaceName + "." + NamesCreator.MakeDataIdClassName(dataTypeDescriptor);
        }
コード例 #3
0
        public XmlDataProviderCodeBuilder(string providerName, CodeGenerationBuilder codeGenerationBuilder)
        {
            _codeGenerationBuilder = codeGenerationBuilder;

            _namespaceName = NamesCreator.MakeNamespaceName(providerName);

            AddCodeNamespaces();
        }
コード例 #4
0
        public XmlProviderCodeGenerator(DataTypeDescriptor dataTypeDescriptor, string namespaceName)
        {
            _dataTypeDescriptor = dataTypeDescriptor;

            DataProviderHelperClassFullName = namespaceName + "." + NamesCreator.MakeDataProviderHelperClassName(dataTypeDescriptor);
            WrapperClassFullName            = namespaceName + "." + NamesCreator.MakeWrapperClassName(dataTypeDescriptor);
            DataIdClassFullName             = namespaceName + "." + NamesCreator.MakeDataIdClassName(dataTypeDescriptor);
        }
コード例 #5
0
        public IEnumerable <CodeTypeDeclaration> CreateDataContextCodeDOMs(IEnumerable <Tuple <string, string> > entityClassNamesAndDataContextFieldNames)
        {
            string dataContextClassName = NamesCreator.MakeDataContextClassName(_providerName);

            DataContextClassGenerator dataContextClassGenerator  = new DataContextClassGenerator(dataContextClassName, entityClassNamesAndDataContextFieldNames);
            CodeTypeDeclaration       dataContextTypeDeclaration = dataContextClassGenerator.CreateClass();

            yield return(dataContextTypeDeclaration);
        }
コード例 #6
0
        private HelperClassesGenerationInfo CompileMissingClasses(DataTypeDescriptor dataTypeDescriptor, Dictionary <DataTypeDescriptor,
                                                                                                                     IEnumerable <SqlDataTypeStoreDataScope> > allSqlDataTypeStoreDataScopes,
                                                                  Dictionary <SqlDataTypeStoreTableKey, StoreTypeInfo> fields,
                                                                  List <SqlDataTypeStoreDataScope> storeDataScopesToCompile,
                                                                  List <SqlDataTypeStoreDataScope> storeDataScopesAlreadyCompiled)
        {
            return(new HelperClassesGenerationInfo
            {
                GenerateCodeAction = codeGenerationBuilder =>
                {
                    var sqlDataProviderCodeBuilder = new SqlDataProviderCodeBuilder(_dataProviderContext.ProviderName, codeGenerationBuilder);
                    sqlDataProviderCodeBuilder.AddDataType(dataTypeDescriptor, storeDataScopesToCompile);

                    sqlDataProviderCodeBuilder.AddExistingDataType(dataTypeDescriptor, storeDataScopesAlreadyCompiled);
                },
                PopulateFieldsAction = types =>
                {
                    foreach (SqlDataTypeStoreDataScope storeDataScope in storeDataScopesToCompile)
                    {
                        string dataContextFieldName = NamesCreator.MakeDataContextFieldName(storeDataScope.TableName);

                        string helperClassFullName = NamesCreator.MakeSqlDataProviderHelperClassFullName(
                            dataTypeDescriptor, storeDataScope.DataScopeName, storeDataScope.CultureName, _dataProviderContext.ProviderName);
                        Type helperClass = types.Single(f => f.FullName == helperClassFullName);

                        string entityClassFullName = NamesCreator.MakeEntityClassFullName(
                            dataTypeDescriptor, storeDataScope.DataScopeName, storeDataScope.CultureName, _dataProviderContext.ProviderName);
                        Type entityClass = types.Single(f => f.FullName == entityClassFullName);

                        var storeTableKey = new SqlDataTypeStoreTableKey(storeDataScope.DataScopeName, storeDataScope.CultureName);
                        fields[storeTableKey] = new StoreTypeInfo(dataContextFieldName, entityClass, helperClass);
                    }

                    foreach (SqlDataTypeStoreDataScope storeDataScope in storeDataScopesAlreadyCompiled)
                    {
                        string dataContextFieldName = NamesCreator.MakeDataContextFieldName(storeDataScope.TableName);

                        string helperClassFullName = NamesCreator.MakeSqlDataProviderHelperClassFullName(
                            dataTypeDescriptor, storeDataScope.DataScopeName, storeDataScope.CultureName, _dataProviderContext.ProviderName);
                        Type helperClass = TryGetGeneratedType(helperClassFullName);

                        string entityClassFullName = NamesCreator.MakeEntityClassFullName(
                            dataTypeDescriptor, storeDataScope.DataScopeName, storeDataScope.CultureName, _dataProviderContext.ProviderName);
                        Type entityClass = TryGetGeneratedType(entityClassFullName);

                        var storeTableKey = new SqlDataTypeStoreTableKey(storeDataScope.DataScopeName, storeDataScope.CultureName);
                        fields[storeTableKey] = new StoreTypeInfo(dataContextFieldName, entityClass, helperClass);
                    }
                }
            });
        }
コード例 #7
0
        private Type TryLoadDataContext(ref bool forceCompile)
        {
            string dataContextClassFullName = NamesCreator.MakeDataContextClassFullName(_dataProviderContext.ProviderName);
            Type   dataContextClass         = TryGetGeneratedType(dataContextClassFullName);

            // Trying to instantiate a data context object
            if (dataContextClass != null && !TryLoadDataContextClass(dataContextClass))
            {
                forceCompile = true;
                return(null);
            }

            return(dataContextClass);
        }
コード例 #8
0
        public IEnumerable <Tuple <string, string> > CreateEntityClassNamesAndDataContextFieldNames(DataTypeDescriptor dataTypeDescriptor, IEnumerable <SqlDataTypeStoreDataScope> sqlDataTypeStoreDataScopes)
        {
            List <Tuple <string, string> > entityClassNamesAndDataContextFieldNames = new List <Tuple <string, string> >();

            foreach (SqlDataTypeStoreDataScope dataScope in sqlDataTypeStoreDataScopes)
            {
                string entityClassName      = NamesCreator.MakeEntityClassName(dataTypeDescriptor, dataScope.DataScopeName, dataScope.CultureName);
                string dataContextFieldName = NamesCreator.MakeDataContextFieldName(dataScope.TableName);

                entityClassNamesAndDataContextFieldNames.Add(new Tuple <string, string>(entityClassName, dataContextFieldName));
            }

            return(entityClassNamesAndDataContextFieldNames);
        }
コード例 #9
0
        public IEnumerable <CodeTypeDeclaration> CreateCodeDOMs(DataTypeDescriptor dataTypeDescriptor, IEnumerable <SqlDataTypeStoreDataScope> sqlDataTypeStoreDataScopes, out IEnumerable <Tuple <string, string> > entityClassNamesAndDataContextFieldNames)
        {
            List <CodeTypeDeclaration> result = new List <CodeTypeDeclaration>();

            string dataIdClassName     = NamesCreator.MakeDataIdClassName(dataTypeDescriptor);
            string entityBaseClassName = NamesCreator.MakeEntityBaseClassName(dataTypeDescriptor);

            DataIdClassGenerator dataIdClassGenerator           = new DataIdClassGenerator(dataTypeDescriptor, dataIdClassName);
            CodeTypeDeclaration  dataIdClassCodeTypeDeclaration = dataIdClassGenerator.CreateClass();

            result.Add(dataIdClassCodeTypeDeclaration);


            EntityBaseClassGenerator entityBaseClassGenerator           = new EntityBaseClassGenerator(dataTypeDescriptor, entityBaseClassName, dataIdClassName, _providerName);
            CodeTypeDeclaration      entityBaseClassCodeTypeDeclaration = entityBaseClassGenerator.CreateClass();

            result.Add(entityBaseClassCodeTypeDeclaration);

            List <Tuple <string, string> > outResult = new List <Tuple <string, string> >();

            foreach (SqlDataTypeStoreDataScope dataScope in sqlDataTypeStoreDataScopes)
            {
                string entityClassName = NamesCreator.MakeEntityClassName(dataTypeDescriptor, dataScope.DataScopeName, dataScope.CultureName);

                EntityClassGenerator entityClassGenerator           = new EntityClassGenerator(dataTypeDescriptor, entityClassName, entityBaseClassName, dataScope.TableName, dataScope.DataScopeName, dataScope.CultureName);
                CodeTypeDeclaration  entityClassCodeTypeDeclaration = entityClassGenerator.CreateClass();
                result.Add(entityClassCodeTypeDeclaration);

                string sqlDataProviderHelperClassName = NamesCreator.MakeSqlDataProviderHelperClassName(dataTypeDescriptor, dataScope.DataScopeName, dataScope.CultureName);
                string dataContextFieldName           = NamesCreator.MakeDataContextFieldName(dataScope.TableName);

                SqlDataProviderHelperGenerator sqlDataProviderHelperGenerator       = new SqlDataProviderHelperGenerator(dataTypeDescriptor, sqlDataProviderHelperClassName, dataIdClassName, entityClassName, dataContextFieldName);
                CodeTypeDeclaration            sqlDataProviderHelperTypeDeclaration = sqlDataProviderHelperGenerator.CreateClass();
                result.Add(sqlDataProviderHelperTypeDeclaration);

                outResult.Add(new Tuple <string, string>(entityClassName, dataContextFieldName));
            }

            entityClassNamesAndDataContextFieldNames = outResult;

            return(result);
        }
コード例 #10
0
        /// <summary>
        /// This class is used to create <see cref="XmlDataTypeStore"/>.
        /// Either for existing stores or for just newly created/added stores.
        /// There exist one store for each data type that the provider handles.
        /// While the <see cref="XmlDataTypeStore"/> is created, the input and
        /// configuration is validated.
        /// </summary>
        /// <param name="dataTypeDescriptor"></param>
        /// <param name="dataProviderHelperClassType">The runtime type for the generated implementation of <see cref="IXmlDataProviderHelper"/></param>
        /// <param name="dataIdClassType">The runtime type for the generated data id class.</param>
        /// <param name="xmlDataTypeStoreDataScopes">If this is null, default values will be created.</param>
        /// <returns>
        /// Returns a <see cref="XmlDataTypeStore"/> if it is valid, else null
        /// </returns>
        public XmlDataTypeStore CreateStoreResult(DataTypeDescriptor dataTypeDescriptor, Type dataProviderHelperClassType, Type dataIdClassType, IEnumerable <XmlDataTypeStoreDataScope> xmlDataTypeStoreDataScopes)
        {
            if (xmlDataTypeStoreDataScopes == null)
            {
                var defaultDataScopes = new List <XmlDataTypeStoreDataScope>();

                IEnumerable <string> cultureNames;
                if (dataTypeDescriptor.Localizeable)
                {
                    cultureNames = DataLocalizationFacade.ActiveLocalizationNames;
                }
                else
                {
                    cultureNames = new[] { CultureInfo.InvariantCulture.Name };
                }

                foreach (DataScopeIdentifier dataScopeIdentifier in dataTypeDescriptor.DataScopes.Distinct())
                {
                    foreach (string cultureName in cultureNames)
                    {
                        var defaultXmlDataTypeStoreDataScope = new XmlDataTypeStoreDataScope
                        {
                            DataScopeName = dataScopeIdentifier.Name,
                            CultureName   = cultureName,
                            ElementName   = NamesCreator.MakeElementName(dataTypeDescriptor),
                            Filename      = Path.Combine(_fileStoreDirectory, NamesCreator.MakeFileName(dataTypeDescriptor, dataScopeIdentifier, cultureName))
                        };

                        var document = new XDocument(new XElement(defaultXmlDataTypeStoreDataScope.ElementName));
                        document.SaveToFile(defaultXmlDataTypeStoreDataScope.Filename);

                        defaultDataScopes.Add(defaultXmlDataTypeStoreDataScope);
                    }
                }

                xmlDataTypeStoreDataScopes = defaultDataScopes;
            }

            return(new XmlDataTypeStore(dataTypeDescriptor, dataProviderHelperClassType, dataIdClassType, xmlDataTypeStoreDataScopes, dataTypeDescriptor.IsCodeGenerated));
        }
コード例 #11
0
        /// <summary>
        /// This will not add needed entity class and data context field names if the data entity class does not exist
        /// </summary>
        /// <param name="dataTypeDescriptor"></param>
        /// <param name="sqlDataTypeStoreDataScopes"></param>
        internal void AddExistingDataType(DataTypeDescriptor dataTypeDescriptor, IEnumerable <SqlDataTypeStoreDataScope> sqlDataTypeStoreDataScopes)
        {
            SqlProviderCodeGenerator codeGenerator = new SqlProviderCodeGenerator(_providerName);

            IEnumerable <Tuple <string, string> > names = codeGenerator.CreateEntityClassNamesAndDataContextFieldNames(dataTypeDescriptor, sqlDataTypeStoreDataScopes);

            foreach (Tuple <string, string> name in names)
            {
                Type type = TypeManager.TryGetType(NamesCreator.MakeNamespaceName(_providerName) + "." + name.Item1);

                if (type != null)
                {
                    _codeGenerationBuilder.AddReference(type.Assembly.Location);
                    _entityClassNamesAndDataContextFieldNames.Add(name);

                    foreach (Type interfaceType in type.GetInterfaces())
                    {
                        _codeGenerationBuilder.AddReference(interfaceType.Assembly);
                    }
                }
            }
        }
コード例 #12
0
        internal void AddDataType(DataTypeDescriptor dataTypeDescriptor, IEnumerable <SqlDataTypeStoreDataScope> sqlDataTypeStoreDataScopes)
        {
            Type interfaceType = DataTypeTypesManager.GetDataType(dataTypeDescriptor);

            if (interfaceType == null)
            {
                return;
            }

            IEnumerable <Tuple <string, string> > names;

            SqlProviderCodeGenerator          codeGenerator        = new SqlProviderCodeGenerator(_providerName);
            IEnumerable <CodeTypeDeclaration> codeTypeDeclarations = codeGenerator.CreateCodeDOMs(dataTypeDescriptor, sqlDataTypeStoreDataScopes, out names);

            codeTypeDeclarations.ForEach(f => _codeGenerationBuilder.AddType(_namespaceName, f));

            _entityClassNamesAndDataContextFieldNames.AddRange(names);

            _codeGenerationBuilder.AddReference(interfaceType.Assembly);

            // Property serializer for entity tokens and more
            string dataIdClassFullName = NamesCreator.MakeDataIdClassFullName(dataTypeDescriptor, _providerName);

            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, dataIdClassFullName, keyPropertiesList);
        }
コード例 #13
0
        /// <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);
        }