private static ConventionScannerSettings ProcessSettings(Action <IConventionScanner> scanner)
        {
            ConventionScannerSettings defaultScannerSettings = new ConventionScannerSettings
            {
                PrimaryKeysAutoIncremented = x => true,
                PrimaryKeysNamed           = x => "ID",
                TablesNamed              = x => x.Name,
                DbColumnsNamed           = x => x.Name,
                AliasNamed               = x => null,
                DbColumnTypesAs          = x => null,
                ResultPropertiesWhere    = x => false,
                VersionPropertiesWhere   = x => false,
                VersionPropertyTypeAs    = x => VersionColumnType.Number,
                ComputedPropertiesWhere  = x => false,
                ComputedPropertyTypeAs   = x => ComputedColumnType.Always,
                ForceDateTimesToUtcWhere = x => true,
                ReferencePropertiesWhere = x => x.GetMemberInfoType().IsAClass() && ReflectionUtils.GetCustomAttributes(x, typeof(ReferenceAttribute)).Any(),
                ComplexPropertiesWhere   = x => x.GetMemberInfoType().IsAClass() && ReflectionUtils.GetCustomAttributes(x, typeof(ComplexMappingAttribute)).Any(),
                ReferenceDbColumnsNamed  = x => x.Name + "ID",
                SequencesNamed           = x => null,
                UseOutputClauseWhere     = x => false,
                SerializedWhere          = x => ReflectionUtils.GetCustomAttributes(x, typeof(SerializedColumnAttribute)).Any(),
                DbColumnWhere            = x => ReflectionUtils.GetCustomAttributes(x, typeof(ColumnAttribute)).Any(),
                Lazy = false
            };

            scanner.Invoke(new ConventionScanner(defaultScannerSettings));
            return(defaultScannerSettings);
        }
        public static FluentConfig Scan(Action <IConventionScanner> scanner)
        {
            ConventionScannerSettings scannerSettings = ProcessSettings(scanner);

            if (scannerSettings.Lazy)
            {
                Mappings lazyPocoMappings = new Mappings();
                return(SetFactory(lazyPocoMappings, scanner));
            }

            return(Configure(CreateMappings(scannerSettings, null)));
        }
        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 <Type> FindTypes(ConventionScannerSettings scannerSettings)
        {
            if (scannerSettings.TheCallingAssembly)
            {
                scannerSettings.Assemblies.Add(FindTheCallingAssembly());
            }

            var types = scannerSettings.Assemblies
                        .SelectMany(x => x.GetExportedTypes())
                        .Where(x => scannerSettings.IncludeTypes.All(y => y.Invoke(x)))
                        .Where(x => !x.IsNested && !typeof(Map <>).IsAssignableFrom(x) && !typeof(Mappings).IsAssignableFrom(x));

            return(types);
        }
        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)
        {
            IEnumerable <Type> types = typesOverride ?? FindTypes(scannerSettings);
            Dictionary <Type, TypeDefinition> config = new Dictionary <Type, TypeDefinition>();

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

                foreach (ColumnDefinition columnDefinition in GetColumnDefinitions(scannerSettings, type, new List <MemberInfo>()))
                {
                    string key = PocoColumn.GenerateKey(columnDefinition.MemberInfoChain);
                    if (!pocoDefn.ColumnConfiguration.ContainsKey(key))
                    {
                        pocoDefn.ColumnConfiguration.Add(key, columnDefinition);
                    }
                }

                config.Add(type, pocoDefn);
            }

            foreach (Mappings mappingOverride in scannerSettings.MappingOverrides)
            {
                MergeOverrides(config, mappingOverride);
            }

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

            Mappings pocoMappings = new Mappings {
                Config = config
            };

            return(pocoMappings);
        }
        private static ConventionScannerSettings ProcessSettings(Action <IConventionScanner> scanner)
        {
            var defaultScannerSettings = new ConventionScannerSettings
            {
                PrimaryKeysAutoIncremented = x => true,
                PrimaryKeysNamed           = x => "ID",
                TablesNamed              = x => x.Name,
                PropertiesNamed          = x => x.Name,
                AliasNamed               = x => null,
                DbColumnTypesAs          = x => null,
                ResultPropertiesWhere    = x => false,
                VersionPropertiesWhere   = x => false,
                VersionColumnTypeAs      = x => VersionColumnType.Number,
                ComputedPropertiesWhere  = x => false,
                ForceDateTimesToUtcWhere = x => true,
                SequencesNamed           = x => null,
                Lazy = false
            };

            scanner.Invoke(new ConventionScanner(defaultScannerSettings));
            return(defaultScannerSettings);
        }
        private static FluentConfig SetFactory(Mappings mappings, Action <IConventionScanner> scanner)
        {
            Mappings maps = mappings;
            Action <IConventionScanner> scana = scanner;

            return(new FluentConfig(mapper => new FluentPocoDataFactory((t, pocoDataFactory) =>
            {
                if (maps != null)
                {
                    if (maps.Config.ContainsKey(t))
                    {
                        return new FluentMappingsPocoDataBuilder(t, mappings, mapper).Init();
                    }

                    if (scana != null)
                    {
                        ConventionScannerSettings settings = ProcessSettings(scana);
                        Mappings typeMapping = CreateMappings(settings, new[] { t });
                        return new FluentMappingsPocoDataBuilder(t, typeMapping, mapper).Init();
                    }
                }
                return new PocoDataBuilder(t, mapper).Init();
            })));
        }
        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);
                }
            }
        }
 private static ConventionScannerSettings ProcessSettings(Action<IConventionScanner> scanner)
 {
     var defaultScannerSettings = new ConventionScannerSettings
     {
         PrimaryKeysAutoIncremented = x => true,
         PrimaryKeysNamed = x => "ID",
         TablesNamed = x => x.Name,
         PropertiesNamed = x => x.Name,
         AliasNamed = x => null,
         DbColumnTypesAs = x => null,
         ResultPropertiesWhere = x => false,
         VersionPropertiesWhere = x => false,
         VersionColumnTypeAs = x => VersionColumnType.Number,
         ComputedPropertiesWhere = x => false,
         ForceDateTimesToUtcWhere = x => true,
         SequencesNamed = x => null,
         Lazy = false
     };
     scanner.Invoke(new ConventionScanner(defaultScannerSettings));
     return defaultScannerSettings;
 }
        private static IEnumerable<Type> FindTypes(ConventionScannerSettings scannerSettings)
        {
            if (scannerSettings.TheCallingAssembly)
                scannerSettings.Assemblies.Add(FindTheCallingAssembly());

            var types = scannerSettings.Assemblies
                .SelectMany(x => x.GetExportedTypes())
                .Where(x => scannerSettings.IncludeTypes.All(y => y.Invoke(x)))
                .Where(x => !x.IsNested && !typeof (Map<>).IsAssignableFrom(x) && !typeof (Mappings).IsAssignableFrom(x));
            return types;
        }
Exemple #12
0
 public ConventionScanner(ConventionScannerSettings scannerSettings)
 {
     this._scannerSettings = scannerSettings;
 }
 public ConventionScanner(ConventionScannerSettings scannerSettings)
 {
     _scannerSettings = scannerSettings;
 }
 public PropertyBuilderConventions(ConventionScannerSettings scannerSettings)
 {
     _scannerSettings = scannerSettings;
 }
 public PropertyBuilderConventions(ConventionScannerSettings scannerSettings)
 {
     this._scannerSettings = scannerSettings;
 }