• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / FluentCommand / 16301005024

15 Jul 2025 06:03PM UTC coverage: 54.951%. First build
16301005024

push

github

pwelter34
import data tweaks, xml doc updates

1716 of 3630 branches covered (47.27%)

Branch coverage included in aggregate %.

78 of 143 new or added lines in 11 files covered. (54.55%)

4361 of 7429 relevant lines covered (58.7%)

231.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.92
/src/FluentCommand.SqlServer/Import/ImportDefinitionBuilder.cs
1
using System.Linq.Expressions;
2

3
using FluentCommand.Extensions;
4
using FluentCommand.Reflection;
5

6
namespace FluentCommand.Import;
7

8
/// <summary>
9
/// Provides a fluent API for configuring and building an <see cref="ImportDefinition"/> for a specific model type.
10
/// Enables automatic mapping and explicit configuration of import fields based on the model's properties.
11
/// </summary>
12
/// <typeparam name="TModel">The type of the model to configure import mapping for.</typeparam>
13
public class ImportDefinitionBuilder<TModel> : ImportDefinitionBuilder
14
{
15
    private readonly TypeAccessor _typeAccessor = TypeAccessor.GetAccessor<TModel>();
2✔
16

17
    /// <summary>
18
    /// Initializes a new instance of the <see cref="ImportDefinitionBuilder{TModel}"/> class
19
    /// using the specified <see cref="ImportDefinition"/>.
20
    /// </summary>
21
    /// <param name="importDefinition">The <see cref="ImportDefinition"/> to configure.</param>
22
    public ImportDefinitionBuilder(ImportDefinition importDefinition) : base(importDefinition)
2✔
23
    {
24
        if (_typeAccessor.TableSchema.HasValue() && _typeAccessor.TableName.HasValue())
2!
NEW
25
            TargetTable($"{_typeAccessor.TableSchema}.{_typeAccessor.TableName}");
×
26
        else if (_typeAccessor.TableName.HasValue())
2✔
27
            TargetTable(_typeAccessor.TableName);
2✔
28

29
        Name(_typeAccessor.Name);
2✔
30
        
31
        ValidatorKey(nameof(ImportValidator));
2✔
32
    }
2✔
33

34
    /// <summary>
35
    /// Automatically maps all eligible properties of the model type to import fields.
36
    /// Properties marked as not mapped or database generated are excluded.
37
    /// </summary>
38
    /// <returns>The current <see cref="ImportDefinitionBuilder{TModel}"/> instance for method chaining.</returns>
39
    public ImportDefinitionBuilder<TModel> AutoMap()
40
    {
41
        var properties = _typeAccessor.GetProperties()
1✔
42
            .Where(p => !p.IsNotMapped && !p.IsDatabaseGenerated)
23✔
43
            .ToList();
1✔
44

45
        foreach (var property in properties)
38✔
46
        {
47
            var fieldMapping = ImportDefinition.Fields.FirstOrDefault(m => m.Name == property.Column);
171✔
48
            if (fieldMapping == null)
18✔
49
            {
50
                fieldMapping = new FieldDefinition { Name = property.Column };
18✔
51
                ImportDefinition.Fields.Add(fieldMapping);
18✔
52
            }
53

54
            // set defaults from property
55
            fieldMapping.CanMap = true;
18✔
56
            fieldMapping.DisplayName = property.DisplayName;
18✔
57
            fieldMapping.DataType = property.MemberType;
18✔
58
            fieldMapping.IsKey = property.IsKey;
18✔
59
            fieldMapping.CanInsert = !property.IsDatabaseGenerated;
18✔
60
            fieldMapping.CanUpdate = !property.IsDatabaseGenerated;
18✔
61
            fieldMapping.IsRequired = property.IsRequired;
18✔
62
        }
63

64
        return this;
1✔
65
    }
66

67
    /// <summary>
68
    /// Configures an import field for the specified model property using a property expression.
69
    /// Allows further customization of the field mapping via the returned <see cref="FieldDefinitionBuilder"/>.
70
    /// </summary>
71
    /// <typeparam name="TValue">The type of the property value.</typeparam>
72
    /// <param name="property">An expression selecting the property to map (e.g., <c>x =&gt; x.Property</c>).</param>
73
    /// <returns>A <see cref="FieldDefinitionBuilder"/> for further configuration of the field.</returns>
74
    public FieldDefinitionBuilder Field<TValue>(Expression<Func<TModel, TValue>> property)
75
    {
76
        var memberAccessor = _typeAccessor.FindProperty(property);
1✔
77

78
        var fieldMapping = ImportDefinition.Fields.FirstOrDefault(m => m.Name == memberAccessor.Column);
1✔
79
        if (fieldMapping == null)
1✔
80
        {
81
            fieldMapping = new FieldDefinition { Name = memberAccessor.Column };
1✔
82
            ImportDefinition.Fields.Add(fieldMapping);
1✔
83
        }
84

85
        var fieldBuilder = new FieldDefinitionBuilder(fieldMapping);
1✔
86

87
        fieldBuilder
1✔
88
            .DisplayName(memberAccessor.DisplayName)
1✔
89
            .DataType(memberAccessor.MemberType)
1✔
90
            .Required(memberAccessor.IsRequired)
1✔
91
            .IsKey(memberAccessor.IsKey)
1✔
92
            .CanInsert(!memberAccessor.IsDatabaseGenerated)
1✔
93
            .CanUpdate(!memberAccessor.IsDatabaseGenerated);
1✔
94

95
        return fieldBuilder;
1✔
96
    }
97

98
    /// <summary>
99
    /// Builds an <see cref="ImportDefinition"/> for the specified model type using the provided builder action.
100
    /// </summary>
101
    /// <param name="builder">An action to configure the <see cref="ImportDefinitionBuilder{TModel}"/>.</param>
102
    /// <returns>A fully configured <see cref="ImportDefinition"/> instance.</returns>
103
    public static ImportDefinition Build(Action<ImportDefinitionBuilder<TModel>> builder)
104
    {
105
        var importDefinition = new ImportDefinition();
2✔
106
        var definitionBuilder = new ImportDefinitionBuilder<TModel>(importDefinition);
2✔
107
        builder(definitionBuilder);
2✔
108

109
        return importDefinition;
2✔
110
    }
111
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc