【发布时间】:2018-02-22 21:41:12
【问题描述】:
我目前正在尝试在调用方法时使用字典值来命名可选参数。我不确定这对 c# 是否可行,但我对使用动态 SQL 的查询做了类似的事情。
string[] dobArrayKey = {"dob: "};
string[] dobArrayValue = {txtDob.Text};
string[] ptntNumArrayKey = { "PatientID: " };
string[] ptntNumArrayValue = { txtOfficeMR.Text};
string[] nameArrayKey = { "FirstName: ", "LastName: " };
string[] nameArrayValue = { txtFirstname.Text, txtLastname.Text };
List<List<string>> searchResults = new List<List<string>>();
Dictionary<string[], string[]> searchCriteria = new Dictionary<string[], string[]>
{
{dobArrayKey,dobArrayValue}
,{ptntNumArrayKey,ptntNumArrayValue}
,{nameArrayKey,nameArrayValue}
};
foreach (var item in searchCriteria)
{
if (item.Value[0] != "" && item.Value[0] != null)
{
searchResults.Add(new List<string>());
for (int x = 0; x <= item.Key.Count(); x++)
{
string strJSON = doPatientSearch(Convert.ToInt32(au.UserID)
, Convert.ToInt32(Session["PracticeID"]), au.SessionID, item.Key[x].ToString() : item.Value[x].ToString() );
PatientSearchResponse ptLi = JsonConvert.DeserializeObject<PatientSearchResponse>(json2);
foreach (PatientList3 patient in ptLi.PatientList)
{
searchResults[x].Add(patient.PatientNumber);
}
}
}
}
public static string doPatientSearch(int UserID, int PracticeID, string SessionID, string PatientID = null,
,string first = null, string last = null, string dob = null, string social = null)
{
//search
}
我的同事建议我通过删除所有可选参数来更改方法本身,而是通过包含所有参数的字典并在方法中处理它们。
我认为这可行,但出于好奇,我想获得一些反馈,并了解我在上述代码中尝试做的事情是否可行。
如果这是不可能的,但有另一种方法可以达到预期的结果,我很乐意看到你的建议。
提前谢谢你。
【问题讨论】:
-
一本字典表明参数实际上可以是任何东西。相反,我会为真正的公共方法使用接口,为任何内部/受保护/私有方法使用类。
IPatientSearchCriteria或PatientSearchCriteria. -
搜索在物理上是如何执行的?在某些时候是否有存储过程调用?
-
@JohnWu 没有执行存储过程。这些参数用于构建 API 调用。
-
API 调用的签名是什么?最终,您传入的任何参数都需要映射到 API,对吧?
-
@JohnWu 如果我正确理解您的问题,是的。签名由从 au 对象中检索到的 userID、sessionID 和 practiceID 组成,我没有将其结构和初始化包括在问题中,因为我认为它没有密切关系。我在循环中尝试做的每次迭代是使用字典搜索条件传递不同的可选参数,然后在事后比较结果。