Beispiel #1
11
        /// <summary>
        /// Initialises a new instance of the <see cref="TableInfo"/> class.
        /// </summary>
        /// <param name="columns">The columns that are mapped for the table.</param>
        /// <param name="identifierStrategy">The identifier strategy used by the table.</param>
        /// <param name="name">The name of the table.</param>
        /// <param name="schema">The database schema the table exists within (e.g. 'dbo'); otherwise null.</param>
        /// <exception cref="ArgumentNullException">Thrown if columns or name are null.</exception>
        /// <exception cref="MappingException">Thrown if no there is a problem with the column mappings.</exception>
        public TableInfo(
            IList<ColumnInfo> columns,
            IdentifierStrategy identifierStrategy,
            string name,
            string schema)
        {
            if (columns == null)
            {
                throw new ArgumentNullException("columns");
            }

            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            this.columns = new ReadOnlyCollection<ColumnInfo>(columns);
            this.identifierStrategy = identifierStrategy;
            this.name = name;
            this.schema = schema;

            this.identifierColumn = columns.FirstOrDefault(c => c.IsIdentifier);

            this.insertColumnCount = columns.Count(c => c.AllowInsert);
            this.updateColumnCount = columns.Count(c => c.AllowUpdate);

            this.ValidateColumns();
        }
Beispiel #2
0
 public static ConventionMappingSettings GetConventionMappingSettings(IdentifierStrategy identifierStrategy)
 {
     return(new ConventionMappingSettings
     {
         AllowInsert = (PropertyInfo p) =>
         {
             return p.Name != "Updated";
         },
         AllowUpdate = (PropertyInfo p) =>
         {
             return p.Name != "Created";
         },
         ResolveIdentifierStrategy = (Type type) =>
         {
             return identifierStrategy;
         },
         ResolveSequenceName = (PropertyInfo propertyInfo) =>
         {
             return identifierStrategy == IdentifierStrategy.Sequence
                 ? propertyInfo.DeclaringType.Name + "_" + propertyInfo.Name + "_Sequence"
                 : null;
         },
         ResolveTableSchema = (Type type) =>
         {
             return "Sales";
         }
     });
 }
Beispiel #3
0
        /// <summary>
        /// Initialises a new instance of the <see cref="TableInfo"/> class.
        /// </summary>
        /// <param name="columns">The columns that are mapped for the table.</param>
        /// <param name="identifierStrategy">The identifier strategy used by the table.</param>
        /// <param name="name">The name of the table.</param>
        /// <param name="schema">The database schema the table exists within (e.g. 'dbo'); otherwise null.</param>
        /// <exception cref="ArgumentNullException">Thrown if columns or name are null.</exception>
        /// <exception cref="MappingException">Thrown if no there is a problem with the column mappings.</exception>
        public TableInfo(
            IList <ColumnInfo> columns,
            IdentifierStrategy identifierStrategy,
            string name,
            string schema)
        {
            if (columns == null)
            {
                throw new ArgumentNullException("columns");
            }

            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            this.columns            = new ReadOnlyCollection <ColumnInfo>(columns);
            this.identifierStrategy = identifierStrategy;
            this.name   = name;
            this.schema = schema;

            this.identifierColumn = columns.FirstOrDefault(c => c.IsIdentifier);

            this.insertColumnCount = columns.Count(c => c.AllowInsert);
            this.updateColumnCount = columns.Count(c => c.AllowUpdate);

            this.ValidateColumns();
        }
        public IObjectInfo CreateObjectInfo(Type forType)
        {
            if (forType is null)
            {
                throw new ArgumentNullException(nameof(forType));
            }

            TableAttribute tableAttribute = forType.GetAttribute <TableAttribute>(inherit: false);

            if (tableAttribute is null)
            {
                throw new MappingException(ExceptionMessages.AttributeMappingConvention_NoTableAttribute.FormatWith(forType.FullName));
            }

            IdentifierStrategy identifierStrategy = IdentifierStrategy.DbGenerated;
            List <ColumnInfo>  columns            = CreateColumnInfos(forType, ref identifierStrategy);

            var tableInfo = new TableInfo(columns, identifierStrategy, tableAttribute.Name, tableAttribute.Schema);

            if (_log.IsDebug)
            {
                _log.Debug(LogMessages.MappingConvention_MappingTypeToTable, forType.FullName, tableInfo.Schema, tableInfo.Name);
            }

            return(new PocoObjectInfo(forType, tableInfo));
        }
