views:

58

answers:

2

So I've just been experimenting with ASP.NET MVC and I'm really starting to like the way everything has one single purpose (as far as the separation of purpose between the models, views and controllers) but I'm still having some trouble applying my basic knowledge to a somewhat practical purpose.

So, I was thinking of trying to create an ASP.NET MVC version of my original JavaScript polynomial root finder. It should be relatively simple enough. Obviously it's not going to solve for all roots of every single degree (at least at the moment) but I'm at least trying to get Quadratic-Quartic.

So, I have 1 Controller in addition to my HomeController. This is called CalculateController and will handle all of the controlling related to any pages dealing with calculating for roots of a certain type of Polynomial.

Therefore, I have 3 Views, 1 for each of the different kind of polynomial, Quadratic.aspx, Cubic.aspx and Quartic.aspx.

Now, I was thinking I would need a Model here to handle my "business logic" in this case, the actual computing and algebraic calculating of the roots.

This is where I need some feedback.

Should I create one Calculate class? That is what I have at the moment. In the class, I declare 5 different instance string variables (I think that's the correct terminology):

public int aValue;
public int bValue;
public int cValue;
public int dValue;
public int eValue;

These will store the values of each of the terms. Now, is this a bad idea, considering that Quadratic and Cubic won't be using all of these instance fields? If so, then I could just create separate classes for each separate polynomial type.

OK, now, I also have to receive some input from my form in order to assign the inputted values into these variables.

I'm a little stumped about how I should do this.

I have a form here on my quadratic view:

<% using(Html.BeginForm("QuadraticForm", "Calculate")) %>
<% { %>
    a: <%= Html.TextBox("quadraticAValue") %>
    <br />
    b: <%= Html.TextBox("quadraticAValue") %>
    <br />
    c: <%= Html.TextBox("quadraticAValue") %>
    <br />
    <input type="submit" id="quadraticSubmitButton" />
<% } %>

Very simple, very basic.

Now, how should assign these inputted integers into a new object of the Calculate class through a Quadratic() method?

UPDATE:

OK, I have some more knowledge than before due to some research, so I'll leave the original question in its entirety above, and update below with some of the info I found out:

I'm going to have a separate class in the Models folder for each different type of polynomial. Currently, I'm experimenting with the QuadCalc.cs file, the Model for the Quadratic Polynomials.

Here is the code I have so far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace RootFinder.Models
{
    public class QuadCalc
    {
        public double quadraticAValue { get; set; }
        public double quadraticBValue { get; set; }
        public double quadraticCValue { get; set; }

        public void QuadCalculate(double quadraticdAValue, double quadraticBValue, double quadraticCValue, out double x1, out double x2)
        {
            //Quadratic Formula: x = (-b +- sqrt(b^2 - 4ac)) / 2a

            //Calculate the inside of the square root
            double insideSquareRoot = (quadraticBValue * quadraticBValue) - 4 * quadraticdAValue * quadraticCValue;

            if (insideSquareRoot < 0)
            {
                //There is no solution
                x1 = double.NaN;
                x2 = double.NaN;
            }
            else
            {
                //Compute the value of each x
                //if there is only one solution, both x's will be the same
                double sqrt = Math.Sqrt(insideSquareRoot);
                x1 = (-quadraticBValue + sqrt) / (2 * quadraticdAValue);
                x2 = (-quadraticBValue - sqrt) / (2 * quadraticAValue);
            }
        }
    }
}

In the code above, I get and set the values of the forms (not really sure how the whole string to double thing is gonna work out, but we can worry about that later xD) and then use those as parameters of a method built around the oh so famous quadratic "formula".

Now, in my Calculate controller, this is my code which applies to the Quadratics:

    [AcceptVerbs(HttpVerbs.Get)]
    public ViewResult Quadratic()
    {
        return View();
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ViewResult Cubic(QuadCalc boss)
    {
        return View();
    }

Now, in the above code, instead of boss what should I place here? I cannot leave it just omitted but I believe it should be the name of an object? Problem is, I never specified an object in my Model, should I do that?

+1  A: 

I usually call my posted models postedModel.

jfar
That wouldn't affect my outcome, right?
BOSS
postedModel is pretty ambiguous. I usually name it whatever I'm expecting: `person`, `tradeParameters`, `account`, etc.
Ryan
+2  A: 

I'm a little confused by your terminology. Maybe it was a mistype? You don't specify an object in your model. The Model is a class, an object instance of which will be used by the View. The MVC model binding will work in the background to map your input values to the class you specified as the parameter for this View's corresponding Controller method.

I created a sample ASP.NET MVC 2 project just to hash some of this out and get buildable sample code. I named the project PolynomialRootFinder. I created a QuadCalc.cs class file in the Models folder, and a Calculate subfolder in the Views folder with a QuadCalcIndex.aspx view page in it (I know the page naming is messy there. You might want to create further subfolders or use some other convention, but this is just a quick-and-dirty.).

Quick change to your QuadCalc.cs code: I would change the signature of QuadCalculate to this:

public void QuadCalculate(out double x1, out double x2)

No need to pass in parameters that you already have specified as properties of the class, so I took out the quadraticXValue params. Actually I would go further and make x1 and x2 be properties of the class, but that's just me.

What follows next depends on how you implement this, but I'm going to assume a particular implementation and go from there. Let's say QuadCalcIndex.aspx is the input form and QuadCalcResult.aspx displays the calculation results. Your CalculateController.cs code would look something like this:

    public ActionResult QuadCalcIndex()
    {
        return View();
    }

    public ActionResult QuadCalcResult(QuadCalc quadCalc)
    {
        double x1;
        double x2;
        quadCalc.QuadCalculate(out x1, out x2);
        ViewData["x1"] = x1;
        ViewData["x2"] = x2;
        return View();
    }

Note that the built-in model binder is smart enough to map your quadraticXValue inputs to corresponding properties of the same name in the quadCalc object, which is why you don't have to manually assign those values to the object.

The meat of QuadCalcResult.aspx would look something like this:

x1: <%= ViewData["x1"] %><br />
x2: <%= ViewData["x2"] %>

Modify file/class/property/method/variable/etc. names to taste. I typically like to return a ViewModel type of class to show values like x1 and x2 using a strongly-typed view, but the ViewData method works and is good for a quick example.

Yohan P