예제 #1
0
        private static Mappings CreateMappings(ConventionScannerSettings scannerSettings, Type[] typesOverride)
        {
            var types  = typesOverride ?? FindTypes(scannerSettings);
            var config = new Dictionary <Type, TypeDefinition>();

            foreach (var type in types)
            {
                var pocoDefn = new TypeDefinition(type)
                {
                    AutoIncrement   = scannerSettings.PrimaryKeysAutoIncremented(type),
                    PrimaryKey      = scannerSettings.PrimaryKeysNamed(type),
                    TableName       = scannerSettings.TablesNamed(type),
                    SequenceName    = scannerSettings.SequencesNamed(type),
                    ExplicitColumns = true
                };

                foreach (var prop in ReflectionUtils.GetFieldsAndPropertiesForClasses(type))
                {
                    var column = new ColumnDefinition();
                    column.MemberInfo        = prop;
                    column.DbColumnName      = scannerSettings.PropertiesNamed(prop);
                    column.DbColumnAlias     = scannerSettings.AliasNamed(prop);
                    column.IgnoreColumn      = scannerSettings.IgnorePropertiesWhere.Any(x => x.Invoke(prop));
                    column.DbColumnType      = scannerSettings.DbColumnTypesAs(prop);
                    column.ResultColumn      = scannerSettings.ResultPropertiesWhere(prop);
                    column.ComputedColumn    = scannerSettings.ComputedPropertiesWhere(prop);
                    column.VersionColumn     = scannerSettings.VersionPropertiesWhere(prop);
                    column.VersionColumnType = scannerSettings.VersionColumnTypeAs(prop);
                    column.ForceUtc          = scannerSettings.ForceDateTimesToUtcWhere(prop);

                    if (!pocoDefn.ColumnConfiguration.ContainsKey(prop.Name))
                    {
                        pocoDefn.ColumnConfiguration.Add(prop.Name, column);
                    }
                }

                config.Add(type, pocoDefn);
            }

            MergeOverrides(config, scannerSettings.MappingOverrides);

            //if (scannerSettings.OverrideWithAttributes)
            //{
            //    MergeAttributeOverrides(config);
            //}

            var pocoMappings = new Mappings {
                Config = config
            };

            return(pocoMappings);
        }
        private static Mappings CreateMappings(ConventionScannerSettings scannerSettings, Type[] typesOverride)
        {
            var types = typesOverride ?? FindTypes(scannerSettings);
            var config = new Dictionary<Type, TypeDefinition>();
            
            foreach (var type in types)
            {
                var pocoDefn = new TypeDefinition(type)
                {
                    AutoIncrement = scannerSettings.PrimaryKeysAutoIncremented(type),
                    PrimaryKey = scannerSettings.PrimaryKeysNamed(type),
                    TableName = scannerSettings.TablesNamed(type),
                    SequenceName = scannerSettings.SequencesNamed(type),
                    ExplicitColumns = true
                };

                foreach (var prop in ReflectionUtils.GetFieldsAndPropertiesForClasses(type))
                {
                    var column = new ColumnDefinition();
                    column.MemberInfo = prop;
                    column.DbColumnName = scannerSettings.PropertiesNamed(prop);
                    column.DbColumnAlias = scannerSettings.AliasNamed(prop);
                    column.IgnoreColumn = scannerSettings.IgnorePropertiesWhere.Any(x => x.Invoke(prop));
                    column.DbColumnType = scannerSettings.DbColumnTypesAs(prop);
                    column.ResultColumn = scannerSettings.ResultPropertiesWhere(prop);
                    column.ComputedColumn = scannerSettings.ComputedPropertiesWhere(prop);
                    column.VersionColumn = scannerSettings.VersionPropertiesWhere(prop);
                    column.VersionColumnType = scannerSettings.VersionColumnTypeAs(prop);
                    column.ForceUtc = scannerSettings.ForceDateTimesToUtcWhere(prop);

                    if (!pocoDefn.ColumnConfiguration.ContainsKey(prop.Name))
                        pocoDefn.ColumnConfiguration.Add(prop.Name, column);
                }

                config.Add(type, pocoDefn);
            }

            MergeOverrides(config, scannerSettings.MappingOverrides);

            //if (scannerSettings.OverrideWithAttributes)
            //{
            //    MergeAttributeOverrides(config);
            //}

            var pocoMappings = new Mappings {Config = config};
            return pocoMappings;
        }
예제 #3
0
        private static IEnumerable<ColumnDefinition> GetColumnDefinitions(ConventionScannerSettings scannerSettings, Type type, List<MemberInfo> memberInfos, bool isReferenceProperty = false)
        {
            var capturedMembers = memberInfos.ToArray();
            foreach (var member in ReflectionUtils.GetFieldsAndPropertiesForClasses(type))
            {
                var complexProperty = scannerSettings.ComplexPropertiesWhere(member);
                var referenceProperty = scannerSettings.ReferencePropertiesWhere(member);
                var dbColumn = scannerSettings.DbColumnWhere(member);

                if ((complexProperty || referenceProperty) && !dbColumn)
                {
                    if (capturedMembers.GroupBy(x => x.GetMemberInfoType()).Any(x => x.Count() >= 2))
                    {
                        continue;
                    }

                    var members = new List<MemberInfo>();
                    members.AddRange(capturedMembers);
                    members.Add(member);

                    var memberInfoType = member.GetMemberInfoType();
                    if (PocoDataBuilder.IsList(member))
                    {
                        memberInfoType = memberInfoType.GetGenericArguments().First();
                    }

                    var columnDefinitions = GetColumnDefinitions(scannerSettings, memberInfoType, members, referenceProperty).ToList();

                    foreach (var columnDefinition in columnDefinitions)
                    {
                        yield return columnDefinition;
                    }

                    var referenceDbColumnsNamed = scannerSettings.ReferenceDbColumnsNamed(member);

                    yield return new ColumnDefinition()
                    {
                        MemberInfoChain = capturedMembers.Concat(new[] { member }).ToArray(),
                        MemberInfo = member,
                        IsComplexMapping = complexProperty,
                        IsReferenceMember = referenceProperty,
                        ReferenceType = ReferenceType.None,
                        ReferenceMember = null,
                        ResultColumn = scannerSettings.ResultPropertiesWhere(member),
                        DbColumnName = referenceProperty ? referenceDbColumnsNamed : null,
                    };
                }
                else
                {
                    var columnDefinition = new ColumnDefinition();
                    columnDefinition.MemberInfoChain = capturedMembers.Concat(new[] {member}).ToArray();
                    columnDefinition.MemberInfo = member;

                    var prefixProperty = isReferenceProperty ? Enumerable.Empty<string>() : capturedMembers.Select(x => scannerSettings.DbColumnsNamed(x));
                    columnDefinition.DbColumnName = string.Join(PocoData.Separator, prefixProperty.Concat(new[] { scannerSettings.DbColumnsNamed(member) }).ToArray());

                    columnDefinition.DbColumnAlias = scannerSettings.AliasNamed(member);
                    columnDefinition.IgnoreColumn = scannerSettings.IgnorePropertiesWhere.Any(x => x.Invoke(member));
                    columnDefinition.DbColumnType = scannerSettings.DbColumnTypesAs(member);
                    columnDefinition.ResultColumn = scannerSettings.ResultPropertiesWhere(member);
                    columnDefinition.ComputedColumn = scannerSettings.ComputedPropertiesWhere(member);
                    columnDefinition.ComputedColumnType = scannerSettings.ComputedPropertyTypeAs(member);
                    columnDefinition.VersionColumn = scannerSettings.VersionPropertiesWhere(member);
                    columnDefinition.VersionColumnType = scannerSettings.VersionPropertyTypeAs(member);
                    columnDefinition.ForceUtc = scannerSettings.ForceDateTimesToUtcWhere(member);
                    columnDefinition.Serialized = scannerSettings.SerializedWhere(member);
                    yield return columnDefinition;
                }
            }
        }
