私は入力と出力のパラメータを持つプロシージャを格納しています。私は入力クラスと出力クラスのために別々のクラスを持っています。ですから、それぞれのプロパティを追加するのではなく、テンプレートオブジェクトを使って動的パラメータを作成したいのです。
例:
public class StudentInput {
public StudentInput() {
this.StudentId = 1;
}
public int StudentId {get; set}
}
public class StudentOutput {
public string StudentName {get; set;}
public string FavSubject {get; set;}
public int Grade {get; set;}
public int YearOfJoining {get; set;}
}
私は入力としてStudentIdを取得し、StudentName、FavSubject、GradeおよびYearOfJoiningを返すストアドプロシージャ[SP_GetStudentData]
を持っています。
Dapperを使って、私は以下のコードを書いた。
DynamicParameters ip = new DynamicParameters(new StudentInput());
DynamicParameters op = new DynamicParameters(new StudentOutput());
ip.AddDynamicParameters(op);
// Here is the question. How do I tell Dapper that op is a output parameters object? How do I add ParameterDirection output to each of the properties in this object?
SqlMapper.ExecuteSP(connection, SP_GetStudentData, ip, CommandType.StoredProcedure)
問題は、SQL Serverは出力パラメータとしてスカラオブジェクトのみを返すことができることです。したがって、出力パラメータをStudentOutput
クラスにマップすることはできません。必要な値を結果セット(SP内のSELECTステートメントを使用)として返し、その結果セットをクラスにマップするだけです。
var result = connection.Query("SP_GetStudentData @studentId", new { studentId = (new StudentInput()).StudentId }, commandType:StoredProcedure)