Skip to content

Commit 1df26fc

Browse files
authored
Merge pull request #762 from mikependon/repodb-immutable-class-messages
#753 #761 - Added a descriptive message for the unmatched ctor argume…
2 parents 1d6229b + d1c088f commit 1df26fc

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

RepoDb.Core/RepoDb/Reflection/Compiler/Compiler.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,18 @@ internal struct FieldDirection
107107
/// <summary>
108108
/// A class that contains both the property <see cref="MemberAssignment"/> object and the constructor argument <see cref="Expression"/> value.
109109
/// </summary>
110-
internal struct MemberBinding
110+
internal class MemberBinding
111111
{
112112
/// <summary>
113113
/// Gets the instance of <see cref="ClassProperty"/> object in used.
114114
/// </summary>
115115
public ClassProperty ClassProperty { get; set; }
116116

117+
/// <summary>
118+
/// Gets the instance of <see cref="ParameterInfo"/> object in used.
119+
/// </summary>
120+
public ParameterInfo ParameterInfo { get; set; }
121+
117122
/// <summary>
118123
/// Gets the current member assignment of the defined property.
119124
/// </summary>
@@ -129,7 +134,7 @@ internal struct MemberBinding
129134
/// </summary>
130135
/// <returns>The presented string.</returns>
131136
public override string ToString() =>
132-
ClassProperty.ToString();
137+
ClassProperty?.ToString() ?? ParameterInfo?.ToString();
133138
}
134139

135140
#endregion
@@ -1275,6 +1280,7 @@ internal static IEnumerable<MemberBinding> GetMemberBindingsForDataEntity<TResul
12751280
memberBindings.Add(new MemberBinding
12761281
{
12771282
ClassProperty = classPropertyParameterInfo.ClassProperty,
1283+
ParameterInfo = classPropertyParameterInfo?.ParameterInfo,
12781284
MemberAssignment = memberAssignment,
12791285
Argument = argument
12801286
});

RepoDb.Core/RepoDb/Reflection/Compiler/DataReaderToType.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,30 @@ internal static Func<DbDataReader, TResult> CompileDataReaderToDataEntity<TResul
9494
// Throw an error if there are no bindings
9595
if (arguments?.Any() != true && memberAssignments?.Any() != true)
9696
{
97-
throw new InvalidOperationException($"There are no 'constructor parameter' and/or 'property member' bindings found between the ResultSet of the data reader and the type '{typeOfResult.FullName}'.");
97+
throw new InvalidOperationException($"There are no 'constructor parameter' and/or 'property member' bindings found between the resultset of the data reader and the type '{typeOfResult.FullName}'. " +
98+
$"Make sure the 'constructor arguments' and/or 'model properties' are matching the list of the fields returned by the data reader object.");
9899
}
99100

100101
// Initialize the members
101102
var constructorInfo = typeOfResult.GetConstructorWithMostArguments();
102103
var entityExpression = (Expression)null;
103104

105+
// Validate arguments equality
106+
if (arguments?.Any() == true)
107+
{
108+
var parameters = constructorInfo.GetParameters();
109+
var unmatches = parameters
110+
.Where(e => memberBindings.FirstOrDefault(binding => binding.Argument != null &&
111+
string.Equals(binding.ParameterInfo?.Name, e.Name, StringComparison.OrdinalIgnoreCase)) == null);
112+
113+
// Throw the detailed message
114+
if (unmatches?.Any() == true)
115+
{
116+
var unmatchesNames = unmatches.Select(e => e.Name).Join(",");
117+
throw new MissingMemberException($"The following ctor arguments ('{unmatchesNames}') for type '{typeOfResult.FullName}' are not matching from any of the resultset fields returned by the data reader object.");
118+
}
119+
}
120+
104121
try
105122
{
106123
// Constructor arguments

0 commit comments

Comments
 (0)