DynamicParameters.AddDynamicParams interpolation for Stored Procedure
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 18.4k
- Forks
- 3.7k
- Avg merge
- 5h 8m
- Merged PRs (30d)
- 1
Description
Looking through StackOverflow I ran across this question about an error being thrown on stored procedure call about too many arguments. @mgravell explained that the DynamicParameters trusts the custom implementation when adding an object. I thought I'd make an issue with an example of what I'm currently trying to do as an example of why it may be beneficial NOT to trust the custom implementation and still do the analysis for needed parameters.
I have a class as follows :
public class SourceFile {
public int SourceFileID { get; set; }
public SourceFileStatus SourceFileStatus { get; set; }
public int SourceID { get; set; }
public string FileName { get; set; }
public string OriginalPath { get; set; }
public string CurrentPath { get; set; }
public DateTime ProcessStart { get; set; }
public DateTime? ProcessEnd { get; set; }
}
and a stored procedure :
ALTER PROCEDURE [dbo].[SourceFile_Insert]
@SourceID tinyint
,@SourceFileStatusID tinyint
,@FileName nvarchar(100)
,@OriginalPath nvarchar(255)
,@CurrentPath nvarchar(255)
,@ProcessStart datetime
,@ProcessEnd datetime
AS
BEGIN
INSERT INTO [dbo].[SourceFile]
([SourceFileStatusID]
,[SourceID]
,[FileName]
,[OriginalPath]
,[CurrentPath]
,[ProcessStart]
,[ProcessEnd]
)
VALUES
(@SourceFileStatusID
,@SourceID
,@FileName
,@OriginalPath
,@CurrentPath
,@ProcessStart
,@ProcessEnd
)
RETURN (SELECT SCOPE_IDENTITY());
END
I want to call this stored procedure to insert the record before processing starts, then catch exceptions and update the status etc.
I do this by calling the following method :
public async Task<SourceFile> InsertSourceFileAsync(SourceFile file) {
DynamicParameters parameters = new DynamicParameters();
parameters.AddDynamicParams(file);
parameters.Add("@id", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);
await conn.ExecuteAsync("SourceFile_Insert", param: parameters, commandType: CommandType.StoredProcedure);
file.SourceFileID = parameters.Get<int>("@id");
return file;
}
Because SourceFileID is an identity column, it is not included in the stored procedure's parameters, and since there are quite a few parameters (I trimmed down what is actually on my object just for the sake of space), I don't really want to map each one individually just to exclude the SourceFileID one being brought over.
TLDR: Needing to add a ParameterDirection.ReturnValue parameter to a query with DynamicParameters is annoying if you have an object with a lot of properties but don't need some of them.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading DynamicParameters.AddDynamicParams and the ExecuteAsync call with CommandType.StoredProcedure shown in the issue. Compare the SourceFile properties with the stored procedure parameters, including the return-value parameter. Done should mean that extra object properties can be handled without manual mapping while the stored procedure call and return value continue to work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, sql
- Domain
- backend-api-design, databases
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100