Reputation: 1227
Recently, our professor stated that our presentation layer should consist of mostly method calls and that most of our code should be done in the business object and data access layers. My question is does this usually include the code for user input? What I mean is this; I have a form that consists of multiple text boxes so that the user can input values for different things. The user then clicks a button and the information is saved in a database.
The button even method looks like this:
//event handler for data input
public static void btnEnterAbRipperXInfo_Click(object sender, EventArgs e)
{
//convert text box data into int datatype and assign to variable
inAndouts = int.Parse(txtInAndOuts.Text);
forwardBicycles = int.Parse(txtForwardBicycles.Text);
reverseBicycles = int.Parse(txtReverseBicycles.Text);
crunchyFrog = int.Parse(txtCrunchyFrog.Text);
crossLegWideLegSitups = int.Parse(txtCrossLegWideLegSitups.Text);
fiferScissors = int.Parse(txtFiferScissors.Text);
hipRockNRaise = int.Parse(txtHipRockNRaise.Text);
pulseUpsHeelsToHeaven = int.Parse(txtPulseUpsHeelsToHeaven.Text);
vUpRollUpCombos = int.Parse(txtVUpRollUpCombos.Text);
obliqueVUps = int.Parse(txtObliqueVUps.Text);
legClimbs = int.Parse(txtLegClimbs.Text);
masonTwists = int.Parse(txtMasonTwists.Text);
}
Should the code within the above button event method actually go into a business object or data access class instead of the presentation layer class?
This is not homework. I am creating a 90 day exercise program for my son outside of my programming class. I can also use it as a portfolio for when I graduate, therefore, I want to ensure that I am following standard practices.
Upvotes: 2
Views: 1044
Reputation: 223392
Usually in UI Layer or Presentation layer you get the values from the controls(like you are doing now). Then call your Business logic method with all these values. Something like
BAL.ProcessRequest(arg1,arg2,.....);
Now if you have some business rules related to these inputs, you perform then in your business logic. For example if you are passing a product and its ordered quantity and you want to calculate discount on those products based on some business rule then you perform the discount calculation in the business layer.
After that when you are about to submit to database then you call the Data Access Layer. Something like:
DAL.SaveData(arg1,arg2,...);
Reason for having separate layer is basically to loosely couple the application. For example, if you decide to change the under laying database then you would be require to change your Data Access Layer only, not the business layer or the presentation layer.
Lets say if you want to change the UI/Presentation layer from Web application to Desktop application, then in case change will only be made in Presentation layer. Business layer and data access layer will remain unchanged. If you are thinking about passing the textbox controls to Business layer then your application will be coupled (UI/Presentatioin layer will be coupled with Business Layer).
Here is an article explaining Three Tier Architecture.
Upvotes: 1
Reputation: 65461
Your code reads values from the UI.
This code can only be in the UI layer, since the business layer is not able to access the UI.
Upvotes: 1
Reputation: 41256
No, this would not be appropriate for domain or data layer objects. Because you are interacting directly with presentation elements, this is just fine for the UI layer.
Now, once you have those values pulled out and parsed into the appropriate types, THEN it would be a good idea to transfer that work off the UI layer.
In addition, you should consider using int.TryParse
to convert your values, because your form will throw an exception if you enter a non-numeric value into once of your text boxes.
Upvotes: 0
Reputation: 33183
The BOL takes care of specific business checks / cases that need to be sanitized before going to the DAL (data access layer). The DAL just takes the inputs from the BOL and passes them down to the database.
If your textbox values don't need any type of business logic you can pass them to the BOL to freely pass them to the DAL.
From the looks of your code there is no checking / validation needed. Although it may seem redundant, just to be consistent with your project, you can still pass them to the BOL. In essence your BOL won't act upon these values - it will merely take these values and pass it up to the DAL.
Upvotes: 0