Beispiel #5
0
 public static ConventionMappingSettings GetConventionMappingSettings(IdentifierStrategy identifierStrategy)
 {
     return new ConventionMappingSettings
     {
         AllowInsert = (PropertyInfo p) =>
         {
             return p.Name != "Updated";
         },
         AllowUpdate = (PropertyInfo p) =>
         {
             return p.Name != "Created";
         },
         ResolveIdentifierStrategy = (Type type) =>
         {
             return identifierStrategy;
         },
         ResolveSequenceName = (PropertyInfo propertyInfo) =>
         {
             return identifierStrategy == IdentifierStrategy.Sequence
                 ? propertyInfo.DeclaringType.Name + "_" + propertyInfo.Name + "_Sequence"
                 : null;
         },
         ResolveTableSchema = (Type type) =>
         {
             return "Sales";
         }
     };
 }
        private List<ColumnInfo> CreateColumnInfos(Type forType, ref IdentifierStrategy identifierStrategy)
        {
            var properties = forType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            var columns = new List<ColumnInfo>(properties.Length);

            foreach (var property in properties.OrderBy(p => p.Name))
            {
                if (!property.CanRead || !property.CanWrite)
                {
                    if (this.log.IsDebug)
                    {
                        this.log.Debug(LogMessages.MappingConvention_PropertyNotGetAndSet, forType.Name, property.Name);
                    }

                    continue;
                }

                var columnAttribute = property.GetAttribute<ColumnAttribute>(inherit: true);

                if (columnAttribute == null)
                {
                    if (this.log.IsDebug)
                    {
                        this.log.Debug(LogMessages.AttributeMappingConvention_IgnoringProperty, forType.FullName, property.Name);
                    }

                    continue;
                }

                var identifierAttribute = property.GetAttribute<IdentifierAttribute>(inherit: true);

                if (identifierAttribute != null)
                {
                    identifierStrategy = identifierAttribute.IdentifierStrategy;
                }

                var columnInfo = new ColumnInfo(
                    columnName: columnAttribute.Name,
                    dbType: TypeConverter.ResolveDbType(property.PropertyType),
                    propertyInfo: property,
                    isIdentifier: identifierAttribute != null,
                    allowInsert: identifierAttribute != null ? identifierStrategy == IdentifierStrategy.Assigned : columnAttribute.AllowInsert,
                    allowUpdate: identifierAttribute != null ? false : columnAttribute.AllowUpdate,
                    sequenceName: identifierAttribute != null ? identifierAttribute.SequenceName : null);

                if (this.log.IsDebug)
                {
                    this.log.Debug(LogMessages.MappingConvention_MappingColumnToProperty, forType.Name, columnInfo.PropertyInfo.Name, columnInfo.ColumnName);
                }

                columns.Add(columnInfo);
            }

            return columns;
        }
        private List <ColumnInfo> CreateColumnInfos(Type forType, ref IdentifierStrategy identifierStrategy)
        {
            PropertyInfo[] properties = forType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            var            columns    = new List <ColumnInfo>(properties.Length);

            foreach (PropertyInfo property in properties.OrderBy(p => p.Name))
            {
                if (!property.CanRead || !property.CanWrite)
                {
                    if (_log.IsDebug)
                    {
                        _log.Debug(LogMessages.MappingConvention_PropertyNotGetAndSet, forType.Name, property.Name);
                    }

                    continue;
                }

                ColumnAttribute columnAttribute = property.GetAttribute <ColumnAttribute>(inherit: true);

                if (columnAttribute is null)
                {
                    if (_log.IsDebug)
                    {
                        _log.Debug(LogMessages.AttributeMappingConvention_IgnoringProperty, forType.FullName, property.Name);
                    }

                    continue;
                }

                IdentifierAttribute identifierAttribute = property.GetAttribute <IdentifierAttribute>(inherit: true);

                if (identifierAttribute != null)
                {
                    identifierStrategy = identifierAttribute.IdentifierStrategy;
                }

                var columnInfo = new ColumnInfo(
                    columnName: columnAttribute.Name,
                    dbType: TypeConverter.ResolveDbType(property.PropertyType),
                    propertyInfo: property,
                    isIdentifier: identifierAttribute != null,
                    allowInsert: identifierAttribute != null ? identifierStrategy == IdentifierStrategy.Assigned : columnAttribute.AllowInsert,
                    allowUpdate: identifierAttribute == null && columnAttribute.AllowUpdate,
                    sequenceName: identifierAttribute?.SequenceName);

                if (_log.IsDebug)
                {
                    _log.Debug(LogMessages.MappingConvention_MappingColumnToProperty, forType.Name, columnInfo.PropertyInfo.Name, columnInfo.ColumnName);
                }

                columns.Add(columnInfo);
            }

            return(columns);
        }
        private List<ColumnInfo> CreateColumnInfos(Type forType, IdentifierStrategy identifierStrategy)
        {
            var properties = forType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            var columns = new List<ColumnInfo>(properties.Length);

            foreach (var property in properties.OrderBy(p => p.Name))
            {
                if (!property.CanRead || !property.CanWrite)
                {
                    if (this.log.IsDebug)
                    {
                        this.log.Debug(LogMessages.MappingConvention_PropertyNotGetAndSet, forType.Name, property.Name);
                    }

                    continue;
                }

                if (this.settings.Ignore(property))
                {
                    if (this.log.IsDebug)
                    {
                        this.log.Debug(LogMessages.ConventionMappingConvention_IgnoringProperty, forType.Name, property.Name);
                    }

                    continue;
                }

                var isIdentifier = this.settings.IsIdentifier(property);

                var columnInfo = new ColumnInfo(
                    columnName: isIdentifier ? this.settings.ResolveIdentifierColumnName(property) : this.settings.ResolveColumnName(property),
                    dbType: this.settings.ResolveDbType(property),
                    propertyInfo: property,
                    isIdentifier: isIdentifier,
                    allowInsert: isIdentifier ? identifierStrategy == IdentifierStrategy.Assigned : this.settings.AllowInsert(property),
                    allowUpdate: isIdentifier ? false : this.settings.AllowUpdate(property),
                    sequenceName: isIdentifier && identifierStrategy == IdentifierStrategy.Sequence ? this.settings.ResolveSequenceName(property) : null);

                if (this.log.IsDebug)
                {
                    this.log.Debug(LogMessages.MappingConvention_MappingColumnToProperty, forType.Name, columnInfo.PropertyInfo.Name, columnInfo.ColumnName);
                }

                columns.Add(columnInfo);
            }

            return columns;
        }
