Showing posts with label Asp.net MVC. Show all posts
Showing posts with label Asp.net MVC. Show all posts

Sunday, November 15, 2015

Asp.net MVC - Application Architecture


Don't directly call your database in controller.

In your Asp.net MVC Application, you should not place your database logic inside your controller actions. Mixing your database and controller logic makes your application more difficult to maintain over time. It is recommended that you place your database logic in separate repository layer.

Don't directly call your repository in controller. 

You should create another separate layer called service layer or business logic layer, and put your application business logic. roles and validations. Service layer communicates with the repository layer, process the data into information and pass it the controller.

Don't coupled your service layer with controller.

You should isolate your service layer with controller. Service layer must be in-depended, means it should not depended on the plate-form depended objects. So any application can use it without worrying about frame-work version and plate-form.
for example: The service layer should be able to use in WinForms , WebForms , WPF or Asp.net MVC Applications.

Don't put your UI Logic in controller.

UI may have some kind of logic, such as, display the user panel if the user is logged In, show/hide buttons, show the publish button if the publisher is logged in and so on. This logic may have several conditions, functions and properties, We should avoid to put the UI logic directly in our controller because controller is not responsible for handling the logic, but the application flow.
Mixing your application flow and UI/Business logic makes more difficult to maintain your application. We should separate our UI Logic in form of view models, and view models should be called by controller.
Controller is responsible for taking the input from user and pass it to the view models, view model take the input from controller, apply some UI logic if needed, and pass it to he service layer, service layer process it and pass it to repository, and repository is responsible for manipulate the database. 



Thursday, October 22, 2015

How to prevent partial page to access directly in asp.net MVC.

Use the ChildActionOnly Attribute on Action of your Controller. 
For example:

        [HttpGet]
        [ChildActionOnly]
        public ActionResult GetProductList() {
            var vm = new ViewModels.EntryListViewModel(Models.UIdentity.Id);
            return View(vm);
        }

ASP.NET MVC DisplayFor Html Helper Set Default Value

Use use DisplayFor Html Helper for displaying our model text in to razor view. It is actually render Span <span> tag in our html document. Sometime our Model returns null values and we required to display the text over null value like this. "Null" , "N/A" ,"$0.00" and so on...
Here is the simplest solution.

In your Model Class (C#). 


Import the namespace:
using System.ComponentModel.DataAnnotations;

Decorate the your property. 

[DisplayFormat(NullDisplayText="N/A")]
public string LabelText { get; set; }

Thats it!.