/// <summary> /// Maps between two entities. /// </summary> /// <typeparam name="TSource">Type of the source entity.</typeparam> /// <typeparam name="TTarget">Type of the target entity.</typeparam> /// <param name="source">The source entity.</param> /// <param name="target">The target entity.</param> /// <param name="columnMapper">Delegate to map between entities. If <see langword="null"/>, default value will be used to map insertable columns.</param> /// <param name="isInsertable">Specifies whether the columns are insertable.</param> /// <returns>A list of <see cref="ColumnMapping"/> between two entities.</returns> public static IReadOnlyList <ColumnMapping> Map <TSource, TTarget>(TSource source, TTarget target, Action <ColumnMapper, TSource, TTarget> columnMapper, bool isInsertable) where TSource : class, IEntity, new() where TTarget : class, IEntity, new() { target.VerifyNotNull(nameof(target)); source.VerifyNotNull(nameof(source)); if (columnMapper == null) { return(GetColumnMappings(source.Model, target.Model, isInsertable)); } var result = new ColumnMapper(source.Model, target.Model).Build(builder => columnMapper(builder, source, target)); var columns = isInsertable ? target.Model.GetInsertableColumns() : target.Model.GetUpdatableColumns(); var targetModelIds = new HashSet <ColumnId>(columns.Select(x => x.Id)); foreach (var resultItem in result) { if (!targetModelIds.Contains(resultItem.Target.Id)) { throw new InvalidOperationException(DiagnosticMessages.ColumnMapper_InvalidTarget(resultItem.Target)); } } return(result); }
private void VerifyTarget(Column targetColumn, string paramName) { targetColumn.VerifyNotNull(paramName); if (targetColumn.ParentModel != _targetModel) { throw new ArgumentException(DiagnosticMessages.ColumnMapper_InvalidTarget(targetColumn), paramName); } }