Use T4MVC to Remove Magic Strings in ASP.NET MVC Apps 🎩
ASP.NET MVC has a fascination with magic strings. ❌
Basically any time you need to specify an action, a view, or controller, you use a magic string ✨ :
The problem with magic strings is the same problem that ViewBag has ⚠️ :
1️⃣ There's no type checking.
2️⃣ The developer won't catch these errors until runtime, and sometimes not at all.
🔹🔸🔹🔸
T4MVC aims to remove magic strings from MVC and replace them with strongly-typed ActionResults. ✅
It adds a bunch more overloads to methods such as ActionLink(), BeginForm(), and RedirectToAction() so that they can now accept ActionResults as parameters. 🗃
Thereby making them strongly-typed and removing their dependency on magic strings. 💎
Most importantly, it means we can take the above code samples and refactor them to look like this 🤙🏻 :
https://t.me/pgimg/98
[ Article ] : http://bit.do/t4mvc
〰〰〰〰〰〰
#AspMvc #CleanCode #T4MVC
@ProgrammingTip
ASP.NET MVC has a fascination with magic strings. ❌
Basically any time you need to specify an action, a view, or controller, you use a magic string ✨ :
return RedirectToAction("Index", "Home", new { id = 4 });
return View("Details");
@Html.ActionLink("Back to details", "Detals", "User", new {id = 12}, new {@class = "backlink"});
@using (Html.BeginForm("Details", "User", FormMethod.Post))The problem with magic strings is the same problem that ViewBag has ⚠️ :
1️⃣ There's no type checking.
2️⃣ The developer won't catch these errors until runtime, and sometimes not at all.
🔹🔸🔹🔸
T4MVC aims to remove magic strings from MVC and replace them with strongly-typed ActionResults. ✅
It adds a bunch more overloads to methods such as ActionLink(), BeginForm(), and RedirectToAction() so that they can now accept ActionResults as parameters. 🗃
Thereby making them strongly-typed and removing their dependency on magic strings. 💎
Most importantly, it means we can take the above code samples and refactor them to look like this 🤙🏻 :
return RedirectToAction(MVC.Home.Index(4));
return View(MVC.User.Views.ViewNames.Details);
@Html.ActionLink("Back to details", MVC.User.Details(12), new {@class = "backlink"});
@using (Html.BeginForm(MVC.User.Details(), FormMethod.Post))
https://t.me/pgimg/98
[ Article ] : http://bit.do/t4mvc
〰〰〰〰〰〰
#AspMvc #CleanCode #T4MVC
@ProgrammingTip
Telegram
Programming Tips Resources