1

Calculations.csクラスに次のコードがあります。

public decimal decPaymentPlan(QuoteData quoteData)
    {
        if (quoteData.StepFilingInformation.PaymentPlanRadioButton 
            == StepFilingInformation.PaymentPlan.No)
            return PriceQuote.priceNoPaymentPlan;
        else
            return PriceQuote.pricePaymentPlanChapter7; //may want to switch
                                                        //to Chapter13 value
    }

public decimal CalculateChapter7(QuoteData quoteData)
    {
        decimal total = PriceQuote.priceChapter7;
        total += this.decPaymentPlan(quoteData); //want to be able to tell
                                                 //which to use, 7 or 13
        return total;
    }

私はdecPaymentPlan、最終的なリターンがである余分なものを避けることができるかどうかを確認しようとしていますpricePaymentPlanChapter13。私はそれを切り替える方法があるかもしれないと思いました。

そうでなければ、私は次のことをしなければならないでしょう:

public decimal decPaymentPlanChapter7(QuoteData quoteData)
    {
        ...
        else
            return PriceQuote.pricePaymentPlanChapter7;
    }

public decimal decPaymentPlanChapter13(QuoteData quoteData)
    {
        ...
        else
            return PriceQuote.pricePaymentPlanChapter13;
    }

...

//the following will appear anyway, but rather than just using
//one method call which switches the choice based on something
public decimal CalculateChpater7(QuoteData quoteData)
    {
        ...
        //instead of decPaymentPlan(quoteData) + something to switch
        total+= this.decPaymentPlanChapter7(quoteData);
        ...
    }

public decimal CalculateChpater13(QuoteData quoteData)
    {
        ...
        //instead of decPaymentPlan(quoteData) + something to switch
        total+= this.decPaymentPlanChapter13(quoteData);
        ...
    }

このようなことは(そしてどのように)実行可能ですか?ありがとう。コードサンプルまたはガイダンスに感謝します。

更新: これは私のコントローラーです:

public ActionResult EMailQuote()
{
    Calculations calc = new Calculations();

    QuoteData quoteData = new QuoteData
    {
        StepFilingInformation = new Models.StepFilingInformation
        {
            //just moking user input here temporarily to test out the UI
            PaymentPlanRadioButton = Models.StepFilingInformation.PaymentPlan.Yes,
        }
     };

     var total = calc.CalculatePrice(quoteData);
     ViewBag.CalculatePrice = total; // ADDED THIS LINE
     return View(quoteData);
}

また、第7章と第13章のPriceQuoteに値を設定しました(例:public static decimal priceChapter7 { get { return 799; } }

4

4 に答える 4

1

何をしているのかを理解せずに提案を確認するのは難しいですが、メソッド間の唯一の違いが使用する値のセット(1つはchapter7用、もう1つはchapter13用)である場合は、これらの値を使用するのが理にかなっています。 PriceQuoteから、これらの値を保持するための基本タイプを作成します。その場合、decPaymentPlanおよびその他のメソッドは、そのタイプのインスタンスのみを取得します。例えば:

class Chapter // for lack of a better name
{
    public decimal PaymentPlan { get; set; }
    public decimal Price { get; set; }
    ....
}

次に、Chapterパラメーターを取得するようにメソッドを変更します

public decimal decPaymentPlan(QuoteData quoteData, Chapter chapter)
{
    if (quoteData.StepFilingInformation.PaymentPlanRadioButton 
        == StepFilingInformation.PaymentPlan.No)
        return PriceQuote.priceNoPaymentPlan;
    else
        return chapter.PaymentPlan;
}

public decimal Calculate(QuoteData quoteData, Chapter chapter)
{
    decimal total = chapter.Price;
    total += this.decPaymentPlan(quoteData, chapter);

    return total;
}

これで必要なのは、Chapterの2つのインスタンス(1つは7用、もう1つは13用)だけで、それに応じてcalculateメソッドを呼び出します。

更新:「それに応じてcalculateメソッドを呼び出す」とはどういう意味かを少し詳しく説明するために、たとえば2つの静的変数があったとします(アプリケーションでは、おそらくCalculations.csで意味があります)。

static Chapter Chapter7 = new Chapter() { Price = 799.99, PaymentPlan = 555.55 };
static Chapter Chapter13 = ...

次に、コントローラーで、次のように記述できます。

ViewBag.Chapter7Total = calc.CalculatePrice(quoteData, Chapter7);
ViewBag.Chapter13Total = calc.CalculatePrice(quoteData, Chapter13);
于 2012-05-24T17:36:47.757 に答える
1

ビジネスロジックを視覚化レイヤーと混合しています。

if(quoteData.StepFilingInformation.PaymentPlanRadioButton == StepFilingInformation.PaymentPlan.No)

より良い設計は、MVC、MVP、MVVMなどの変更が適用されるモデルを持つことです。

例:

public class View
{
    private Model _model = new Model();

    public View()
    {
    }

    public Controller Controller
    {
        get;
        set;
    }

    private void OnButton1Click(object sender, EventArgs args)
    {
        _model.Option = Options.Option1;
    }

    private void OnSaveClick(object sender, EventArgs args)
    {
        if (Controller != null)
            Controller.ApplyChanges(_model);
    }
}

コントローラは、ビュー構造から解放されたビジネスロジックを適用できるため、2つのうちどちらかを自由に変更できます。

例えば

public class Controller
{
    Model Model
    {
        get;
        set;
    }

    decimal CalculateSum()
    {
        return Model.Items.Aggregate((a, b) => a + b);
    }
}
于 2012-05-24T17:37:20.937 に答える
1

7と13の違いは何ですか?私はただすることを選ぶでしょう:

if (quoteData.StepFilingInformation.PaymentPlanRadioButton ==
StepFilingInformation.PaymentPlan.No)              
      return PriceQuote.priceNoPaymentPlan;          
else if (//whatever fulfills ch. 7)             
      return PriceQuote.pricePaymentPlanChapter7;
else //ch. 13
      return PriceQuote.pricePaymentPlanChapter13;
于 2012-05-24T17:26:37.797 に答える
1

の列挙を作成し、Chaptersそれを2番目のパラメーターとしてdecPaymentPlanメソッドyesに渡すことができるようです。

于 2012-05-24T17:28:58.420 に答える