/// <summary>Maps.</summary> /// <param name="bulkOperation">The bulk operation.</param> /// <param name="config">The configuration.</param> public void Map(BulkOperation bulkOperation, DapperPlusEntityMapper config) { var isMapModified = config.IsMapModified(); var isKeyModified = config.IsKeyModified(); var isOutputModified = config.IsOutputModified(); var isIdentityModified = config.IsIdentityModified(); var isIgnoreModified = config.IsIgnoreModified(); var isModified = config._columnMappings == null || isMapModified.HasValue && isMapModified.Value || isKeyModified.HasValue && isKeyModified.Value || isOutputModified.HasValue && isOutputModified.Value || isIdentityModified.HasValue && isIdentityModified.Value || isIgnoreModified.HasValue && isIgnoreModified.Value; if (!isModified) { return; } config._mapAccessors = config.GetMapAccessors(); config._keyAccessors = config.GetKeyAccessors(); config._outputAccessors = config.GetOutputAccessors(); config._identityAccessors = config.GetIdentityAccessors(); config._ignoreAccessors = config.GetIgnoreAccessors(); // SET AutoMap value AutoMap(config); var columnMappings = new List <DapperPlusColumnMapping>(); config._columnMappings = columnMappings; foreach (var accessor in config._mapAccessors) { if (accessor.IsCalculated) { // NOT Supported yet } else { columnMappings.Add(new DapperPlusColumnMapping { SourceName = accessor.ToString(), DestinationName = accessor.Member, Input = true }); } } foreach (var accessor in config._outputAccessors) { var columnMapping = columnMappings.Find(x => x.SourceName == accessor.ToString() && x.DestinationName == accessor.Member); if (columnMapping != null) { columnMapping.Output = true; } else { // CHECK smart mapping if (string.IsNullOrEmpty(accessor.Member) && columnMappings.Count(x => x.SourceName == accessor.ToString()) == 1) { columnMapping = columnMappings.Find(x => x.SourceName == accessor.ToString()); columnMapping.Output = true; } else { columnMappings.Add(new DapperPlusColumnMapping { SourceName = accessor.ToString(), DestinationName = accessor.Member, Output = true }); } } } foreach (var accessor in config._keyAccessors) { var columnMapping = columnMappings.Find(x => x.SourceName == accessor.ToString() && x.DestinationName == accessor.Member); if (columnMapping != null) { columnMapping.IsPrimaryKey = true; } else { // CHECK smart mapping if (string.IsNullOrEmpty(accessor.Member) && columnMappings.Count(x => x.SourceName == accessor.ToString()) == 1) { columnMapping = columnMappings.Find(x => x.SourceName == accessor.ToString()); columnMapping.IsPrimaryKey = true; } else { columnMappings.Add(new DapperPlusColumnMapping { SourceName = accessor.ToString(), DestinationName = accessor.Member, IsPrimaryKey = true }); } } } foreach (var accessor in config._identityAccessors) { var columnMapping = columnMappings.Find(x => x.SourceName == accessor.ToString() && x.DestinationName == accessor.Member); if (columnMapping != null) { columnMapping.IsIdentity = true; } else { // CHECK smart mapping if (string.IsNullOrEmpty(accessor.Member) && columnMappings.Count(x => x.SourceName == accessor.ToString()) == 1) { columnMapping = columnMappings.Find(x => x.SourceName == accessor.ToString()); columnMapping.IsIdentity = true; } else { columnMappings.Add(new DapperPlusColumnMapping { SourceName = accessor.ToString(), DestinationName = accessor.Member, IsIdentity = true }); } } } config._isIdentityModified = false; config._isIgnoreModified = false; config._isKeyModified = false; config._isMapModified = false; config._isOutputModified = false; }
/// <summary>Automatic map.</summary> /// <param name="entityMapper">The entity mapper.</param> public void AutoMap(DapperPlusEntityMapper entityMapper) { var entityType = entityMapper.GetType().GetGenericArguments()[0]; var properties = entityType.GetProperties(); #if NET45 // Key if (entityMapper._keyAccessors.Count == 0) { foreach (var property in properties) { if (property.GetIndexParameters().Length == 0 && !property.IsDefined(typeof(NotMappedAttribute)) && property.IsDefined(typeof(KeyAttribute)) ) { var destinationName = property.Name; #if NET45 if (property.IsDefined(typeof(ColumnAttribute))) { var attribute = (ColumnAttribute)property.GetCustomAttribute(typeof(ColumnAttribute)); if (!string.IsNullOrEmpty(attribute.Name)) { destinationName = attribute.Name; } } #endif entityMapper._keyAccessors.Add(new PropertyOrFieldAccessor(property) { Member = destinationName }); } } } #endif #if NET45 // Identity if (entityMapper._identityAccessors.Count == 0) { foreach (var property in properties) { if (property.GetIndexParameters().Length == 0 && !property.IsDefined(typeof(NotMappedAttribute)) && property.IsDefined(typeof(DatabaseGeneratedAttribute)) && ((DatabaseGeneratedAttribute)property.GetCustomAttribute(typeof(DatabaseGeneratedAttribute))).DatabaseGeneratedOption == DatabaseGeneratedOption.Identity) { var destinationName = property.Name; #if NET45 if (property.IsDefined(typeof(ColumnAttribute))) { var attribute = (ColumnAttribute)property.GetCustomAttribute(typeof(ColumnAttribute)); if (!string.IsNullOrEmpty(attribute.Name)) { destinationName = attribute.Name; } } #endif entityMapper._identityAccessors.Add(new PropertyOrFieldAccessor(property) { Member = destinationName }); } } } #endif #if NET45 // Output if (entityMapper._outputAccessors.Count == 0) { foreach (var property in properties) { if (property.GetIndexParameters().Length == 0 && !property.IsDefined(typeof(NotMappedAttribute)) && property.IsDefined(typeof(DatabaseGeneratedAttribute)) && ((DatabaseGeneratedAttribute)property.GetCustomAttribute(typeof(DatabaseGeneratedAttribute))).DatabaseGeneratedOption == DatabaseGeneratedOption.Computed) { var destinationName = property.Name; #if NET45 if (property.IsDefined(typeof(ColumnAttribute))) { var attribute = (ColumnAttribute)property.GetCustomAttribute(typeof(ColumnAttribute)); if (!string.IsNullOrEmpty(attribute.Name)) { destinationName = attribute.Name; } } #endif entityMapper._outputAccessors.Add(new PropertyOrFieldAccessor(property) { Member = destinationName }); } } } #endif // Map if (entityMapper._mapAccessors.Count == 0) { var ignoreAccessors = new HashSet <string>(); var outputAccessors = new HashSet <string>(); var identityAccessors = new HashSet <string>(); if (entityMapper._ignoreAccessors.Count > 0) { entityMapper._ignoreAccessors.ForEach(x => ignoreAccessors.Add(x.ToString())); } if (entityMapper._outputAccessors.Count > 0) { entityMapper._outputAccessors.ForEach(x => outputAccessors.Add(x.ToString())); } if (entityMapper._identityAccessors.Count > 0) { entityMapper._identityAccessors.ForEach(x => identityAccessors.Add(x.ToString())); } foreach (var property in properties) { if (property.GetIndexParameters().Length == 0 #if NET45 && !property.IsDefined(typeof(NotMappedAttribute)) #endif && !ignoreAccessors.Contains(property.Name) && !outputAccessors.Contains(property.Name)) { var destinationName = property.Name; #if NET45 if (property.IsDefined(typeof(ColumnAttribute))) { var attribute = (ColumnAttribute)property.GetCustomAttribute(typeof(ColumnAttribute)); if (!string.IsNullOrEmpty(attribute.Name)) { destinationName = attribute.Name; } } #endif entityMapper._mapAccessors.Add(new PropertyOrFieldAccessor(property) { Member = destinationName }); } } } }
/// <summary>Applies the configuration.</summary> /// <param name="bulkOperation">The bulk operation.</param> /// <param name="config">The configuration.</param> public void ApplyConfig(BulkOperation bulkOperation, DapperPlusEntityMapper config) { if (config == null) { return; } // Verify Column Mappings Map(bulkOperation, config); // Batch { var batchDelayInterval = config.BatchDelayInterval(); var batchSize = config.BatchSize(); var batchTimeout = config.BatchTimeout(); if (batchDelayInterval.HasValue) { bulkOperation.BatchDelayInterval = batchDelayInterval.Value; } if (batchSize.HasValue) { bulkOperation.BatchSize = batchSize.Value; } if (batchTimeout.HasValue) { bulkOperation.BatchTimeout = batchTimeout.Value; } } // Destination { var table = config.Table(); if (!string.IsNullOrEmpty(table)) { bulkOperation.DestinationTableName = table; } } // SqlServer { var sqlBulkCopyOptions = config.SqlBulkCopyOptions(); if (sqlBulkCopyOptions.HasValue) { bulkOperation.SqlBulkCopyOptions = sqlBulkCopyOptions.Value; } } // TemproaryTable { var temporaryTableBatchByTable = config.TemporaryTableBatchByTable(); var temporaryTableInsertBatchSize = config.TemporaryTableInsertBatchSize(); var temporaryTableMinRecord = config.TemporaryTableMinRecord(); var temporaryTableSchemaName = config.TemporaryTableSchemaName(); if (temporaryTableBatchByTable.HasValue) { bulkOperation.TemporaryTableBatchByTable = temporaryTableBatchByTable.Value; } if (temporaryTableInsertBatchSize.HasValue) { bulkOperation.TemporaryTableInsertBatchSize = temporaryTableInsertBatchSize.Value; } if (temporaryTableMinRecord.HasValue) { bulkOperation.TemporaryTableMinRecord = temporaryTableMinRecord.Value; } if (!string.IsNullOrEmpty(temporaryTableSchemaName)) { bulkOperation.TemporaryTableSchemaName = temporaryTableSchemaName; } } // Transient { var retryCount = config.RetryCount(); var retryInterval = config.RetryInterval(); if (retryCount.HasValue) { bulkOperation.RetryCount = retryCount.Value; } if (retryInterval.HasValue) { bulkOperation.RetryInterval = new TimeSpan(0, 0, 0, 0, retryInterval.Value); } } // Column Mapping { foreach (var column in config._columnMappings) { var bulkMapping = new ColumnMapping { SourceName = column.SourceName, DestinationName = column.DestinationName }; if (column.IsPrimaryKey) { bulkMapping.IsPrimaryKey = true; } if (column.Input && column.Output) { bulkMapping.Direction = ColumnMappingDirectionType.InputOutput; } else if (column.Output) { bulkMapping.Direction = ColumnMappingDirectionType.Output; } if (column.IsIdentity && (config == config._masterConfig._configInsert) || (config == config._masterConfig._configMerge)) { bulkMapping.Direction = ColumnMappingDirectionType.Output; } bulkOperation.ColumnMappings.Add(bulkMapping); } } }
/// <summary>Applies the configuration.</summary> /// <param name="bulkOperation">The bulk operation.</param> /// <param name="config">The configuration.</param> public void ApplyConfig(BulkOperation bulkOperation, DapperPlusEntityMapper config) { if (config == null) { return; } // Verify Column Mappings Map(bulkOperation, config); // Batch { var batchDelayInterval = config.BatchDelayInterval(); var batchSize = config.BatchSize(); var batchTimeout = config.BatchTimeout(); if (batchDelayInterval.HasValue) { bulkOperation.BatchDelayInterval = batchDelayInterval.Value; } if (batchSize.HasValue) { bulkOperation.BatchSize = batchSize.Value; } if (batchTimeout.HasValue) { bulkOperation.BatchTimeout = batchTimeout.Value; } } // Destination { var table = config.Table(); if (!string.IsNullOrEmpty(table)) { bulkOperation.DestinationTableName = table; } } // SqlServer { var sqlBulkCopyOptions = config.SqlBulkCopyOptions(); if (sqlBulkCopyOptions.HasValue) { bulkOperation.SqlBulkCopyOptions = sqlBulkCopyOptions.Value; } } // TemproaryTable { var temporaryTableBatchByTable = config.TemporaryTableBatchByTable(); var temporaryTableInsertBatchSize = config.TemporaryTableInsertBatchSize(); var temporaryTableMinRecord = config.TemporaryTableMinRecord(); var temporaryTableSchemaName = config.TemporaryTableSchemaName(); if (temporaryTableBatchByTable.HasValue) { bulkOperation.TemporaryTableBatchByTable = temporaryTableBatchByTable.Value; } if (temporaryTableInsertBatchSize.HasValue) { bulkOperation.TemporaryTableInsertBatchSize = temporaryTableInsertBatchSize.Value; } if (temporaryTableMinRecord.HasValue) { bulkOperation.TemporaryTableMinRecord = temporaryTableMinRecord.Value; } if (!string.IsNullOrEmpty(temporaryTableSchemaName)) { bulkOperation.TemporaryTableSchemaName = temporaryTableSchemaName; } } // Transient { var retryCount = config.RetryCount(); var retryInterval = config.RetryInterval(); if (retryCount.HasValue) { bulkOperation.RetryCount = retryCount.Value; } if (retryInterval.HasValue) { bulkOperation.RetryInterval = new TimeSpan(0, 0, 0, 0, retryInterval.Value); } } // Column Mapping { foreach (var column in config._columnMappings) { var bulkMapping = new ColumnMapping { SourceName = column.SourceName, DestinationName = column.DestinationName }; if (column.IsPrimaryKey) { bulkMapping.IsPrimaryKey = true; } if (column.Input && column.Output) { bulkMapping.Direction = ColumnMappingDirectionType.InputOutput; } else if (column.Output) { bulkMapping.Direction = ColumnMappingDirectionType.Output; } if (column.IsIdentity && (config == config._masterConfig._configInsert) || (config == config._masterConfig._configMerge)) { bulkMapping.Direction = ColumnMappingDirectionType.Output; } bulkOperation.ColumnMappings.Add(bulkMapping); } } }
/// <summary>Copy key configuration from an existing mapper.</summary> /// <param name="copyFromConfiguration">The configuration to copy from.</param> /// <returns>A DapperPlusEntityMapper<T></returns> public DapperPlusEntityMapper <T> Key(DapperPlusEntityMapper <T> copyFromConfiguration) { IsKeyModified(true); _key.AddRange(copyFromConfiguration._key); return(this); }
/// <summary>Copy map configuration from an existing mapper.</summary> /// <param name="copyFromConfiguration">The configuration to copy from.</param> /// <returns>A DapperPlusEntityMapper<T></returns> public DapperPlusEntityMapper <T> Map(DapperPlusEntityMapper <T> copyFromConfiguration) { IsMapModified(true); _map.AddRange(copyFromConfiguration._map); return(this); }
/// <summary>Copy ignore configuration from an existing mapper.</summary> /// <param name="copyFromConfiguration">The configuration to copy from.</param> /// <returns>A DapperPlusEntityMapper<T></returns> public DapperPlusEntityMapper <T> Ignore(DapperPlusEntityMapper <T> copyFromConfiguration) { IsIgnoreModified(true); _ignore.AddRange(copyFromConfiguration._ignore); return(this); }
/// <summary>Copy identity configuration from an existing mapper.</summary> /// <param name="copyFromConfiguration">The configuration to copy from.</param> /// <returns>A DapperPlusEntityMapper<T></returns> public DapperPlusEntityMapper <T> Identity(DapperPlusEntityMapper <T> copyFromConfiguration) { IsIdentityModified(true); _identity.AddRange(copyFromConfiguration._identity); return(this); }
/// <summary>Copy output configuration from an existing mapper.</summary> /// <param name="copyFromConfiguration">The configuration to copy from.</param> /// <returns>A DapperPlusEntityMapper<T></returns> public DapperPlusEntityMapper <T> Output(DapperPlusEntityMapper <T> copyFromConfiguration) { IsOutputModified(true); _output.AddRange(copyFromConfiguration._output); return(this); }
/// <summary>Maps.</summary> /// <param name="bulkOperation">The bulk operation.</param> /// <param name="config">The configuration.</param> public void Map(BulkOperation bulkOperation, DapperPlusEntityMapper config) { var isMapModified = config.IsMapModified(); var isKeyModified = config.IsKeyModified(); var isOutputModified = config.IsOutputModified(); var isIdentityModified = config.IsIdentityModified(); var isIgnoreModified = config.IsIgnoreModified(); var isModified = isMapModified.HasValue && isMapModified.Value || isKeyModified.HasValue && isKeyModified.Value || isOutputModified.HasValue && isOutputModified.Value || isIdentityModified.HasValue && isIdentityModified.Value || isIgnoreModified.HasValue && isIgnoreModified.Value; if (!isModified) return; config._mapAccessors = config.GetMapAccessors(); config._keyAccessors = config.GetKeyAccessors(); config._outputAccessors = config.GetOutputAccessors(); config._identityAccessors = config.GetIdentityAccessors(); config._ignoreAccessors = config.GetIgnoreAccessors(); // SET AutoMap value AutoMap(config); var columnMappings = new List<DapperPlusColumnMapping>(); config._columnMappings = columnMappings; foreach (var accessor in config._mapAccessors) { if (accessor.IsCalculated) { // NOT Supported yet } else { columnMappings.Add(new DapperPlusColumnMapping {SourceName = accessor.ToString(), DestinationName = accessor.Member, Input = true}); } } foreach (var accessor in config._outputAccessors) { var columnMapping = columnMappings.Find(x => x.SourceName == accessor.ToString() && x.DestinationName == accessor.Member); if (columnMapping != null) { columnMapping.Output = true; } else { columnMappings.Add(new DapperPlusColumnMapping {SourceName = accessor.ToString(), DestinationName = accessor.Member, Output = true}); } } foreach (var accessor in config._keyAccessors) { var columnMapping = columnMappings.Find(x => x.SourceName == accessor.ToString() && x.DestinationName == accessor.Member); if (columnMapping != null) { columnMapping.IsPrimaryKey = true; } else { columnMappings.Add(new DapperPlusColumnMapping {SourceName = accessor.ToString(), DestinationName = accessor.Member, IsPrimaryKey = true}); } } foreach (var accessor in config._identityAccessors) { var columnMapping = columnMappings.Find(x => x.SourceName == accessor.ToString() && x.DestinationName == accessor.Member); if (columnMapping != null) { columnMapping.IsIdentity = true; } else { columnMappings.Add(new DapperPlusColumnMapping {SourceName = accessor.ToString(), DestinationName = accessor.Member, IsIdentity = true}); } } config._isIdentityModified = false; config._isIgnoreModified = false; config._isKeyModified = false; config._isMapModified = false; config._isOutputModified = false; }
/// <summary>Automatic map.</summary> /// <param name="entityMapper">The entity mapper.</param> public void AutoMap(DapperPlusEntityMapper entityMapper) { var entityType = entityMapper.GetType().GetGenericArguments()[0]; var properties = entityType.GetProperties(); #if NET45 // Key if (entityMapper._keyAccessors.Count == 0) { foreach (var property in properties) { if (property.GetIndexParameters().Length == 0 && !property.IsDefined(typeof (NotMappedAttribute)) && property.IsDefined(typeof (KeyAttribute)) ) { var destinationName = property.Name; #if NET45 if (property.IsDefined(typeof (ColumnAttribute))) { var attribute = (ColumnAttribute) property.GetCustomAttribute(typeof (ColumnAttribute)); if (!string.IsNullOrEmpty(attribute.Name)) { destinationName = attribute.Name; } } #endif entityMapper._keyAccessors.Add(new PropertyOrFieldAccessor(property) {Member = destinationName}); } } } #endif #if NET45 // Identity if (entityMapper._identityAccessors.Count == 0) { foreach (var property in properties) { if (property.GetIndexParameters().Length == 0 && !property.IsDefined(typeof (NotMappedAttribute)) && property.IsDefined(typeof (DatabaseGeneratedAttribute)) && ((DatabaseGeneratedAttribute) property.GetCustomAttribute(typeof (DatabaseGeneratedAttribute))).DatabaseGeneratedOption == DatabaseGeneratedOption.Identity) { var destinationName = property.Name; #if NET45 if (property.IsDefined(typeof (ColumnAttribute))) { var attribute = (ColumnAttribute) property.GetCustomAttribute(typeof (ColumnAttribute)); if (!string.IsNullOrEmpty(attribute.Name)) { destinationName = attribute.Name; } } #endif entityMapper._identityAccessors.Add(new PropertyOrFieldAccessor(property) {Member = destinationName}); } } } #endif #if NET45 // Output if (entityMapper._outputAccessors.Count == 0) { foreach (var property in properties) { if (property.GetIndexParameters().Length == 0 && !property.IsDefined(typeof (NotMappedAttribute)) && property.IsDefined(typeof (DatabaseGeneratedAttribute)) && ((DatabaseGeneratedAttribute) property.GetCustomAttribute(typeof (DatabaseGeneratedAttribute))).DatabaseGeneratedOption == DatabaseGeneratedOption.Computed) { var destinationName = property.Name; #if NET45 if (property.IsDefined(typeof (ColumnAttribute))) { var attribute = (ColumnAttribute) property.GetCustomAttribute(typeof (ColumnAttribute)); if (!string.IsNullOrEmpty(attribute.Name)) { destinationName = attribute.Name; } } #endif entityMapper._outputAccessors.Add(new PropertyOrFieldAccessor(property) {Member = destinationName}); } } } #endif // Map if (entityMapper._mapAccessors.Count == 0) { var ignoreAccessors = new HashSet<string>(); var outputAccessors = new HashSet<string>(); var identityAccessors = new HashSet<string>(); if (entityMapper._ignoreAccessors.Count > 0) { entityMapper._ignoreAccessors.ForEach(x => ignoreAccessors.Add(x.ToString())); } if (entityMapper._outputAccessors.Count > 0) { entityMapper._outputAccessors.ForEach(x => outputAccessors.Add(x.ToString())); } if (entityMapper._identityAccessors.Count > 0) { entityMapper._identityAccessors.ForEach(x => identityAccessors.Add(x.ToString())); } foreach (var property in properties) { if (property.GetIndexParameters().Length == 0 #if NET45 && !property.IsDefined(typeof (NotMappedAttribute)) #endif && !ignoreAccessors.Contains(property.Name) && !outputAccessors.Contains(property.Name)) { var destinationName = property.Name; #if NET45 if (property.IsDefined(typeof (ColumnAttribute))) { var attribute = (ColumnAttribute) property.GetCustomAttribute(typeof (ColumnAttribute)); if (!string.IsNullOrEmpty(attribute.Name)) { destinationName = attribute.Name; } } #endif entityMapper._mapAccessors.Add(new PropertyOrFieldAccessor(property) {Member = destinationName}); } } } }