我可以将这三个相似的功能合并为一个功能吗?

Pra*_*abu 2 .net c# servicestack ormlite-servicestack

我可以将下面三个非常相似的功能合并到一个功能中吗?

所有这三个函数都会更新数据库中的特定列.update语句中的匿名对象用于更新相应的列.不应更改匿名对象中的成员名称,因为它是数据库中列的名称.

我正在使用ormlite-servicestack进行数据库连接.数据库我使用Microsoft SQLServer 2012.

功能1:

//Updating the call status.
private void UpdateCallStatus(string claimId, bool isDisconnected)
{
    _LogFactory.LogInfo(this.GetType(), "Updating call status....\n");

    IDbConnectionFactory maConnectionFactory = new DatabaseConnection().getConnection();
    using (var db = maConnectionFactory.Open())
    {
        db.Update<IVRSCallDetails>(new { IsDisconnected = isDisconnected }, where: callDetail => callDetail.ClaimId == claimId);
    }
}
Run Code Online (Sandbox Code Playgroud)

功能2:

//Updating the selected dtmf by the client using the claimid.
private void UpdateDtmf(string claimId, string selectedDtmf)
{
    _LogFactory.LogInfo(this.GetType(), "Updating Selected DTMF:" + selectedDtmf + "\n");

    IDbConnectionFactory maConnectionFactory = new DatabaseConnection().getConnection();
    using (var db = maConnectionFactory.Open())
    {
        db.Update<IVRSCallDetails>(new { SelectedDTMF = selectedDtmf }, where: callDetail => callDetail.ClaimId == claimId);
    }
}
Run Code Online (Sandbox Code Playgroud)

功能3:

//Updating the isCallMade value..
private void updateIsCallMade(string claimId, bool isCallMade)
{
    _LogFactory.LogInfo(this.GetType(), "Call has been made to the client with claim id: " + claimId + "\n");

    IDbConnectionFactory maConnectionFactoruy = new DatabaseConnection().getConnection();
    using (var db = maConnectionFactoruy.Open())
    {
        db.Update<IVRSCallDetails>(new { IsCallMade = isCallMade }, where: callDetail => callDetail.ClaimId == claimId);
    }
}
Run Code Online (Sandbox Code Playgroud)

Jam*_*iec 5

您可以将其更改为通用方法(以便能够传递类型),也可以Func<object>替换第二个参数来生成匿名对象.

private void updateData<T>(string claimId, Func<object> data)
{
    _LogFactory.LogInfo(this.GetType(), "Call has been made to the client with claim id: " + claimId + "\n");

    IDbConnectionFactory maConnectionFactoruy = new DatabaseConnection().getConnection();
    using (var db = maConnectionFactoruy.Open())
    {
        db.Update<T>(data(), where: callDetail => callDetail.ClaimId == claimId);
    }
}
Run Code Online (Sandbox Code Playgroud)

用法

updateData<IVRSCallDetails>("123", () => new { IsCallMade = true});
updateData<IVRSCallDetails>("123", () => new { SelectedDTMF = selectedDtmf});
Run Code Online (Sandbox Code Playgroud)