Beispiel #9
0
        private List <ColumnInfo> CreateColumnInfos(Type forType, IdentifierStrategy identifierStrategy)
        {
            var properties = forType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            var columns    = new List <ColumnInfo>(properties.Length);

            foreach (var property in properties.OrderBy(p => p.Name))
            {
                if (!property.CanRead || !property.CanWrite)
                {
                    if (this.log.IsDebug)
                    {
                        this.log.Debug(LogMessages.MappingConvention_PropertyNotGetAndSet, forType.Name, property.Name);
                    }

                    continue;
                }

                if (this.settings.Ignore(property))
                {
                    if (this.log.IsDebug)
                    {
                        this.log.Debug(LogMessages.ConventionMappingConvention_IgnoringProperty, forType.Name, property.Name);
                    }

                    continue;
                }

                var isIdentifier = this.settings.IsIdentifier(property);

                var columnInfo = new ColumnInfo(
                    columnName: isIdentifier ? this.settings.ResolveIdentifierColumnName(property) : this.settings.ResolveColumnName(property),
                    dbType: this.settings.ResolveDbType(property),
                    propertyInfo: property,
                    isIdentifier: isIdentifier,
                    allowInsert: isIdentifier ? identifierStrategy == IdentifierStrategy.Assigned : this.settings.AllowInsert(property),
                    allowUpdate: isIdentifier ? false : this.settings.AllowUpdate(property),
                    sequenceName: isIdentifier && identifierStrategy == IdentifierStrategy.Sequence ? this.settings.ResolveSequenceName(property) : null);

                if (this.log.IsDebug)
                {
                    this.log.Debug(LogMessages.MappingConvention_MappingColumnToProperty, forType.Name, columnInfo.PropertyInfo.Name, columnInfo.ColumnName);
                }

                columns.Add(columnInfo);
            }

            return(columns);
        }
