Skip to content

Commit 033039c

Browse files
committed
QQQ
1 parent 71e45f8 commit 033039c

11 files changed

Lines changed: 28 additions & 73 deletions

src/RepoDb/DataEntityDataReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ private IEnumerable<ClassProperty> GetClassProperties()
691691
/// </summary>
692692
/// <param name="dictionary"></param>
693693
/// <returns></returns>
694-
private static IEnumerable<Field> GetFields(IDictionary<string, object> dictionary)
694+
private static IEnumerable<Field> GetFields(IDictionary<string, object>? dictionary)
695695
{
696696
if (dictionary != null)
697697
{

src/RepoDb/Mappers/PropertyHandlerMapper.cs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public static void Add<TEntity, TPropertyHandler>(string propertyName,
247247
bool force)
248248
where TEntity : class
249249
where TPropertyHandler : new() =>
250-
Add<TEntity, TPropertyHandler>(propertyName, new TPropertyHandler(), false);
250+
Add<TEntity, TPropertyHandler>(propertyName, new TPropertyHandler(), force);
251251

252252
/// <summary>
253253
/// Property Level: Adds a property handler mapping into a data entity type property (via property name).
@@ -266,11 +266,7 @@ public static void Add<TEntity, TPropertyHandler>(string propertyName,
266266
ObjectExtension.ThrowIfNull(propertyName, nameof(propertyName));
267267

268268
// Get the property
269-
var property = TypeExtension.GetProperty<TEntity>(propertyName);
270-
if (property == null)
271-
{
272-
throw new PropertyNotFoundException(nameof(propertyName), $"Property '{propertyName}' is not found at type '{typeof(TEntity).FullName}'.");
273-
}
269+
var property = TypeExtension.GetProperty<TEntity>(propertyName) ?? throw new PropertyNotFoundException(nameof(propertyName), $"Property '{propertyName}' is not found at type '{typeof(TEntity).FullName}'.");
274270

275271
// Add to the mapping
276272
Add<TEntity, TPropertyHandler>(property, propertyHandler, force);
@@ -312,7 +308,7 @@ public static void Add<TEntity, TPropertyHandler>(Field field,
312308
bool force)
313309
where TEntity : class
314310
where TPropertyHandler : new() =>
315-
Add<TEntity, TPropertyHandler>(field, new TPropertyHandler(), false);
311+
Add<TEntity, TPropertyHandler>(field, new TPropertyHandler(), force);
316312

317313
/// <summary>
318314
/// Property Level: Adds a property handler mapping into a data entity type property (via <see cref="Field"/> object).
@@ -331,11 +327,7 @@ public static void Add<TEntity, TPropertyHandler>(Field field,
331327
ObjectExtension.ThrowIfNull(field, nameof(field));
332328

333329
// Get the property
334-
var property = TypeExtension.GetProperty<TEntity>(field.FieldName);
335-
if (property == null)
336-
{
337-
throw new PropertyNotFoundException(nameof(field), $"Property '{field.FieldName}' is not found at type '{typeof(TEntity).FullName}'.");
338-
}
330+
var property = TypeExtension.GetProperty<TEntity>(field.FieldName) ?? throw new PropertyNotFoundException(nameof(field), $"Property '{field.FieldName}' is not found at type '{typeof(TEntity).FullName}'.");
339331

340332
// Add to the mapping
341333
Add<TEntity, TPropertyHandler>(property, propertyHandler, force);
@@ -533,11 +525,7 @@ public static void Remove<TEntity>(string propertyName)
533525
ObjectExtension.ThrowIfNull(propertyName, nameof(propertyName));
534526

535527
// Get the property
536-
var property = TypeExtension.GetProperty<TEntity>(propertyName);
537-
if (property == null)
538-
{
539-
throw new PropertyNotFoundException(nameof(propertyName), $"Property '{propertyName}' is not found at type '{typeof(TEntity).FullName}'.");
540-
}
528+
var property = TypeExtension.GetProperty<TEntity>(propertyName) ?? throw new PropertyNotFoundException(nameof(propertyName), $"Property '{propertyName}' is not found at type '{typeof(TEntity).FullName}'.");
541529

542530
// Add to the mapping
543531
Remove<TEntity>(property);
@@ -555,11 +543,7 @@ public static void Remove<TEntity>(Field field)
555543
ObjectExtension.ThrowIfNull(field, nameof(field));
556544

557545
// Get the property
558-
var property = TypeExtension.GetProperty<TEntity>(field.FieldName);
559-
if (property == null)
560-
{
561-
throw new PropertyNotFoundException(nameof(field), $"Property '{field.FieldName}' is not found at type '{typeof(TEntity).FullName}'.");
562-
}
546+
var property = TypeExtension.GetProperty<TEntity>(field.FieldName) ?? throw new PropertyNotFoundException(nameof(field), $"Property '{field.FieldName}' is not found at type '{typeof(TEntity).FullName}'.");
563547

564548
// Add to the mapping
565549
Remove<TEntity>(property);

src/RepoDb/QueryField/ParseExpression.cs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,9 @@ public partial class QueryField
2020
var properties = PropertyCache.Get<TEntity>();
2121

2222
// Failing at some point - for base interfaces
23-
var property = properties
24-
.FirstOrDefault(p =>
25-
string.Equals(p.FieldName, field.FieldName, StringComparison.OrdinalIgnoreCase));
26-
27-
// Matches to the actual class properties
28-
if (property == null)
29-
{
30-
property = properties
31-
.FirstOrDefault(p =>
32-
string.Equals(p.PropertyInfo.Name, field.FieldName, StringComparison.OrdinalIgnoreCase));
33-
}
34-
35-
// Return the value
36-
return property;
23+
return
24+
properties.GetByFieldName(field.FieldName)
25+
?? properties.GetByPropertyName(field.FieldName);
3726
}
3827

3928
/// <summary>

src/RepoDb/QueryGroup/ParseExpression.cs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,7 @@ public static QueryGroup Parse<TEntity>(Expression<Func<TEntity, bool>> expressi
5656
#endif
5757

5858
// Parse the expression base on type
59-
var parsed = Parse<TEntity>(expression.Body);
60-
61-
/*
62-
* In order to NOT trigger the 'Equality' comparision (via overriden GetHashCode()), do not use the '=='
63-
* when comparing to NULLs, instead, use the ReferenceEquals method.
64-
*/
65-
66-
// Throw an unsupported exception if not parsed
67-
if (parsed is null)
68-
{
69-
throw new NotSupportedException($"Expression '{expression}' is currently not supported.");
70-
}
59+
var parsed = Parse<TEntity>(expression.Body) ?? throw new NotSupportedException($"Expression '{expression}' is currently not supported.");
7160

7261
// Return the parsed values
7362
return parsed.Fix(connection, transaction, tableName ?? ClassMappedNameCache.Get<TEntity>());
@@ -124,10 +113,7 @@ private static QueryGroup Parse<TEntity>(BinaryExpression expression)
124113
}
125114

126115
// Variables
127-
var leftQueryGroup = Parse<TEntity>(expression.Left);
128-
129-
if (leftQueryGroup is null)
130-
throw new NotSupportedException($"Expression {expression.Left} is currently not supported");
116+
var leftQueryGroup = Parse<TEntity>(expression.Left) ?? throw new NotSupportedException($"Expression {expression.Left} is currently not supported");
131117

132118
// IsNot
133119
if (expression.NodeType is ExpressionType.Equal or ExpressionType.NotEqual
@@ -141,10 +127,7 @@ private static QueryGroup Parse<TEntity>(BinaryExpression expression)
141127
}
142128
else
143129
{
144-
var rightQueryGroup = Parse<TEntity>(expression.Right);
145-
if (rightQueryGroup is null)
146-
throw new NotSupportedException($"Expression {expression.Right} is currently not supported");
147-
130+
var rightQueryGroup = Parse<TEntity>(expression.Right) ?? throw new NotSupportedException($"Expression {expression.Right} is currently not supported");
148131
return new QueryGroup([leftQueryGroup, rightQueryGroup], GetConjunction(expression));
149132
}
150133

src/RepoDb/Reflection/Compiler.DataEntityPropertySetter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ partial class Compiler
1616
Field field)
1717
{
1818
// Get the entity property
19-
var property = PropertyCache.Get(entityType)?.GetByFieldName(field.FieldName)?.PropertyInfo;
19+
var property = PropertyCache.Get(entityType).GetByFieldName(field.FieldName)?.PropertyInfo;
2020

2121
// Return the function
2222
return CompileDataEntityPropertySetter(entityType,
@@ -32,7 +32,7 @@ partial class Compiler
3232
/// <param name="targetType"></param>
3333
/// <returns></returns>
3434
private static Action<object, object?> CompileDataEntityPropertySetter(Type entityType,
35-
PropertyInfo property,
35+
PropertyInfo? property,
3636
Type targetType)
3737
{
3838
// Check the property first
@@ -64,7 +64,7 @@ partial class Compiler
6464
{
6565
var classProperty = PropertyCache.Get(entityType, property, true);
6666
valueExpression = ConvertExpressionToPropertyHandlerSetExpression(valueExpression,
67-
null, classProperty, targetType ?? classProperty.PropertyInfo.PropertyType);
67+
null, classProperty, targetType);
6868
}
6969

7070
// Assign the value into DataEntity.Property

src/RepoDb/Reflection/Compiler.DataReaderToType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private static Func<DbDataReader, TResult> CompileDataReaderToDataEntity<TResult
8282
var readerFields = GetDataReaderFields(reader, dbFields);
8383
var memberBindings = GetMemberBindingsForDataEntity<TResult>(readerParameterExpression, readerFields, reader.GetType());
8484
var memberAssignments = memberBindings.Where(item => item.MemberAssignment != null).Select(item => item.MemberAssignment!);
85-
var arguments = memberBindings?.Where(item => item.Argument != null).Select(item => item.Argument!);
85+
var arguments = memberBindings.Where(item => item.Argument != null).Select(item => item.Argument!);
8686
var typeOfResult = typeof(TResult);
8787

8888
// Throw an error if there are no bindings

src/RepoDb/Reflection/Compiler.DbCommandToProperty.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Data.Common;
22
using System.Globalization;
33
using System.Linq.Expressions;
4+
using RepoDb.Exceptions;
45
using RepoDb.Extensions;
56
using RepoDb.Interfaces;
67

@@ -39,7 +40,7 @@ public static Action<TEntity, DbCommand> CompileDbCommandToProperty<TEntity>(Fie
3940

4041
// Get the entity property
4142
var propertyName = field.FieldName.AsUnquoted(true, dbSetting).AsAlphaNumeric();
42-
var property = (typeOfEntity.GetProperty(propertyName) ?? PropertyCache.Get(typeOfEntity)?.GetByFieldName(propertyName)?.PropertyInfo)?.SetMethod;
43+
var property = (typeOfEntity.GetProperty(propertyName) ?? PropertyCache.Get(typeOfEntity).GetByFieldName(propertyName)?.PropertyInfo)?.SetMethod ?? throw new PropertyNotFoundException(propertyName, $"Property {propertyName} not found");
4344

4445
// Get the command parameter
4546
var name = parameterName ?? propertyName;
@@ -50,7 +51,7 @@ public static Action<TEntity, DbCommand> CompileDbCommandToProperty<TEntity>(Fie
5051
// Assign the Parameter.Value into DataEntity.Property
5152
var value = Expression.Property(parameter, dbParameterValueProperty);
5253
var propertyAssignment = Expression.Call(entityParameterExpression, property,
53-
Expression.Convert(value, TypeCache.Get(field.Type)?.GetUnderlyingType()));
54+
Expression.Convert(value, TypeCache.Get(field.Type).GetUnderlyingType()));
5455

5556
// Return function
5657
return Expression.Lambda<Action<TEntity, DbCommand>>(

src/RepoDb/Reflection/Compiler.DictionaryStringObjectDbParameterSetter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ partial class Compiler
2222
var dbCommandExpression = Expression.Parameter(StaticType.DbCommand, "command");
2323
var entityParameterExpression = Expression.Parameter(StaticType.Object, "entityParameter");
2424
var dbParameterCollectionExpression = Expression.Property(dbCommandExpression,
25-
StaticType.DbCommand.GetProperty(nameof(DbCommand.Parameters)));
25+
StaticType.DbCommand.GetProperty(nameof(DbCommand.Parameters))!);
2626
var dictionaryInstanceExpression = ConvertExpressionToTypeExpression(entityParameterExpression, StaticType.IDictionaryStringObject);
2727
var bodyExpressions = new List<Expression>
2828
{

src/RepoDb/Reflection/Compiler.PlainTypeToDbParameters.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,9 @@ public static Action<DbCommand, object> GetPlainTypeToDbParametersCompiledFuncti
100100
}
101101
else
102102
{
103-
var targetType = TypeCache.Get(dbField?.Type).GetUnderlyingType() ?? valueType;
104103
dbType = targetProperty.GetDbType() ??
105-
targetType?.GetDbType() ??
106-
new ClientTypeToDbTypeResolver().Resolve(targetType);
104+
valueType.GetDbType() ??
105+
new ClientTypeToDbTypeResolver().Resolve(valueType);
107106
}
108107
var setDbTypeExpression = GetDbParameterDbTypeAssignmentExpression(dbParameterExpression, dbType);
109108
parameterCallExpressions.AddIfNotNull(setDbTypeExpression);

src/RepoDb/Reflection/Compiler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,7 @@ private static Expression ConvertExpressionToClassHandlerGetExpression<TResult>(
11821182
private static Expression ConvertExpressionToPropertyHandlerSetExpression(Expression expression,
11831183
Expression? parameterExpression,
11841184
ClassProperty? classProperty,
1185-
Type targetType) =>
1185+
Type? targetType) =>
11861186
ConvertExpressionToPropertyHandlerSetExpressionTuple(expression, parameterExpression, classProperty, targetType).convertedExpression;
11871187

11881188
/// <summary>
@@ -1196,7 +1196,7 @@ private static Expression ConvertExpressionToPropertyHandlerSetExpression(Expres
11961196
private static (Expression convertedExpression, Type? handlerSetReturnType) ConvertExpressionToPropertyHandlerSetExpressionTuple(Expression expression,
11971197
Expression? parameterExpression,
11981198
ClassProperty? classProperty,
1199-
Type targetType)
1199+
Type? targetType)
12001200
{
12011201
var handlerInstance = classProperty?.GetPropertyHandler() ??
12021202
PropertyHandlerCache.Get<object>(targetType);
@@ -1213,7 +1213,7 @@ private static (Expression convertedExpression, Type? handlerSetReturnType) Conv
12131213

12141214
// Nullable
12151215
expression = ConvertExpressionToNullableExpression(expression,
1216-
TypeCache.Get(setParameter.ParameterType).GetUnderlyingType() ?? targetType);
1216+
TypeCache.Get(setParameter.ParameterType).GetUnderlyingType());
12171217

12181218
// Call
12191219
var valueExpression = ConvertExpressionToTypeExpression(expression, setParameter.ParameterType);

0 commit comments

Comments
 (0)