DapperLib / DapperLib/Dapper

Data table generator for table-valued parameters

Open
#389 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
C#
Stars
18.4k
Forks
3.7k
Avg merge
5h 8m
Merged PRs (30d)
1

Description

When using .AsTableValuedParameter(), is there any reason why it does not generate IL code for creating a DataTable from the object passed in? As it is currently, you have to create the DataTable in user code differently for every table type that you want to use.

I'm thinking that in the same way POCOs are being mapped to the table schema for constructing the DbCommand, they could be mapped to the table type for constructing the DataTable. With an extension method like the existing ones, the API would be:

// Given a table type KeyValueTableType with columns Key and Value
var keyValueTableParameter = new [] { 
    new KeyValueTableType { Key = "abc", Value = "qwe" },
    new KeyValueTableType { Key = "xyz", Value = "asd" }
}.AsTableValuedParameter();
// would have signature ICustomQueryParameter AsTableValuedParameter<T>(IEnumerable<T> rows)

The extension method above would (in addition to it's current implementation) be generating a dynamic method that essentially does:

var table = new DataTable();
table.Columns.Add("Key", typeof(string));
table.Columns.Add("Value", typeof(string));
foreach (var row in rows)
{
    table.Rows.Add(new[] { row.Key, row.Value });
}

Is this something that you think makes sense? Or is it too specific of a use-case, and should go into contrib?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Begin with the current .AsTableValuedParameter implementation and the existing POCO-to-table-schema mapping used when constructing DbCommand. Determine whether a generic IEnumerable overload can produce the requested DataTable for each table type; done means the proposed API and row/column mapping work without per-type user DataTable construction, with scope decided between core and contrib.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, sql
Domain
database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.