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

Friday, 23 May 2014

Difference between ASP.NET WebForms and ASP.NET MVC?

ASP.NET Web Forms uses Page controller pattern approach for rendering layout. In this approach, every page has its own controller i.e. code-behind file that processes the request. On the other hand, ASP.NET MVC uses Front Controller approach. In this approach a common controller for all pages, processes the requests.


Friday, 9 May 2014

What are the main advantages of MVC pattern?

  1. Provides clean separation of concerns(SoC) between UI(Presentation layer), Model(transfer object/domain object/entities) and Business logic(Controller).
  2. Enables Test Driven Development (TDD)./Easy to unit test.
  3. Easy integration with JavaScript frameworks.
  4. Improved re-usability of view,model. One can have multiple view which can point to the same model & vice versa.
  5. Enables the full control over the rendered HTML, lots of new helper function, utilities.
  6. Mobile project template using JQuery mobile.
  7. Razor view engine, Razor syntax is easier to type, doesn't have the XML like heavy syntax.
  8. Support for .Net4 data-annotations/ improved model validation.
  9. Better javascript support with unobtrusive javascript, jqueryvalidation & Json binding.
  10. Greater control and flexibility with support for dependencies resolution and global action filter.
  11. Following the design of stateless nature of the web.
  • RESTful urls that enables SEO.
  • No ViewState and PostBack events

Advantages of using strongly typed views ?

There are several ways available to pass data from a controller to a view in an ASP.Net MVC application.
1.ViewBag or ViewData   2.Data Strongly typed view
The following are the advantages of using strongly typed views. We will get
  1. Intellisense
  2. Compile time error checking

What is tempData in MVC?

TempData:
TempData is also a dictionary derived from TempDataDictionary class and stored in short lives session and it is a string key and object value. The difference is that the life cycle of the object. TempData keep the information for the time of an HTTP Request. This mean only from one page to another.
  • when you redirect, “Tempdata” helps to maintain data between those redirects.
  • It internally uses session variables.
  • It requires typecasting for complex data type and check for null values to avoid error.
  • It’s life is very short and lies only till the target view is fully loaded.
  • It is used to store only one time messages like error messages, validation messages. 

public ActionResult Index()
{
  var model = new Review()
            {
                Body = "Start",
                Rating=5
            };
    TempData["ModelName"] = model;
    return RedirectToAction("About");
}
<pre><pre lang="cs">public ActionResult About()
{
    var model= TempData["ModelName"];
    return View(model);
}

What is ViewBag & ViewData ?

ASP.NET MVC offers us three options ViewData, VieBag and TempData for passing data from controller to view and in next request.ViewData and ViewBag are almost similar and TempData performs additional responsibility. you'll use the ViewData, ViewBag, and TempData objects for the purposes of transporting small amounts of data from and to specific locations (e.g., controller to view or between views). Both the ViewData and ViewBag objects work well in the following scenarios:
  • Incorporating dropdown lists of lookup data into an entity
  • Components like a shopping cart
  • Widgets like a user profile widget
  • Small amounts of aggregate data
Similarities between ViewBag & ViewData :
  1. Helps to maintain data when you move from controller to view.
  2. Used to pass data from controller to corresponding view.
  3. Short life means value becomes null when redirection occurs. This is because their goal is to provide a way to communicate between controllers and views. It’s a communication mechanism within the server call.
Difference between ViewBag & ViewData:
  1. ViewData is a dictionary of objects that is derived from ViewDataDictionary class and accessible using strings as keys.
  2. ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
  3. ViewData requires typecasting for complex data type and check for null values to avoid error.
  4. ViewBag doesn’t require typecasting for complex data type.
ViewBag & ViewData Example:

public ActionResult Index()
{
    ViewBag.Name = "Monjurul Habib";
    return View();
}
public ActionResult Index()
{
    ViewData["Name"] = "Monjurul Habib";
    return View();
} 
In View:

@ViewBag.Name 
@ViewData["Name"] 

ViewData VS ViewBag VS TempData ?


ViewData
ViewBag
TempData
It is Key-Value Dictionary collection
It is a type object
It is Key-Value Dictionary collection
ViewData is a dictionary object and it is property of ControllerBase class
ViewBag is Dynamic property of ControllerBase class.
TempData is a dictionary object and it is property of controllerBase class.
ViewData is Faster than ViewBag
ViewBag is slower than ViewData
NA
ViewData is introduced in MVC 1.0 and available in MVC 1.0 and above
ViewBag is introduced in MVC 3.0 and available in MVC 3.0 and above
TempData is also introduced in MVC1.0 and available in MVC 1.0 and above.
ViewData  is also work with .net framework 3.5 and above
ViewBag  is only  work with .net framework 4.0 and above
TempData  is also work with .net framework 3.5 and above
Type Conversion code is required while enumerating
In depth, ViewBag is used dynamic, so there is no need to type conversion while enumerating.
Type Conversion code is required while enumerating
It value become null if redirection is occurred.
Same as ViewData
TempData is used to pass data between two consecutive requests.
It lies only during the current request.
Same as ViewData
TempData is only work during the current and subsequent request

Wednesday, 23 April 2014

MVC vs. 3-tier architecture?


  • At first glance, the three tiers may seem similar to the model-view-controller (MVC) concept; however, topologically they are different. A fundamental rule in a three tier architecture is the client tier never communicates directly with the data tier; in a three-tier model all communication must pass through the middle tier. Conceptually the three-tier architecture is linearMVC architecture is triangular.
  • 3 tier architecture is a pattern used for a completely different reason. It separates the entire application into meaningful "groups": UI, Business Logic, Data Storage.
  • In MVC : MVC architecture is triangular: the view sends updates to the controller, the controller updates the model, and the view gets updated directly from the model.
  • In Three Tier : A three tier architecture is the client tier never communicates directly with the data tier In a Three-tier model all communication must pass through the middle tier.

How to improve applications performance which is hosted in cloud ?

Improving the performance of an application hosted in Microsoft Azure involves a combination of optimizing your application code, leveraging...