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

GetTemplateParameter method in DynamicParameters #1861

Open
wants to merge 1 commit 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
33 changes: 33 additions & 0 deletions Dapper/DynamicParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,39 @@ public T Get<T>(string name)
return (T)val;
}

/// <summary>
/// Param names from templates
/// </summary>
public IEnumerable<string> TemplateParameterNames => templates.Select(t => t.GetType().GetProperties().Select(p => p.Name)).SelectMany(p => p);

/// <summary>
/// Get the value of a parameter that was created from a template
/// </summary>
/// <typeparam name="T">The type of a parameter</typeparam>
/// <param name="name"></param>
/// <returns>The value of the parameter</returns>
/// <exception cref="Exception">When templates is null or the param wasn't found</exception>
public T GetTemplateParameter<T>(string name)
{
if (templates == null)
throw new Exception("There are no templates");

foreach (object t in templates)
{
var property = t.GetType().GetProperties().Where(p => p.Name == name).FirstOrDefault();

if (property != null)
return (T)property.GetValue(t);
}

throw new Exception($"Parameter name {name} not present in templates");
}

/// <summary>
/// Param names from templates and bag
/// </summary>
public IEnumerable<string> AllParameterNames => ParameterNames.Concat(TemplateParameterNames);

/// <summary>
/// Allows you to automatically populate a target property/field from output parameters. It actually
/// creates an InputOutput parameter, so you can still pass data in.
Expand Down
24 changes: 24 additions & 0 deletions tests/Dapper.Tests/ParameterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1593,5 +1593,29 @@ private static int GetExpectedListExpansionCount(int count, bool enabled)
if (delta != 0) blocks++;
return blocks * padFactor;
}

[Fact]
public void TestGetParameterFromAnonymousType()
{
// https://github.com/DapperLib/Dapper/issues/343
// https://github.com/DapperLib/Dapper/issues/1113

var parameters = new DynamicParameters(new { par = 1 });

Assert.Equal(1, parameters.GetTemplateParameter<int>("par"));
Assert.Equal("par", parameters.TemplateParameterNames.First());
}

[Fact]
public void TestGetAllParemeters()
{
var parameters = new DynamicParameters(new { par1 = 1 });
parameters.Add("par2", 2);

var paramNames = parameters.AllParameterNames.ToList();

Assert.Equal("par2", paramNames[0]);
Assert.Equal("par1", paramNames[1]);
}
}
}