Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DynamicParametersExtensions #2123

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Dapper/DynamicParametersExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Reflection;

namespace Dapper
{
/// <summary>
/// An Extention Class to DynamicParameters
/// </summary>

internal static class DynamicParametersExtensions
{
/// <summary>
/// Add Parameter if exists
/// </summary>
/// <param name="dp">Extending DynamicParameter</param>
/// <param name="paramName">Parameter Name</param>
/// <param name="param">Parameter with type of dynamic</param>
internal static void AddIfExists(this DynamicParameters dp, string paramName, dynamic? param)
{
if (param is null)
{
return;
}
dp.Add(paramName, param);
}

/// <summary>
/// Add members of class as parameter; if the member is not null
/// </summary>
/// <typeparam name="T">Generic Type</typeparam>
/// <param name="dp">Extending DynamicParameters</param>
/// <param name="param"></param>
public static void AddParametersIfExists<T>(this DynamicParameters dp, T param)
{
if (param is not null)
{
foreach (PropertyInfo prop in param.GetType().GetProperties())
{

AddIfExists(dp, paramName: prop.Name, param: prop.GetValue(param));

}
}
}
}
}
5 changes: 4 additions & 1 deletion Dapper/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
#nullable enable
#nullable enable
Dapper.DynamicParametersExtensions
static Dapper.DynamicParametersExtensions.AddIfExists(this Dapper.DynamicParameters! dp, string! paramName, dynamic? param) -> void
static Dapper.DynamicParametersExtensions.AddParametersIfExists<T>(this Dapper.DynamicParameters! dp, T param) -> void