首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我可以将这三个相似的函数组合成一个函数吗?

我可以将这三个相似的函数组合成一个函数吗?
EN

Stack Overflow用户
提问于 2017-01-05 18:32:30
回答 1查看 87关注 0票数 2

我可以将下面三个非常相似的函数组合成一个函数吗?

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

我使用ormlite-servicestack进行数据库连接。数据库I使用微软SQLServer 2012。

功能1:

代码语言:javascript
复制
//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);
    }
}

功能2:

代码语言:javascript
复制
//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);
    }
}

功能3:

代码语言:javascript
复制
//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);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-05 18:38:41

您可以将其更改为泛型方法(以便能够传递类型),还可以将第二个参数更改为Func<object>以生成匿名对象。

代码语言:javascript
复制
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);
    }
}

用法

代码语言:javascript
复制
updateData<IVRSCallDetails>("123", () => new { IsCallMade = true});
updateData<IVRSCallDetails>("123", () => new { SelectedDTMF = selectedDtmf});
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41482666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档