Showing posts with label Bussiness Logic Layer (BLL). Show all posts
Showing posts with label Bussiness Logic Layer (BLL). Show all posts

Monday, January 20, 2014

Step By Step How to create an MVC View

         By Carmel Schvartzman


In this tutorial we'll learn how to create an MVC View for an Action method  in ASP.NET MVC 4
An MVC View is the responsible for rendering the html for the UI with the user: it is the Presentation level in the ModelViewController software pattern; if so , it is absolutely forbidden to include in it any bussiness logic (wich is holded into the MVC Model) or any application logic (coded inside the MVC Controllers), nor any data fetching(MVC Model responsability, sent via the Controllers).

By convention, the Views are stored inside subfolders of the View folder, named after the corresponding Controllers: then for instance, all the Views you create related to the AccountController will be placed inside the "Account" folder ("Views/Account"). The MVC framework will search a required View to be rendered, in the folder with the name of the Controller, and if it is not there, will search at the "Shared" subfolder. If you want a View to be shared among several Controllers, place it in the "Shared" folder.

A View is just a class called ViewPage: it inherits from the Page class and also implements the IViewDataContainer, in order to hold all the data sent by the Controllers inside its ViewDataDictionary. There is another useful collection named TempDataDictionary, similar to the former, with the difference that it persists ONLY between the current request and the next request (unless you mark the key to be persisted more than that, using the Keep() method). This is useful while passing data between Action methods, or in case of errors.

To create your own View, first let's create a new Controller called "NewViewController", and then add a View to the Index Action method. So right click over the Controllers folder, and add a new Controller:


Name it "NewViewController", and select the template "Empty MVC Controller":


Open the Controller file and find the "Index" Action method:



Right click on it and select the option "Add View":


On the dialog, let the name be "Index", select the "ASPX" View engine, and choose a master page:


The new created View holds several Contents , areas that will be embedded inside the Master page:


Return to the Index Action method, and add a key with a message to the ViewDataDictionary:


Do the same with the TempDataDictionary:


We have just set the Controller to send 2 strings to the View. Now we have to receive the data in the View, so add the folowing code inside the Content2:


We're done. Let's see how it works. Start the application without debugging (CTL - F5):


Now append to the address , the name we give to the Controller, "NewView". The MVC framework will route the request to the "Index" Action method of the "NewView" controller:

And we'll get the following web page rendered with the sent data:



In this tutorial we've learned how to create an MVC View for an Action method  in ASP.NET MVC 4, sending data via the ViewDataDictionary and the TempDataDictionary
That's all!! 
Happy programming.....


כתב: כרמל שוורצמן



Thursday, January 2, 2014

Step By Step How to add Bussiness Logic Validation to an ASP.NET MVC 4 Application

         By Carmel Schvartzman

In this tutorial we'll learn how  to add Bussiness Logic Validation to an ASP.NET MVC4 application


Concerning the Model in a MVC application, there are two kinds of errors that can happen: data type errors or bussiness logic errors. While the data type errors are usually handled using Data Annotations, the later are usually handled in special classes which extend the DAL of the application. Remember that when you use the Entity Framework as the DAL (Data Access Layer) of your MVC, every time you update the conceptual data model to reflect changes made to the data store, the Model classes are regenerates by a tool, meaning that everything that's in it is erased and rewriten. Therefore the Bussiness Rules are usually coded in partial classes which extend the Model classes.

The Bussiness Logic comprehends both Application Logic (the logic of the interaction between the user and the specific application we developed - web win etc) and Data Store Logic (the logic concerning exclusively the data, no matter which kind of application you have).

The Application Logic will usually  be on the Controllers, because it concerns to the specific application, an MVC app in our case.

The Bussiness Data Logic MUST BE located in the Model, in a separate BLL (Bussiness Logic Layer) folder, and usually is coded in partial classes which extend the DAL (Data Access Layer) created for example by the Entity Framework.

In this tutorial we'll create a BLL (Bussiness Logic Layer) coding partial classes which extend the DAL (Data Access Layer). Let's say we have an MVC 4 app connected to an SQL SERVER database with the following tables on it:



We'll state that the user can save temporarily a comment with a Title but no text, or with a Text but no Title. But it is not allowed to save a comment without text and without title. This is a Bussiness Data Logic rule, which we'll code in an extension of the corresponding Comments Entity.

Focus the Model folder and create a new class on it. Inside, code the extension of the Comment class which we want to enable to apply our Bussiness rule:


Add a MetaData class named CommentMetaData, as we did in a previous tutorial , in order to enable Data Type Validation:


Next, do the Comment class to implement the IValidatableObject:


This way will allow us to code a Validate() method to perform the necessary bussiness logic on our entity, returning an IEnumerable collection holding all input errors.


Add the Validate method with its returned ValidationResult collection:



Finally, code the required if clause stating that we wont persist an user comment only in case of the lack of both Title and Text: 



Now let's make use of the IValidatableObject: provided that you already have a form where the user will type her/his comment. If you don't, referer to this tutorial . Go to the Home Controller and find the method which handles POST requests:


There, add an if clause using our Validate() method, which IEnumerable results count will be 0 only in case of valid input:



Open the Comment's form and save two comments, the first having only a Title but no text, the second having Text but no Title:




Now check the database to see the persisted records:



Then try to persist a Comment with no Title nor Text:



The comment has not been saved, because our extended Comment entity didn't allow that.
To see it from inside, set a breakpoint in the Validate() method, and try to save an empty Comment again. You'll see that the runtime enters the if clause , avoiding saving the record:




That's all!! 
Happy programming.....


כתב: כרמל שוורצמן