Exemple #1
0
        public static TarkTypeMapping <T> CreateEmptyMapType <T>()
        {
            var existingMapping = GetMapping <T>();

            if (existingMapping != null)
            {
                Mappings.Remove(existingMapping);
            }

            Type type = typeof(T);
            Dictionary <string, TarkColumnMapping> propertiesMappings =
                new Dictionary <string, TarkColumnMapping>(StringComparer.OrdinalIgnoreCase);

            var newMapping = new TarkTypeMapping <T>(type, propertiesMappings);

            Mappings.Add(newMapping);

            return(newMapping);
        }
Exemple #2
0
        public static TarkTypeMapping <T> AutoMapType <T>()
        {
            var existingMapping = GetMapping <T>();

            if (existingMapping != null)
            {
                return(existingMapping);
            }

            Type type = typeof(T);

            PropertyInfo[] properties = type.GetProperties();
            Dictionary <string, TarkColumnMapping> propertiesMappings =
                new Dictionary <string, TarkColumnMapping>(StringComparer.OrdinalIgnoreCase);

            for (int i = 0; i < properties.Length; i++)
            {
                if (properties[i].GetCustomAttribute <IgnoreMappingAttribute>() != null)
                {
                    continue;
                }

                string columnName;

                var columnAttribute = properties[i].GetCustomAttribute <ColumnAttribute>();
                if (columnAttribute != null && !string.IsNullOrWhiteSpace(columnAttribute.Name))
                {
                    columnName = columnAttribute.Name;
                }
                else
                {
                    columnName = properties[i].Name;
                }

                propertiesMappings.Add(columnName, new TarkColumnMapping(properties[i]));
            }

            var newMapping = new TarkTypeMapping <T>(type, propertiesMappings);

            //Database
            var databaseAttribute = type.GetCustomAttribute <DatabaseAttribute>();

            if (databaseAttribute != null && !string.IsNullOrWhiteSpace(databaseAttribute.Name))
            {
                newMapping.Database = databaseAttribute.Name;
            }

            //Table and schema
            var tableAttribute = type.GetCustomAttribute <TableAttribute>();

            if (tableAttribute != null)
            {
                if (!string.IsNullOrWhiteSpace(tableAttribute.Name))
                {
                    newMapping.Table = tableAttribute.Name;
                }

                if (!string.IsNullOrWhiteSpace(tableAttribute.Schema))
                {
                    newMapping.Schema = tableAttribute.Schema;
                }
            }
            else
            {
                newMapping.Table = type.Name;
            }

            Mappings.Add(newMapping);

            return(newMapping);
        }