예제 #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;
        }
        private static IEnumerable <ColumnDefinition> GetColumnDefinitions(ConventionScannerSettings scannerSettings, Type type, List <MemberInfo> memberInfos, bool isReferenceProperty = false)
        {
            MemberInfo[] capturedMembers = memberInfos.ToArray();
            foreach (MemberInfo member in ReflectionUtils.GetFieldsAndPropertiesForClasses(type))
            {
                bool complexProperty   = scannerSettings.ComplexPropertiesWhere(member);
                bool referenceProperty = scannerSettings.ReferencePropertiesWhere(member);
                bool dbColumn          = scannerSettings.DbColumnWhere(member);

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

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

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

                    List <ColumnDefinition> columnDefinitions = GetColumnDefinitions(scannerSettings, memberInfoType, members, referenceProperty).ToList();

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

                    string 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,
                        DbColumnName = referenceProperty ? referenceDbColumnsNamed : null,
                    });
                }
                else
                {
                    ColumnDefinition columnDefinition = new ColumnDefinition();
                    columnDefinition.MemberInfoChain = capturedMembers.Concat(new[] { member }).ToArray();
                    columnDefinition.MemberInfo      = member;

                    IEnumerable <string> 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);
                }
            }
        }