1

Dynamic vs static type in asp.net mvc

Is car better than motorcycle? It all depends on the scenario you are using it. Without knowing the scenario it is not possible to make that determination!

Is Dynamic typing better than static typing for certain problems/situations.

In this case

I need to show the view when OnException() method of BaseController is called. This is one time thing and i show the error page once instead of yellow screen of death.

Suppose i need to show ErrorMessage and Stacktrace on the view. As this is one time thing is it not better to use dynamic than create a error model for this.

       dynamic obj = new ExpandoObject();
       obj.ErrorMessage = "message";
       obj.StackTrace = "bla bla bal";
       return to view.

What is the best practice. Thanks for the reply.

Mangesh
  • 3,987
  • 2
  • 31
  • 52
  • 1
    If you just need the ErrorMessage and Stacktrace, why not just pass the exception as a view model? – alexn Jun 11 '11 at 18:02
  • The question is generic not just related to errormessage.When is it useful to use Dynamic vs Static. – Mangesh Jul 19 '11 at 17:07

1 Answers1

1

"Is Dynamic typing better than static typing for certain problems/situations."

Of course.

"Suppose i need to show ErrorMessage and Stacktrace on the view. As this is one time thing is it not better to use dynamic than create a error model for this."

In this case you usually have a view model that can carry the errors with it. Sometimes I use a base view model class with:

public class ViewModelBase
{
      public string ErrorMessage { get; set; }

}

Most often I'll put the errors in TempData:

TempData["Errors"] = "";
John Farrell
  • 24,673
  • 10
  • 77
  • 110