-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#873 Added a base class for the DbParameter property value setter.
- Loading branch information
1 parent
41762f9
commit b0940a9
Showing
7 changed files
with
113 additions
and
58 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
RepoDb.Core/RepoDb/Attributes/ParameterPropertyValueSetterAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System; | ||
using System.Data; | ||
using System.Reflection; | ||
|
||
namespace RepoDb.Attributes | ||
{ | ||
/// <summary> | ||
/// An attribute that is being used to set a value to any property of the <see cref="IDbDataParameter"/> object. | ||
/// </summary> | ||
public class ParameterPropertyValueSetterAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Creates a new instance of <see cref="ParameterPropertyValueSetterAttribute"/> class. | ||
/// </summary> | ||
/// <param name="parameterType">The type of the parameter.</param> | ||
/// <param name="propertyName">The name of the property to be set.</param> | ||
/// <param name="value">The value to be set.</param> | ||
public ParameterPropertyValueSetterAttribute(Type parameterType, | ||
string propertyName, | ||
object value) | ||
{ | ||
ParameterType = parameterType; | ||
PropertyName = propertyName; | ||
Value = value; | ||
} | ||
|
||
// Properties | ||
|
||
/// <summary> | ||
/// Gets the represented <see cref="Type"/> of the <see cref="IDbDataParameter"/> object. | ||
/// </summary> | ||
public Type ParameterType { get; } | ||
|
||
/// <summary> | ||
/// Gets the name of the target property to be set. | ||
/// </summary> | ||
public string PropertyName { get; } | ||
|
||
/// <summary> | ||
/// Gets the value that is used to set property. | ||
/// </summary> | ||
public object Value { get; } | ||
|
||
// Methods | ||
|
||
/// <summary> | ||
/// Gets the instance of the <see cref="PropertyInfo"/> based on the target property name. | ||
/// </summary> | ||
/// <returns></returns> | ||
internal PropertyInfo GetPropertyInfo() => | ||
ParameterType.GetProperty(PropertyName); | ||
|
||
/// <summary> | ||
/// Sets the value of the <see cref="IDbDataParameter"/> object. | ||
/// </summary> | ||
/// <param name="parameter">The instance of the <see cref="IDbDataParameter"/> object.</param> | ||
/// <param name="throwError"> | ||
/// Throw an error if the parameter instance is null or the type is different from the | ||
/// <see cref="ParameterType"/> property. | ||
/// </param> | ||
internal void SetValue(IDbDataParameter parameter, | ||
bool throwError = true) | ||
{ | ||
if (parameter == null) | ||
{ | ||
if (throwError) | ||
{ | ||
throw new NullReferenceException("Parameter"); | ||
} | ||
return; | ||
} | ||
|
||
if (ParameterType != parameter?.GetType()) | ||
{ | ||
if (throwError) | ||
{ | ||
throw new InvalidOperationException($"The instance of the given parameter must be of type '{ParameterType.FullName}'."); | ||
} | ||
return; | ||
} | ||
|
||
GetPropertyInfo().SetValue(parameter, Value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters