views:

169

answers:

2

Hello all!

I have an application for entering in serial numbers to a database. A serial number has a set number of attributes that defines it and the the user must/may provide them to generate.

public class Serial
{
    public string Number {get; set;}
    public string Part {get; set;}
    public string MfgOrder {get; set;}
    public string CusOrder {get; set;}
    public string Note {get; set;}
    ... etc ...
}

Now the start point of this application asks the user for one of several pieces of information (for example a part number or manufacturing order, etc). This start point may already fill in some of the required user input. I'd like to then take those known items and alter the form based on them.

For example. If two of the pieces of information are Part Number and Mfg Order Number, and the user supplies the Mfg Order Number (which has a relationship to the Part Number from the database) I'd like to display these values but not allow them to be edited. If instead the user just gives me a Part Number, I want to allow the Mfg Order to be presented as a textbox with (maybe) optional or required next to it.

public class MfgOrder
{
    public string MfgOrder {get; set;}
    public string Part {get; set;}
}

...

MfgOrder order = new MfgOrder(some_user_value); // queries database, returns populated object
Serial serial = new Serial() {
    MfgOrder = order.MfgOrder,
    Part = order.Part
};

This application is working right now by just having if/then conditions in the UI -- if you gave me a Mfg Order, dispaly it this way, if you gave me something else, do it this way, etc. The problem is several new options have been requested and continually chaining if/then statements is getting really ugly.

if(serial.comes_from_mfgOrder == true)
{
    %>Manufacturing Order: <%=serial.MfgOrder %><%
} else if (serial.comes_from_part_number == true) {
    %>Manufacturing Order: <%=Html.Textbox("MfgOrder")%><%
} else if // continue this for way too long now ...

Is there a good design pattern here?

Thanks!

+1  A: 

If there's an IF, make a Helper.

Craig Stuntz
I haven't full read link so I might be missing the point, but I agree moving it to a helper is definitely something I should do regardless. The thing is I'd still like to replace a chain of if/then statements with a more elegant solution if possible.
eyston
Well, the most significant problem I have with the example you show is not the number of ifs, but that it seems to put business logic into the view. Instead, the model should specify which fields are required, and the view should just obey it. Ifs by themselves are fine.
Craig Stuntz
I agree. I'd like to have all of this defined in the model, but not the actual 'Serial' model, which could be why I'm having such a mental road block.
eyston
And the reason I don't like the if's is that I would have to go redefine every conditional statement whenever I add a new 'start-point'. If instead if it was in a model, I write a new model for the new 'start-point' and the view acts accordingly.
eyston
Right. Use a data transfer object for the view, rather than passing the entity directly. The DTO can have properties to configure the view which Serial does not have. You can use LINQ to create the DTOs "on-the-fly."
Craig Stuntz
+1  A: 

I believe the pattern you're looking for is Model View Controller, or MVC.

http://www.dotnetheaven.com/Uploadfile/rmcochran/MVC_intro02012006001723AM/MVC_intro.aspx

Steven Behnke