How to name 2 methods with identical signatures

Initially, I had a method in our DL that would contribute to the object that it updated like this:

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 thought that creating an object every time just to update one or two fields is redundant. But I had several places in the code using this. He recommended using only UpdateCash and sending the ID for CAsh and the field I want to update. Well, the problem is that I have 2 places in the code using my original method. And these 2 places update 2 completely different fields in the Cash table. Before I could get the existing cash record and drag it into the Cash object, then update the properties that I would like to update in the database, and then send the money object back to my method above.

, . 2 , . , , 2 Cash:

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();
    }
}

, , , , , , :

UpdateCashOrderID

UpdateCashTransactionInfoID

ok, . , :

UpdateCashTransaction(int cashID, paypalTransactionID)

, , , paypalTransactionInfoID? , creditCardInfoID? ? , . , , 2 , 2 cashID:

UpdateCashTransaction(int cashID, paypalTransactionID, someOtherFieldIWantToUpdate)

. ? - ?

+3
8

, .

, . , - . Cash. Cash, .

+2

:

UpdateCashPaymentSource(int cashID, int PayPalTransactionInfoID)

UpdateCashCapture(int cashID, int paypalCaptureID) 
+3

"UpdateCashWithCapture" "UpdateCashWithTransaction"?

+1

UpdateCashByTransactionInfoID
UpdateCashByCaptureID()

?

+1

?

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:

, , , , -, , , , .

+1

UpdateCashByCaptureID UpdateCashByTransactionInfoID?

0

, ..

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

:

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

    protected int CashId { get; private set; }

    public abstract void SetConnectionProperties(OurCustomDbConnection conn);
}

. 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 the request object and use it to set the request properties in the connection and execute it:

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

This means that adding new queries is just a case of adding a new subclass of CashUpdateQuery.

0
source

Source: https://habr.com/ru/post/1739372/


All Articles