예제 #4
0
        private static IEnumerable <ColumnDefinition> GetColumnDefinitions(ConventionScannerSettings scannerSettings, Type type, List <MemberInfo> memberInfos, bool isReferenceProperty = false)
        {
            var capturedMembers = memberInfos.ToArray();

            foreach (var member in ReflectionUtils.GetFieldsAndPropertiesForClasses(type))
            {
                var complexProperty   = scannerSettings.ComplexPropertiesWhere(member);
                var referenceProperty = scannerSettings.ReferencePropertiesWhere(member);
                var dbColumn          = scannerSettings.DbColumnWhere(member);

                if ((complexProperty || referenceProperty) && !dbColumn)
                {
                    if (capturedMembers.GroupBy(x => x.GetMemberInfoType()).Any(x => x.Count() >= 2))
                    {
                        continue;
                    }

                    var members = new List <MemberInfo>();
                    members.AddRange(capturedMembers);
                    members.Add(member);

                    var memberInfoType = member.GetMemberInfoType();
                    if (PocoDataBuilder.IsList(member))
                    {
                        memberInfoType = memberInfoType.GetGenericArguments().First();
                    }

                    var columnDefinitions = GetColumnDefinitions(scannerSettings, memberInfoType, members, referenceProperty).ToList();

                    foreach (var columnDefinition in columnDefinitions)
                    {
                        yield return(columnDefinition);
                    }

                    var referenceDbColumnsNamed = scannerSettings.ReferenceDbColumnsNamed(member);

                    yield return(new ColumnDefinition()
                    {
                        MemberInfoChain = capturedMembers.Concat(new[] { member }).ToArray(),
                        MemberInfo = member,
                        IsComplexMapping = complexProperty,
                        IsReferenceMember = referenceProperty,
                        ReferenceType = ReferenceType.None,
                        ReferenceMember = null,
                        ResultColumn = scannerSettings.ResultPropertiesWhere(member),
                        DbColumnName = referenceProperty ? referenceDbColumnsNamed : null,
                    });
                }
                else
                {
                    var columnDefinition = new ColumnDefinition();
                    columnDefinition.MemberInfoChain = capturedMembers.Concat(new[] { member }).ToArray();
                    columnDefinition.MemberInfo      = member;

                    var prefixProperty = isReferenceProperty ? Enumerable.Empty <string>() : capturedMembers.Select(x => scannerSettings.DbColumnsNamed(x));
                    columnDefinition.DbColumnName = string.Join(PocoData.Separator, prefixProperty.Concat(new[] { scannerSettings.DbColumnsNamed(member) }).ToArray());

                    columnDefinition.DbColumnAlias      = scannerSettings.AliasNamed(member);
                    columnDefinition.IgnoreColumn       = scannerSettings.IgnorePropertiesWhere.Any(x => x.Invoke(member));
                    columnDefinition.DbColumnType       = scannerSettings.DbColumnTypesAs(member);
                    columnDefinition.ResultColumn       = scannerSettings.ResultPropertiesWhere(member);
                    columnDefinition.ComputedColumn     = scannerSettings.ComputedPropertiesWhere(member);
                    columnDefinition.ComputedColumnType = scannerSettings.ComputedPropertyTypeAs(member);
                    columnDefinition.VersionColumn      = scannerSettings.VersionPropertiesWhere(member);
                    columnDefinition.VersionColumnType  = scannerSettings.VersionPropertyTypeAs(member);
                    columnDefinition.ForceUtc           = scannerSettings.ForceDateTimesToUtcWhere(member);
                    columnDefinition.Serialized         = scannerSettings.SerializedWhere(member);
                    yield return(columnDefinition);
                }
            }
        }