Skip to content

Commit

Permalink
#886 Integrated the PropertyValueSetter attributes into the compiler.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikependon committed Sep 14, 2021
1 parent fd730da commit 52fff15
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 23 deletions.
23 changes: 23 additions & 0 deletions RepoDb.Core/RepoDb/ClassProperty.cs
@@ -1,4 +1,5 @@
using RepoDb.Attributes;
using RepoDb.Attributes.Parameter;
using RepoDb.Extensions;
using System;
using System.ComponentModel.DataAnnotations;
Expand Down Expand Up @@ -140,6 +141,7 @@ public IdentityAttribute GetIdentityAttribute()
/// Gets the <see cref="TypeMapAttribute"/> if present.
/// </summary>
/// <returns>The instance of <see cref="TypeMapAttribute"/>.</returns>
[Obsolete("Use the GetDbTypeAttribute() method instead.")]
public TypeMapAttribute GetTypeMapAttribute()
{
if (isTypeMapAttributeWasSet)
Expand All @@ -150,6 +152,27 @@ public TypeMapAttribute GetTypeMapAttribute()
return typeMapAttribute = PropertyInfo.GetCustomAttribute(StaticType.TypeMapAttribute) as TypeMapAttribute;
}

/*
* GetDbTypeAttribute
*/
private bool isDbTypeAttributeWasSet;
private DbTypeAttribute dbTypeAttribute;

/// <summary>
/// Gets the <see cref="DbTypeAttribute"/> if present.
/// </summary>
/// <returns>The instance of <see cref="DbTypeAttribute"/>.</returns>
public DbTypeAttribute GetDbTypeAttribute()
{
if (isDbTypeAttributeWasSet)
{
return dbTypeAttribute;
}
isDbTypeAttributeWasSet = true;
return dbTypeAttribute = (PropertyInfo.GetCustomAttribute(StaticType.DbTypeAttribute) ??
PropertyInfo.GetCustomAttribute(StaticType.TypeMapAttribute)) as DbTypeAttribute;
}

/*
* GetPropertyHandlerAttribute
*/
Expand Down
2 changes: 0 additions & 2 deletions RepoDb.Core/RepoDb/Extensions/DataEntityExtension.cs
Expand Up @@ -25,8 +25,6 @@ internal static IEnumerable<ClassProperty> GetProperties(Type type)
}
}

// TODO: Remove the filter in the 'TEntity'

/// <summary>
/// Gets the list of <see cref="PropertyInfo"/> objects from the data entity type as <see cref="ClassProperty"/> objects.
/// </summary>
Expand Down
16 changes: 8 additions & 8 deletions RepoDb.Core/RepoDb/Extensions/DbCommandExtension.cs
@@ -1,4 +1,4 @@
using RepoDb.Attributes;
using RepoDb.Attributes.Parameter;
using RepoDb.Enumerations;
using RepoDb.Exceptions;
using RepoDb.Interfaces;
Expand Down Expand Up @@ -319,7 +319,7 @@ private static void EnsureTableValueParameter(IDbDataParameter parameter)
parameter.Size = (parameterSize > 0) ? parameterSize : parameter.Size;

// Parameter values
InvokeParameterPropertyValueSetterAttributes(parameter, classProperty);
InvokePropertyValueAttributes(parameter, classProperty);

// Return the parameter
return parameter;
Expand Down Expand Up @@ -363,7 +363,7 @@ private static void EnsureTableValueParameter(IDbDataParameter parameter)
parameter.Size = GetSize(size, dbField);

// Type map attributes
InvokeParameterPropertyValueSetterAttributes(parameter, classProperty);
InvokePropertyValueAttributes(parameter, classProperty);

// Return the parameter
return parameter;
Expand Down Expand Up @@ -700,10 +700,10 @@ private static void EnsureTableValueParameter(IDbDataParameter parameter)
/// </summary>
/// <param name="parameter"></param>
/// <param name="classProperty"></param>
private static void InvokeParameterPropertyValueSetterAttributes(IDbDataParameter parameter,
private static void InvokePropertyValueAttributes(IDbDataParameter parameter,
ClassProperty classProperty)
{
var attributes = GetParameterPropertyValueSetterAttributes(classProperty);
var attributes = GetPropertyValueAttributes(classProperty);
if (attributes?.Any() != true)
{
return;
Expand All @@ -720,14 +720,14 @@ private static void EnsureTableValueParameter(IDbDataParameter parameter)
/// </summary>
/// <param name="classProperty"></param>
/// <returns></returns>
private static IEnumerable<ParameterPropertyValueSetterAttribute> GetParameterPropertyValueSetterAttributes(ClassProperty classProperty) =>
private static IEnumerable<PropertyValueAttribute> GetPropertyValueAttributes(ClassProperty classProperty) =>
classProperty?
.PropertyInfo?
.GetCustomAttributes()?
.Where(e =>
StaticType.ParameterPropertyValueSetterAttribute.IsAssignableFrom(e.GetType()))
StaticType.PropertyValueAttribute.IsAssignableFrom(e.GetType()))
.Select(e =>
(ParameterPropertyValueSetterAttribute)e);
(PropertyValueAttribute)e);

/// <summary>
///
Expand Down
4 changes: 3 additions & 1 deletion RepoDb.Core/RepoDb/Extensions/PropertyInfoExtension.cs
Expand Up @@ -6,6 +6,7 @@
using System;
using System.ComponentModel.DataAnnotations.Schema;
using RepoDb.Interfaces;
using RepoDb.Attributes.Parameter;

namespace RepoDb.Extensions
{
Expand Down Expand Up @@ -55,7 +56,8 @@ public static T GetCustomAttribute<T>(this PropertyInfo property)
Type declaringType)
{
var attributeName = ((MapAttribute)GetCustomAttribute(property, StaticType.MapAttribute))?.Name ??
((ColumnAttribute)GetCustomAttribute(property, StaticType.ColumnAttribute))?.Name;
((ColumnAttribute)GetCustomAttribute(property, StaticType.ColumnAttribute))?.Name ??
((NameAttribute)GetCustomAttribute(property, StaticType.NameAttribute))?.Name;
return attributeName ??
PropertyMapper.Get(declaringType, property) ??
property.Name;
Expand Down
2 changes: 1 addition & 1 deletion RepoDb.Core/RepoDb/Mappers/PropertyHandlerMapper.cs
Expand Up @@ -406,7 +406,7 @@ public static void Remove(Type type)
// Extract
if (propertyInfo != null)
{
propertyInfo = PropertyCache.Get(entityType, propertyInfo.AsField()).PropertyInfo ?? propertyInfo;
propertyInfo = PropertyCache.Get(entityType, propertyInfo?.AsField())?.PropertyInfo ?? propertyInfo;
}

// Variables
Expand Down
20 changes: 10 additions & 10 deletions RepoDb.Core/RepoDb/Reflection/Compiler/Compiler.cs
Expand Up @@ -1598,8 +1598,8 @@ internal static Expression ConvertExpressionToClassHandlerSetExpression<TResult>
/// <returns></returns>
internal static MethodCallExpression GetDbParameterDbTypeAssignmentExpression(ParameterExpression parameterVariableExpression,
ClassProperty classProperty,
DbField dbField)
=> GetDbParameterDbTypeAssignmentExpression(parameterVariableExpression, GetDbType(classProperty, dbField.Type));
DbField dbField) =>
GetDbParameterDbTypeAssignmentExpression(parameterVariableExpression, GetDbType(classProperty, dbField.Type));

/// <summary>
///
Expand All @@ -1608,8 +1608,8 @@ internal static Expression ConvertExpressionToClassHandlerSetExpression<TResult>
/// <param name="dbField"></param>
/// <returns></returns>
internal static MethodCallExpression GetDbParameterDbTypeAssignmentExpression(ParameterExpression parameterVariableExpression,
DbField dbField)
=> GetDbParameterDbTypeAssignmentExpression(parameterVariableExpression, GetDbType(null, dbField.Type));
DbField dbField) =>
GetDbParameterDbTypeAssignmentExpression(parameterVariableExpression, GetDbType(null, dbField.Type));

/// <summary>
///
Expand All @@ -1621,11 +1621,11 @@ internal static Expression ConvertExpressionToClassHandlerSetExpression<TResult>
DbType? dbType)
{
var expression = (MethodCallExpression)null;
var dbParameterDbTypeSetMethod = StaticType.DbParameter.GetProperty("DbType").SetMethod;

// Set the DB Type
if (dbType != null)
{
var dbParameterDbTypeSetMethod = StaticType.DbParameter.GetProperty("DbType").SetMethod;
expression = Expression.Call(parameterVariableExpression, dbParameterDbTypeSetMethod, Expression.Constant(dbType));
}

Expand Down Expand Up @@ -1782,6 +1782,11 @@ internal static Expression GetDbParameterCollectionClearMethodExpression(MemberE
var createParameterExpression = GetDbCommandCreateParameterExpression(commandParameterExpression);
parameterAssignmentExpressions.AddIfNotNull(Expression.Assign(parameterVariableExpression, createParameterExpression));

// PropertyValueAttributes / DbField must precide
var propertyValueAttributeAssignmentExpressions = GetPropertyValueAttributeAssignmentExpressions(parameterVariableExpression,
classProperty);
parameterAssignmentExpressions.AddRangeIfNotNullOrNotEmpty(propertyValueAttributeAssignmentExpressions);

// DbParameter.Name
var nameAssignmentExpression = GetDbParameterNameAssignmentExpression(parameterVariableExpression,
dbField,
Expand All @@ -1806,11 +1811,6 @@ internal static Expression GetDbParameterCollectionClearMethodExpression(MemberE
classProperty, dbField);
parameterAssignmentExpressions.AddIfNotNull(dbTypeAssignmentExpression);

// ParameterPropertyValueSetterAttribute
var parameterPropertyValueSetterAttributesExpressions = GetParameterPropertyValueSetterAttributesAssignmentExpressions(parameterVariableExpression,
classProperty);
parameterAssignmentExpressions.AddRangeIfNotNullOrNotEmpty(parameterPropertyValueSetterAttributesExpressions);

// DbParameter.Direction
if (dbSetting.IsDirectionSupported)
{
Expand Down
4 changes: 3 additions & 1 deletion RepoDb.Core/RepoDb/Resolvers/TypeMapPropertyLevelResolver.cs
@@ -1,4 +1,5 @@
using RepoDb.Attributes;
using RepoDb.Attributes.Parameter;
using RepoDb.Extensions;
using RepoDb.Interfaces;
using System.Data;
Expand All @@ -21,7 +22,8 @@ public class TypeMapPropertyLevelResolver : IResolver<PropertyInfo, DbType?>
var dbType = (DbType?)null;

// Attribute Level
var attribute = propertyInfo.GetCustomAttribute<TypeMapAttribute>();
var attribute = propertyInfo.GetCustomAttribute<TypeMapAttribute>() ??
propertyInfo.GetCustomAttribute<DbTypeAttribute>();
if (attribute != null)
{
dbType = attribute.DbType;
Expand Down
10 changes: 10 additions & 0 deletions RepoDb.Core/RepoDb/StaticType.cs
Expand Up @@ -114,6 +114,11 @@ internal static class StaticType
/// </summary>
public static Type DbType => typeof(DbType);

/// <summary>
/// Gets a type of the <see cref="Attributes.Parameter.DbTypeAttribute"/> .NET CLR type.
/// </summary>
public static Type DbTypeAttribute => typeof(DbTypeAttribute);

/// <summary>
/// Gets a type of the <see cref="decimal"/> .NET CLR type.
/// </summary>
Expand Down Expand Up @@ -219,6 +224,11 @@ internal static class StaticType
/// </summary>
public static Type MapAttribute => typeof(MapAttribute);

/// <summary>
/// Gets a type of the <see cref="Attributes.Parameter.ColumnAttribute"/> .NET CLR type.
/// </summary>
public static Type NameAttribute => typeof(NameAttribute);

/// <summary>
/// Gets a type of the <see cref="System.Nullable"/> .NET CLR type.
/// </summary>
Expand Down

0 comments on commit 52fff15

Please sign in to comment.