Page 69 - MSDN Magazine, November 2017
P. 69
multicolor
hit-
highlighting
easy
options
®
Passing Data to Views
There are three different, non-exclusive ways to pass data to a Razor view (in addition to DI via the @inject directive). You can use one of the two built-in dictionaries—ViewData or ViewBag—or you can use strongly typed view model classes. No difference exists between these approaches from a purely functional point of view, and even from a performance perspective the difference is negligible.
From the design perspective, though, I recommend the single channel view model approach. Any Razor view should have a sin- gle source of data and that’s the view model object. This approach demands that you pay close attention to the design of the base class for your view models, and that you avoid using dictionaries and even the @inject directive.
Any Razor view should have a single source of data and that’s the view model object.
I realize that this approach sounds strict, but it comes from experience. Even calling DateTime.Now in the view can be dan- gerous, as it injects data into the view that might not be abstract enough for the application. DateTime.Now indicates the time of the server, not the time of the application. This is a problem for any application that depends on the time of the day for its operations. To avoid these kinds of traps, spend some time in the design of the view model classes to envision the common data you need to have in all views. Also, be sure to always pass data—any data—to the views via the model, avoiding dictionaries and DI as much as possible. While it’s faster to use dictionaries and DI, it won’t be faster later on when you have to refactor some domain functionality in a large application. DI in views is a cool piece of technology, but it should be used with more than just one grain of salt.
Wrapping Up
Views are the foundation of Web applications. In ASP.NET Core, views are the result of processing a template file—typically a Razor template file—that’s mixed up with data provided by a caller that most often is a controller method. In this article, I tried to com- pare the various approaches available to build views and pass data tothem.MostASP.NETCoreliteratureemphasizestheroleoftag helpers and view components, and even Razor Pages and DI, in views. While these are all compelling resources, it’s important not to use them blindly, as they have natural drawbacks that can produce some bad outcomes if you’re not careful. n
Dino Esposito is the author of “Microsoft .NET: Architecting Applications for the Enterprise” (Microsoft Press, 2014) and “Programming ASP.NET Core” (Microsoft Press, 2018). A Pluralsight author and developer advocate at JetBrains, Esposito shares his vision of software on Twitter: @despos.
thanks to the following Microsoft technical expert for reviewing this article: Steve Smith
Instantly Search Terabytes of Data
across a desktop, network, Internet or Intranet site with dtSearch enterprise and developer products
Over 25 search features, with
dtSearch’s document filters support popular file types, emails with multilevel attachments, databases, web data
Developers:
• APIs for .NET, Java and C++
• SDKs for Windows, UWP, Linux, Mac and Android
• See dtSearch.com for articles on faceted search, advanced data classification, working with SQL, NoSQL & other DBs, MS Azure, etc.
Visit dtSearch.com for
• hundreds of reviews and case studies • fully-functional evaluations
The Smart Choice for Text Retrieval® since 1991
dtSearch.com 1-800-IT-FINDS
msdnmagazine.com