示例#1
0
        /// <summary>
        /// Recupera os metadados dos tipos do assembly informado.
        /// </summary>
        /// <param name="assembly">Assembly onde os metadados serão pesquisados.</param>
        /// <returns></returns>
        private IEnumerable <ITypeMetadata> GetTypeMetadataFromAssembly(System.Reflection.Assembly assembly)
        {
            var types = assembly.GetTypes();

            foreach (var type in types)
            {
                var customAttributes = type.GetCustomAttributes(typeof(global::GDA.PersistenceClassAttribute), false);
                if (customAttributes.Length > 0)
                {
                    var persistenceClass   = (global::GDA.PersistenceClassAttribute)customAttributes[0];
                    var isCache            = type.GetCustomAttributes(typeof(CacheAttribute), false).HasItems();
                    var propertiesMetadata = new List <Local.PropertyMetadata>();
                    foreach (var property in type.GetProperties())
                    {
                        var persistenceProperty = (global::GDA.PersistencePropertyAttribute)property.GetCustomAttributes(typeof(global::GDA.PersistencePropertyAttribute), false).FirstOrDefault();
                        var propertyType        = property.PropertyType;
                        if (propertyType.IsNullable())
                        {
                            propertyType = propertyType.GetRootType();
                        }
                        if (propertyType == typeof(uint))
                        {
                            propertyType = typeof(int);
                        }
                        if (propertyType.IsEnum)
                        {
                            propertyType = Enum.GetUnderlyingType(propertyType);
                        }
                        if (persistenceProperty != null)
                        {
                            var parameterType = PersistenceParameterType.Field;
                            switch (persistenceProperty.ParameterType)
                            {
                            case global::GDA.PersistenceParameterType.Field:
                                parameterType = PersistenceParameterType.Field;
                                break;

                            case global::GDA.PersistenceParameterType.IdentityKey:
                                parameterType = PersistenceParameterType.IdentityKey;
                                break;

                            case global::GDA.PersistenceParameterType.Key:
                                parameterType = PersistenceParameterType.Key;
                                break;
                            }
                            var directionParameter = (DirectionParameter)Enum.Parse(typeof(DirectionParameter), persistenceProperty.Direction.ToString());
                            if (directionParameter == DirectionParameter.InputOptional || directionParameter == DirectionParameter.InputOptionalOutput)
                            {
                                continue;
                            }
                            if (directionParameter == DirectionParameter.InputOptionalOutputOnlyInsert || directionParameter == DirectionParameter.OutputOnlyInsert || directionParameter == DirectionParameter.OnlyInsert)
                            {
                                directionParameter = DirectionParameter.InputOutput;
                            }
                            var fkAttribute    = property.GetCustomAttributes(typeof(global::GDA.PersistenceForeignKeyAttribute), false).FirstOrDefault() as global::GDA.PersistenceForeignKeyAttribute;
                            var isCacheIndexed = parameterType == PersistenceParameterType.IdentityKey || parameterType == PersistenceParameterType.Key || fkAttribute != null || property.GetCustomAttributes(typeof(CacheIndexedAttribute), false).HasItems();
                            var pMetadata      = new Local.PropertyMetadata(_currentPropertyCode++, property.Name, persistenceProperty.Name, propertyType.FullName, null, isCacheIndexed, directionParameter, false, parameterType, false);
                            if (fkAttribute != null)
                            {
                                pMetadata.ForeignKey = new Local.ForeignKeyInfo {
                                    Assembly  = assembly.FullName,
                                    Namespace = fkAttribute.TypeOfClassRelated.Namespace,
                                    TypeName  = fkAttribute.TypeOfClassRelated.Name,
                                    Property  = fkAttribute.PropertyName
                                };
                            }
                            propertiesMetadata.Add(pMetadata);
                        }
                    }
                    var typeMetadata = new Colosoft.Data.Schema.Local.TypeMetadata(_currentTypeCode++, type.Name, type.Namespace, assembly.GetName().Name, isCache, false, new TableName(global::GDA.GDASettings.DefaultProviderName, "dbo", persistenceClass.Name), propertiesMetadata.Select(f => (IPropertyMetadata)f).ToArray());
                    yield return(typeMetadata);
                }
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            var assembly        = typeof(Glass.Data.CFeUtils.ConfigCFe).Assembly;
            var typeSchema      = new Local.TypeSchema();
            var typeCode        = 1;
            var propertyCode    = 1;
            var types           = assembly.GetTypes();
            var outputDirectory = System.IO.Path.GetDirectoryName(typeof(Program).Assembly.Location) + "\\output";

            if (!System.IO.Directory.Exists(outputDirectory))
            {
                System.IO.Directory.CreateDirectory(outputDirectory);
            }
            if (!System.IO.Directory.Exists(System.IO.Path.Combine(outputDirectory, "Models")))
            {
                System.IO.Directory.CreateDirectory(System.IO.Path.Combine(outputDirectory, "Models"));
            }
            if (!System.IO.Directory.Exists(System.IO.Path.Combine(outputDirectory, "Entities")))
            {
                System.IO.Directory.CreateDirectory(System.IO.Path.Combine(outputDirectory, "Entities"));
            }
            foreach (var type in types)
            {
                var customAttributes = type.GetCustomAttributes(typeof(global::GDA.PersistenceClassAttribute), false);
                if (customAttributes.Length > 0)
                {
                    var persistenceClass   = (global::GDA.PersistenceClassAttribute)customAttributes[0];
                    var propertiesMetadata = new List <Local.PropertyMetadata>();
                    foreach (var property in type.GetProperties())
                    {
                        var persistenceProperty = (global::GDA.PersistencePropertyAttribute)property.GetCustomAttributes(typeof(global::GDA.PersistencePropertyAttribute), false).FirstOrDefault();
                        var propertyType        = property.PropertyType;
                        if (propertyType.IsNullable())
                        {
                            propertyType = propertyType.GetRootType();
                        }
                        if (propertyType == typeof(uint))
                        {
                            propertyType = typeof(int);
                        }
                        if (persistenceProperty != null)
                        {
                            var parameterType = PersistenceParameterType.Field;
                            switch (persistenceProperty.ParameterType)
                            {
                            case global::GDA.PersistenceParameterType.Field:
                                parameterType = PersistenceParameterType.Field;
                                break;

                            case global::GDA.PersistenceParameterType.IdentityKey:
                                parameterType = PersistenceParameterType.IdentityKey;
                                break;

                            case global::GDA.PersistenceParameterType.Key:
                                parameterType = PersistenceParameterType.Key;
                                break;
                            }
                            var directionParameter = (DirectionParameter)Enum.Parse(typeof(DirectionParameter), persistenceProperty.Direction.ToString());
                            if (directionParameter == DirectionParameter.InputOptional || directionParameter == DirectionParameter.InputOptionalOutput || directionParameter == DirectionParameter.InputOptionalOutputOnlyInsert)
                            {
                                continue;
                            }
                            var pMetadata = new Local.PropertyMetadata(propertyCode++, property.Name, persistenceProperty.Name, propertyType.FullName, null, false, DirectionParameter.InputOutput, false, parameterType, false);
                            propertiesMetadata.Add(pMetadata);
                        }
                    }
                    var typeMetadata = new Colosoft.Data.Schema.Local.TypeMetadata(typeCode++, type.Name, type.Namespace, assembly.GetName().Name, false, false, new TableName("WebGlass", "dbo", persistenceClass.Name), propertiesMetadata.Select(f => (IPropertyMetadata)f).ToArray());
                    typeSchema.AddTypeMetadata((ITypeMetadata)typeMetadata.Clone());
                    CreateCSharpFile(type, typeMetadata, outputDirectory);
                }
            }
            var xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(Local.TypeSchema));
            var xml           = new StringBuilder();

            xmlSerializer.Serialize(new System.IO.StringWriter(xml), typeSchema);
            var xmlText        = xml.ToString();
            var typeSchema2    = xmlSerializer.Deserialize(new System.IO.StringReader(xmlText));
            var schemaFileName = System.IO.Path.Combine(outputDirectory, "TypeSchema.xml");

            if (System.IO.File.Exists(schemaFileName))
            {
                System.IO.File.Delete(schemaFileName);
            }
            System.IO.File.WriteAllText(schemaFileName, xmlText);
        }