Beispiel #10
0
        /// <summary>
        /// Initialises a new instance of the <see cref="TableInfo"/> class.
        /// </summary>
        /// <param name="columns">The columns that are mapped for the table.</param>
        /// <param name="identifierStrategy">The identifier strategy used by the table.</param>
        /// <param name="name">The name of the table.</param>
        /// <param name="schema">The database schema the table exists within (e.g. 'dbo'); otherwise null.</param>
        /// <exception cref="ArgumentNullException">Thrown if columns or name are null.</exception>
        /// <exception cref="MappingException">Thrown if no there is a problem with the column mappings.</exception>
        public TableInfo(
            IList <ColumnInfo> columns,
            IdentifierStrategy identifierStrategy,
            string name,
            string schema)
        {
            Columns            = new ReadOnlyCollection <ColumnInfo>(columns ?? throw new ArgumentNullException(nameof(columns)));
            IdentifierStrategy = identifierStrategy;
            Name   = name ?? throw new ArgumentNullException(nameof(name));
            Schema = schema;

            IdentifierColumn = columns.FirstOrDefault(c => c.IsIdentifier);

            InsertColumnCount = columns.Count(c => c.AllowInsert);
            UpdateColumnCount = columns.Count(c => c.AllowUpdate);

            ValidateColumns();
        }
Beispiel #11
0
 public static ConventionMappingSettings GetConventionMappingSettings(IdentifierStrategy identifierStrategy)
 {
     return new ConventionMappingSettings
     {
         AllowInsert = (PropertyInfo p) =>
         {
             return p.Name != "Updated";
         },
         AllowUpdate = (PropertyInfo p) =>
         {
             return p.Name != "Created";
         },
         ResolveIdentifierStrategy = (Type type) =>
         {
             return identifierStrategy;
         },
         ResolveTableSchema = (Type type) =>
         {
             return "Sales";
         }
     };
 }
 public WhenConstructedWithIdentifierStrategyAndSequenceName()
 {
     this.identifierStrategy = IdentifierStrategy.Assigned;
     this.sequenceName = "CustomerIdSequence";
     this.identifierAttribute = new IdentifierAttribute(this.identifierStrategy, this.sequenceName);
 }
 /// <summary>
 /// Initialises a new instance of the <see cref="IdentifierAttribute"/> class.
 /// </summary>
 /// <param name="identifierStrategy">The identifier strategy used to manage the identifier's value.</param>
 public IdentifierAttribute(IdentifierStrategy identifierStrategy)
     : this(identifierStrategy, null)
 {
 }
 public WhenConstructedWithIdentifierStrategy()
 {
     this.identifierStrategy  = IdentifierStrategy.Assigned;
     this.identifierAttribute = new IdentifierAttribute(this.identifierStrategy);
 }
 /// <summary>
 /// Initialises a new instance of the <see cref="IdentifierAttribute"/> class.
 /// </summary>
 /// <param name="identifierStrategy">The identifier strategy used to manage the identifier's value.</param>
 public IdentifierAttribute(IdentifierStrategy identifierStrategy)
 {
     this.identifierStrategy = identifierStrategy;
 }
 public WhenConstructedWithIdentifierStrategyAndSequenceName()
 {
     this.identifierStrategy  = IdentifierStrategy.Assigned;
     this.sequenceName        = "CustomerIdSequence";
     this.identifierAttribute = new IdentifierAttribute(this.identifierStrategy, this.sequenceName);
 }
Beispiel #17
0
 /// <summary>
 /// Initialises a new instance of the <see cref="IdentifierAttribute"/> class.
 /// </summary>
 /// <param name="identifierStrategy">The identifier strategy used to manage the identifier's value.</param>
 public IdentifierAttribute(IdentifierStrategy identifierStrategy)
     : this(identifierStrategy, null)
 {
 }
 public WhenConstructedWithIdentifierStrategy()
 {
     this.identifierStrategy = IdentifierStrategy.Assigned;
     this.identifierAttribute = new IdentifierAttribute(this.identifierStrategy);
 }
Beispiel #19
0
 /// <summary>
 /// Initialises a new instance of the <see cref="IdentifierAttribute"/> class.
 /// </summary>
 /// <param name="identifierStrategy">The identifier strategy used to manage the identifier's value.</param>
 /// <param name="sequenceName">The name of the sequence which generates the identifier value.</param>
 public IdentifierAttribute(IdentifierStrategy identifierStrategy, string sequenceName)
 {
     this.identifierStrategy = identifierStrategy;
     this.sequenceName       = sequenceName;
 }
 /// <summary>
 /// Initialises a new instance of the <see cref="IdentifierAttribute"/> class.
 /// </summary>
 /// <param name="identifierStrategy">The identifier strategy used to manage the identifier's value.</param>
 /// <param name="sequenceName">The name of the sequence which generates the identifier value.</param>
 public IdentifierAttribute(IdentifierStrategy identifierStrategy, string sequenceName)
 {
     this.identifierStrategy = identifierStrategy;
     this.sequenceName = sequenceName;
 }