-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
show initial type-handler design in line with DapperLib/Dapper#1924
- Loading branch information
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Dapper.Internal; | ||
using System; | ||
using System.ComponentModel; | ||
using System.Data.Common; | ||
|
||
namespace Dapper; | ||
|
||
/// <summary> | ||
/// Specify that <typeparamref name="TTypeHandler"/> should be used as a custom type-handler | ||
/// when processing values of type <typeparamref name="TValue"/> | ||
/// </summary> | ||
[ImmutableObject(true)] | ||
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)] | ||
public sealed class TypeHandlerAttribute<TValue, TTypeHandler> : Attribute | ||
where TTypeHandler : TypeHandler<TValue>, new() | ||
{} | ||
|
||
/// <summary> | ||
/// Process a parameter value of type <typeparamref name="T"/> | ||
/// </summary> | ||
public abstract class TypeHandler<T> | ||
{ | ||
/// <summary> | ||
/// Configure and assign a value for a parameter | ||
/// </summary> | ||
public virtual void SetValue(DbParameter parameter, T value) | ||
=> parameter.Value = CommandFactory.AsGenericValue<T>(value); | ||
|
||
/// <summary> | ||
/// Interpret the output value from a parameter | ||
/// </summary> | ||
public virtual T Parse(DbParameter parameter) | ||
=> CommandUtils.As<T>(parameter.Value); | ||
} |