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

此外,我在 PriceQuote 中为第 7 章和第 13 章设置了一个值(例如,public static decimal priceChapter7 { get { return 799; } }

4

4 回答 4

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并将其作为第二个参数传递给该decPaymentPlan方法,是吗?

于 2012-05-24T17:28:58.420 回答
1

如果不进一步了解您在做什么,很难确定建议,但如果您的方法之间的唯一区别是一组要使用的值(一组用于第 7 章,另一个用于第 13 章),那么采用这些值可能是有意义的出 PriceQuote 并创建一个基本类型来保存这些值。然后您的 decPaymentPlan 和其他方法将只采用该类型的实例。例如:

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

然后,更改您的方法以获取章节参数

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 实例,一个用于 7,另一个用于 13,并相应地调用您的计算方法。

更新:为了详细说明“相应地调用你的计算方法”的意思,假设你有两个静态变量(在你的应用程序中有意义的地方,也许在 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);
    }
}

然后控制器可以应用业务逻辑而不受视图结构的影响,这样您就可以自由地更改两者中的任何一个。

例如

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

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