简体   繁体   English

如何命名具有相同签名的2种方法

[英]What to name 2 methods with same signatures

Initially I had a method in our DL that would take in the object it's updating like so: 最初我在DL中有一个方法可以接收它正在更新的对象:

internal void UpdateCash(Cash Cash)
{
    using (OurCustomDbConnection conn = CreateConnection("UpdateCash"))
    {
        conn.CommandText = @"update Cash
                             set    captureID = @captureID,
                                    ac_code = @acCode,
                                    captureDate = @captureDate,
                                    errmsg = @errorMessage,
                                    isDebit = @isDebit,
                                    SourceInfoID = @sourceInfoID,
                                    PayPalTransactionInfoID = @payPalTransactionInfoID,
                                    CreditCardTransactionInfoID = @CreditCardTransactionInfoID
                                 where id = @cashID";

        conn.AddParam("@captureID", cash.CaptureID);
        conn.AddParam("@acCode", cash.ActionCode);
        conn.AddParam("@captureDate", cash.CaptureDate);
        conn.AddParam("@errorMessage", cash.ErrorMessage);
        conn.AddParam("@isDebit", cyberCash.IsDebit);
        conn.AddParam("@PayPalTransactionInfoID", cash.PayPalTransactionInfoID);
        conn.AddParam("@CreditCardTransactionInfoID", cash.CreditCardTransactionInfoID);
        conn.AddParam("@sourceInfoID", cash.SourceInfoID);
        conn.AddParam("@cashID", cash.Id);

        conn.ExecuteNonQuery();
    }
}

My boss felt that creating an object every time just to update one or two fields is overkill. 我的老板觉得每次更新一两个字段创建一个对象都是过度的。 But I had a couple places in code using this. 但是我在代码中有几个地方使用它。 He recommended using just UpdateCash and sending in the ID for CAsh and field I want to update. 他建议只使用UpdateCash并发送我想要更新的CAsh和字段的ID。 Well the problem is I have 2 places in code using my original method. 好吧问题是我使用原始方法在代码中有2个位置。 And those 2 places are updating 2 completely different fields in the Cash table. 这两个地方正在更新现金表中2个完全不同的字段。 Before I was just able to get the existing Cash record and shove it into a Cash object, then update the properties I wanted to be updated in the DB, then send back the cash object to my method above. 在我能够获得现有现金记录并将其推入Cash对象之前,然后更新我想要在DB中更新的属性,然后将现金对象发送回上面的方法。

I need some advice on what to do here. 我需要一些关于如何做的建议。 I have 2 methods and they have the same signature. 我有2种方法,它们具有相同的签名。 I'm not quite sure what to rename these because both are updating 2 completely different fields in the Cash table: 我不太确定要重命名这些内容,因为它们都在更新Cash表中的2个完全不同的字段:

internal void UpdateCash(int cashID, int paypalCaptureID)
{
    using (OurCustomDbConnection conn = CreateConnection("UpdateCash"))
    {
        conn.CommandText = @"update Cash
                             set    CaptureID = @paypalCaptureID
                  where id = @cashID";

        conn.AddParam("@captureID", paypalCaptureID);

        conn.ExecuteNonQuery();
    }
}

internal void UpdateCash(int cashID, int PayPalTransactionInfoID)
{
    using (OurCustomDbConnection conn = CreateConnection("UpdateCash"))
    {
        conn.CommandText = @"update Cash
                             set    PaymentSourceID = @PayPalTransactionInfoID
                  where id = @cashID";

        conn.AddParam("@PayPalTransactionInfoID", PayPalTransactionInfoID);

        conn.ExecuteNonQuery();
    }
}

So I thought hmm, maybe change the names to these so that they are now unique and somewhat explain what field its updating: 所以我想嗯,也许更改这些名称,以便它们现在是独一无二的,并在某种程度上解释了它的更新字段:

UpdateCashOrderID

UpdateCashTransactionInfoID

ok but that's not really very good names. 好吧,但那不是很好的名字。 And I can't go too generic, for example: 而且我不能过于笼统,例如:

UpdateCashTransaction(int cashID, paypalTransactionID)

What if we have different types of transactionIDs that the cash record holds besides just the paypalTransactionInfoID? 如果除了paypalTransactionInfoID之外,现金记录还拥有不同类型的transactionID,该怎么办? such as the creditCardInfoID? 例如creditCardInfoID? Then what? 那又怎样? Transaction doesn't tell me what kind. 交易不告诉我是什么样的。 And furthermore what if you're updating 2 fields so you have 2 params next to the cashID param: 此外,如果您要更新2个字段,那么在cashID参数旁边有2个参数:

