Thứ Sáu, 5 tháng 10, 2012

Dotnet Tips: Top 10 ASP.NET MVC Best Practices


    Top 10 Best Practices


    In this section we will discuss 10 best practices and tips we should keep in mind when working with ASP.NET MVC applications.

    Tip 1: Disable Request Validation

    Request Validation is a feature that prevents potentially dangerous content from being submitted. This feature is enabled by default. However, at times you might need your application to post HTML markup tags to the server. You would then need this feature to be disabled. Here is how you can do it:

    [ValidateInput(false)]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create([Bind(Exclude = "Id")] Employee empObj)
    {
    }

    Tip 2: Cache Your Data

    You can improve your application's performance to a considerable extent by caching relatively stale data. That way the network bandwidth between the client and the server is also reduced. It is great if you can also cache the rendered action of web pages that are relatively stale, i.e., don’t change much over time.

    public class HomeController : Controller
    {
    [OutputCache(Duration=3600, VaryByParam="none")]
    public ActionResult Index()
    {
    }
    }

    Tip 3: Isolate Data Access Logic From the Controller

    The Controller in an ASP.NET MVC application should never have the Data Access logic. The Controller in an ASP.NET MVC application is meant to render the appropriate view based on some user interface action. You should make use of Repository Pattern to isolate Data Access Logic from the Controller – you might need dependency injection to inject the appropriate Repository to your controller at runtime.

    Tip 4: Using a Master View Model

    We frequently use Master Pages in ASP.NET applications – the same Master Page would be extended by the Content Pages throughout the application to give a similarity as far as look and feel and functionality is concerned. How do we do that in an ASP.NET MVC application? Well, we need a MasterViewModel similar to what is shown in the code snippet below:

    public class ViewModelBase
    {
    public ViewModelBase()
    {

    }
    //Other methods and properties
    }

    Tip 5: Use Strongly Typed Models

    A strongly typed view is a view that defines its data model as a CLR type instead of a weakly typed dictionary that may contain potentially anything. To create a strongly typed view, check the "Create a strongly-typed view" checkbox while you are creating the view. If you plan to create a strongly typed view manually later, ensure that your view "Inherits" System.Web.Mvc.<Your Namespace>.<YourClass>

    Tip 6: Use Data Annotations for Validation

    You can make use of the System.ComponentModel.DataAnnotations assembly to validate your server - side code by simply decorating your model with the necessary attributes. Here is an example:

    public class Employee
    {
    [Required(ErrorMessage="Employee Name Cannot be Blank")]
    public string Name { get; set; }

    // ...
    }

    Tip 7: Take Advantage of Model Binding

    Consider the following code snippet

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create()
    {
    Employee employee = new Employee();
    employee.Name = Request.Form["Name"];

    // ...

    return View();
    }
    You can make use of model binder to save you from having to use the  Request and HttpContext properties - just use FormsCollection instead.  Here is an example.

    public ActionResult Create(FormCollection values)
    {
    Employee employee = new Employee();
    employee.Name = values["Name"];

    // ...

    return View();
    }

    Tip 8: Cache Pages that Contain Shared Data or are Public and don't Require Authorization

    You should not cache pages that need authorization in ASP.NET MVC. You should not cache pages that contain private data or need authorization. Caching pages in ASP.NET MVC is simple - just specify the OutputCache directive as shown in the code snippet below:

    [OutputCache(Duration = 60)]
    public ActionResult Index()
    {
    return View("Index", somedata);
    }

    Tip 9: Use Extension Methods

    You can make use of Extension Methods to simplifies use of LINQ queries that boost application performance too. This can dramatically reduce the amount of code that you would need to otherwise write when writing your LINQ queries, make your LINQ queries manageable and also improve the application's performance.

    Tip 10: Take Advantage of Model Binding

    You can take advantage of Microsoft Velocity - a distributed caching engine to boost the application performance of your ASP.NET MVC applications.
    You can learn more on Velocity from this link: http://blogs.msdn.com/b/velocity/

    0 nhận xét:

    Đăng nhận xét