开发者

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:

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 upda开发者_开发知识库te. Well the problem is I have 2 places in code using my original method. And those 2 places are updating 2 completely different fields in the Cash table. 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.

I need some advice on what to do here. I have 2 methods and they have the same signature. I'm not quite sure what to rename these because both are updating 2 completely different fields in the Cash table:

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? such as the 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:

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. You pass it the Cash object you already have to be saved.


"UpdateCashWithCapture" and "UpdateCashWithTransaction"?


UpdateCashByTransactionInfoID
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.


How about UpdateCashByCaptureID and UpdateCashByTransactionInfoID?


Add the name of the field being updated, i.e.

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:

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:

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