UpdateCashTransaction(int cashID, paypalTransactionID, someOtherFieldIWantToUpdate)

see my frustration? 看到我的沮丧? what's the best way to handle this is my boss doesn't like my first route? 处理这个的最好方法是我的老板不喜欢我的第一条路线?

Why not just: 为什么不呢:

UpdateCashPaymentSource(int cashID, int PayPalTransactionInfoID)

UpdateCashCapture(int cashID, int paypalCaptureID) 

My boss felt that creating an object every time just to update one or two fields is overkill. 我的老板觉得每次更新一两个字段创建一个对象都是过度的。

He would be right, if you have to create the object every time. 如果你每次都必须创建对象,他就是对的。 The correct response to this is that you should already be using these business objects throughout your app. 对此的正确回答是您应该已经在整个应用程序中使用这些业务对象。 You don't create a new Cash object. 您不创建新的Cash对象。 You pass it the Cash object you already have to be saved. 您将已经保存的Cash对象传递给它。

“UpdateCashWithCapture”和“UpdateCashWithTransaction”?

UpdateCashByTransactionInfoID UpdateCashByTransactionInfoID
UpdateCashByCaptureID() UpdateCashByCaptureID()

?

Would one method and an enum cut it? 一种方法和一个枚举会削减它吗?

internal void UpdateCash(int cashID, int id, FieldName field)
{
    using (OurCustomDbConnection conn = CreateConnection("UpdateCash"))
    {
        conn.CommandText = string.format("update Cash set {0} = @id where id = @cashID", field.ToString());

        conn.AddParam("@id", id);
        conn.AddParam("@cashId", cashId);

        conn.ExecuteNonQuery();
    }
}

public enum FieldName
{
    PayPalCaptureId,
    PayPalTransactionInfoID
}

EDIT: 编辑:

On now reading your edit, I would agree that your original approach would be the way to go, in my opinion - passing in an object and updating all of the associated fields in a database compared to passing in an object property value and updating that in a database, the biggest performance killer will be opening the database connection, not the number of fields relating to one database record. 现在阅读你的编辑,我同意你的原始方法将是我要去的方式 - 传递一个对象并更新数据库中的所有相关字段,而不是传入一个对象属性值并更新它一个数据库,最大的性能杀手将是打开数据库连接,而不是与一个数据库记录相关的字段数。

UpdateCashByCaptureIDUpdateCashByTransactionInfoID怎么样?

Add the name of the field being updated, ie 添加要更新的字段的名称,即

internal void UpdateCash_paypalCaptureID(...)
internal void UpdateCash_PayPalTransactionInfoID(...)

You could encapsulate the update query logic in a class: 您可以将更新查询逻辑封装在类中:

public abstract class CashUpdateQuery
{
    public CashUpdateQuery(int cashId)
    {
        this.CashId = cashId;
    }

    protected int CashId { get; private set; }

    public abstract void SetConnectionProperties(OurCustomDbConnection conn);
}

Then you can have specific subclasses for each update scenario. 然后,您可以为每个更新方案提供特定的子类。 So for your PayPal query you'd have something like this: 因此,对于您的PayPal查询,您将拥有以下内容:

public PaypalTransactionCashUpdateQuery : CashUpdateQuery
{
    private readonly int paypalCaptureId;
    public PaypalTransationCashUpdateQuery(int cashId, int paypalCaptureId)
    {
        this.paypalCaptureId = paypalCaptureId;
    }

    public override void SetConnectionProperties(OurCustomDbConnection conn)
    {
        conn.CommandText = @"update Cash
                            set    CaptureID = @paypalCaptureID
                            where id = @cashID";

        conn.AddParam("@captureID", this.paypalCaptureId);
        conn.AddParam("@cashID", this.CashId);
    }
}

Then your update method can take a query object and use it to set the query properties on the connection and execute it: 然后,您的update方法可以获取查询对象并使用它来设置连接上的查询属性并执行它:

internal void UpdateCash(CashUpdateQuery query)
{
    using(OurCustomDbConnection conn = CreateConnection("UpdateCash"))
    {
        query.SetConnectionProperties(conn);
        conn.ExecuteNonQuery();
    }
}

This means that adding new queries is simply a case of adding a new subclass of CashUpdateQuery. 这意味着添加新查询只是添加CashUpdateQuery的新子类的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM