Skip to content

Commit

Permalink
#886 Added the PropertyHandlerAttributes() method on the FluentMapper…
Browse files Browse the repository at this point in the history
… class.
  • Loading branch information
mikependon committed Sep 17, 2021
1 parent 923bfa9 commit 1ca1638
Show file tree
Hide file tree
Showing 17 changed files with 176 additions and 57 deletions.
Expand Up @@ -48,7 +48,7 @@ public void TestNameAttributeViaEntityViaCreateParameters()
Assert.AreEqual(1, command.Parameters.Count);

// Assert
var parameter = command.Parameters["TableColumnName"];
var parameter = command.Parameters["@TableColumnName"];
Assert.IsNotNull(parameter);
}
}
Expand Down
@@ -1,5 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RepoDb.Attributes;
using RepoDb.Attributes.Parameter;
using RepoDb.Exceptions;
using System.Data;

Expand Down
4 changes: 2 additions & 2 deletions RepoDb.Core/RepoDb/Attributes/Parameter/DbTypeAttribute.cs
Expand Up @@ -4,8 +4,8 @@
namespace RepoDb.Attributes.Parameter
{
/// <summary>
/// An attribute used to define a value to the <see cref="DbParameter.DbType"/>
/// property via an entity property before the actual execution.
/// An attribute that is being used to define a value to the <see cref="DbParameter.DbType"/>
/// property via a class property mapping..
/// </summary>
public class DbTypeAttribute : PropertyValueAttribute
{
Expand Down
4 changes: 2 additions & 2 deletions RepoDb.Core/RepoDb/Attributes/Parameter/DirectionAttribute.cs
Expand Up @@ -4,8 +4,8 @@
namespace RepoDb.Attributes.Parameter
{
/// <summary>
/// An attribute used to define a value to the <see cref="DbParameter.Direction"/>
/// property via an entity property before the actual execution.
/// An attribute that is being used to define a value to the <see cref="DbParameter.Direction"/>
/// property via a class property mapping..
/// </summary>
public class DirectionAttribute : PropertyValueAttribute
{
Expand Down
Expand Up @@ -4,8 +4,8 @@
namespace RepoDb.Attributes.Parameter
{
/// <summary>
/// An attribute used to define a value to the <see cref="DbParameter.IsNullable"/>
/// property via an entity property before the actual execution.
/// An attribute that is being used to define a value to the <see cref="DbParameter.IsNullable"/>
/// property via a class property mapping..
/// </summary>
public class IsNullableAttribute : PropertyValueAttribute
{
Expand Down
13 changes: 10 additions & 3 deletions RepoDb.Core/RepoDb/Attributes/Parameter/NameAttribute.cs
@@ -1,10 +1,11 @@
using System.Data.Common;
using RepoDb.Extensions;
using System.Data.Common;

namespace RepoDb.Attributes.Parameter
{
/// <summary>
/// An attribute used to define a value to the <see cref="DbParameter.ParameterName"/>
/// property via an entity property before the actual execution.
/// An attribute that is being used to define a value to the <see cref="DbParameter.ParameterName"/>
/// property via a class property mapping..
/// </summary>
public class NameAttribute : PropertyValueAttribute
{
Expand All @@ -20,5 +21,11 @@ public NameAttribute(string name)
/// Gets the mapped name of the equivalent database object/field.
/// </summary>
public string Name => (string)Value;

/// <summary>
///
/// </summary>
/// <returns></returns>
internal override object GetValue() => Name.AsParameter();
}
}
4 changes: 2 additions & 2 deletions RepoDb.Core/RepoDb/Attributes/Parameter/PrecisionAttribute.cs
Expand Up @@ -3,8 +3,8 @@
namespace RepoDb.Attributes.Parameter
{
/// <summary>
/// An attribute used to define a value to the <see cref="DbParameter.Precision"/>
/// property via an entity property before the actual execution.
/// An attribute that is being used to define a value to the <see cref="DbParameter.Precision"/>
/// property via a class property mapping..
/// </summary>
public class PrecisionAttribute : PropertyValueAttribute
{
Expand Down
Expand Up @@ -98,10 +98,17 @@ internal void SetValue(IDbDataParameter parameter)

if (ParameterType.IsAssignableFrom(parameter.GetType()))
{
PropertyInfo.SetValue(parameter, Value);
// The reason why we use the 'GetValue()' method over the 'Value' property is because
// of the fact that derived class should sometime customize the value.
PropertyInfo.SetValue(parameter, GetValue());
}
}

/// <summary>
///
/// </summary>
internal virtual object GetValue() => Value;

/*
* Helpers
*/
Expand Down
4 changes: 2 additions & 2 deletions RepoDb.Core/RepoDb/Attributes/Parameter/ScaleAttribute.cs
Expand Up @@ -3,8 +3,8 @@
namespace RepoDb.Attributes.Parameter
{
/// <summary>
/// An attribute used to define a value to the <see cref="DbParameter.Scale"/>
/// property via an entity property before the actual execution.
/// An attribute that is being used to define a value to the <see cref="DbParameter.Scale"/>
/// property via a class property mapping..
/// </summary>
public class ScaleAttribute : PropertyValueAttribute
{
Expand Down
4 changes: 2 additions & 2 deletions RepoDb.Core/RepoDb/Attributes/Parameter/SizeAttribute.cs
Expand Up @@ -3,8 +3,8 @@
namespace RepoDb.Attributes.Parameter
{
/// <summary>
/// An attribute used to define a value to the <see cref="DbParameter.Size"/>
/// property via an entity property before the actual execution.
/// An attribute that is being used to define a value to the <see cref="DbParameter.Size"/>
/// property via a class property mapping..
/// </summary>
public class SizeAttribute : PropertyValueAttribute
{
Expand Down
1 change: 1 addition & 0 deletions RepoDb.Core/RepoDb/Cachers/PropertyValueAttributeCache.cs
Expand Up @@ -141,6 +141,7 @@ internal static IEnumerable<PropertyValueAttribute> Get<TEntity>(PropertyInfo pr
PropertyInfo propertyInfo)
{
// Validate
ObjectExtension.ThrowIfNull(entityType, "EntityType");
ObjectExtension.ThrowIfNull(propertyInfo, "PropertyInfo");

// Variables
Expand Down
21 changes: 21 additions & 0 deletions RepoDb.Core/RepoDb/ClassProperty.cs
Expand Up @@ -2,6 +2,7 @@
using RepoDb.Attributes.Parameter;
using RepoDb.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Reflection;
Expand Down Expand Up @@ -293,6 +294,26 @@ public TPropertyHandler GetPropertyHandler<TPropertyHandler>()
public string GetMappedName() =>
mappedName ??= PropertyMappedNameCache.Get(GetDeclaringType(), PropertyInfo);

/*
* PropertyHandlerAttributes
*/
private bool isPropertyValueAttributesWasSet;
private IEnumerable<PropertyValueAttribute> propertyValueAttributes;

/// <summary>
/// Gets the list of mapped <see cref="PropertyValueAttribute"/> object for the current property.
/// </summary>
/// <returns>The list of mapped <see cref="PropertyValueAttribute"/> object.</returns>
public IEnumerable<PropertyValueAttribute> GetPropertyValueAttributes()
{
if (isPropertyValueAttributesWasSet == true)
{
return propertyValueAttributes;
}
isPropertyValueAttributesWasSet = true;
return propertyValueAttributes = PropertyInfo.GetPropertyValueAttributes();
}

#endregion

#region Comparers
Expand Down
4 changes: 3 additions & 1 deletion RepoDb.Core/RepoDb/Extensions/ClassPropertyExtension.cs
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using RepoDb.Attributes.Parameter;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace RepoDb.Extensions
{
Expand Down
20 changes: 3 additions & 17 deletions RepoDb.Core/RepoDb/Extensions/DbCommandExtension.cs
Expand Up @@ -703,15 +703,15 @@ private static void EnsureTableValueParameter(IDbDataParameter parameter)
private static void InvokePropertyValueAttributes(IDbDataParameter parameter,
ClassProperty classProperty)
{
var attributes = GetPropertyValueAttributes(classProperty);
var attributes = classProperty?.GetPropertyValueAttributes();
if (attributes?.Any() != true)
{
return;
}

// In RepoDb, the only way the parameter has '_' is when the time you call the QueryField.IsForUpdate()
// In RepoDb, the only way the parameter has '@_' is when the time you call the QueryField.IsForUpdate()
// method and it is only happening on update operations.
var isForUpdate = parameter.ParameterName.StartsWith("_");
var isForUpdate = parameter.ParameterName.StartsWith("_") || parameter.ParameterName.StartsWith("@_");

foreach (var attribute in attributes)
{
Expand All @@ -723,20 +723,6 @@ private static void EnsureTableValueParameter(IDbDataParameter parameter)
}
}

/// <summary>
///
/// </summary>
/// <param name="classProperty"></param>
/// <returns></returns>
private static IEnumerable<PropertyValueAttribute> GetPropertyValueAttributes(ClassProperty classProperty) =>
classProperty?
.PropertyInfo?
.GetCustomAttributes()?
.Where(e =>
StaticType.PropertyValueAttribute.IsAssignableFrom(e.GetType()))
.Select(e =>
(PropertyValueAttribute)e);

/// <summary>
///
/// </summary>
Expand Down
17 changes: 16 additions & 1 deletion RepoDb.Core/RepoDb/Extensions/PropertyInfoExtension.cs
Expand Up @@ -83,7 +83,7 @@ public static T GetCustomAttribute<T>(this PropertyInfo property)
internal static QueryField AsQueryField(this PropertyInfo property,
object entity,
bool appendUnderscore) =>
new (property.AsField(), Operation.Equal, property.GetHandledValue(entity), appendUnderscore);
new(property.AsField(), Operation.Equal, property.GetHandledValue(entity), appendUnderscore);

/// <summary>
/// Converts a <see cref="PropertyInfo"/> into a mapped name.
Expand Down Expand Up @@ -176,6 +176,21 @@ public static IEnumerable<Field> AsFields(this IEnumerable<PropertyInfo> propert
public static IEnumerable<Field> AsFields(this PropertyInfo[] properties) =>
AsFields(properties.AsEnumerable<PropertyInfo>());

/// <summary>
/// Returns the list of <see cref="PropertyValueAttribute"/> object that is currently mapped
/// on the target <see cref="PropertyInfo"/> object.
/// </summary>
/// <param name="propertyInfo">The target property.</param>
/// <returns>The list of mapped <see cref="PropertyHandlerAttribute"/> objects.</returns>
public static IEnumerable<PropertyValueAttribute> GetPropertyValueAttributes(this PropertyInfo propertyInfo) =>
propertyInfo?
.GetCustomAttributes()?
.Where(e =>
StaticType.PropertyValueAttribute.IsAssignableFrom(e.GetType()))
.Select(e =>
(PropertyValueAttribute)e) ??
PropertyValueAttributeCache.Get(propertyInfo?.DeclaringType, propertyInfo);

/// <summary>
/// Returns the value of the data entity property. If the property handler is defined in the property, then the
/// handled value will be returned.
Expand Down

0 comments on commit 1ca1638

Please sign in to comment.