magic-string

Getting rid of hardcoded strings in JavaScript with ASP.NET MVC

帅比萌擦擦* 提交于 2019-12-30 11:26:06
问题 We have a few problems in a project I am working on, where we have a lot of JavaScript files, where we have hardcoded URLs to controller actions. Hardcoded URLs are proned to mistyping Hardcoded URLs in JavaScript scripts will cause a breakage if the related controller or action's name is changed Tools like ReSharper (to my knowledge) can't statically analyse it's way to saying the an action is not used, if the URLs pointing to it are hardcoded. Question(s) How can we avoid using hardcoded

Using a lambda expression to avoid using a “magic string” to specify a property

旧巷老猫 提交于 2019-12-22 05:33:23
问题 I am writing a service to take a collection of objects of a particular type and output its primitive, string, and DateTime types to a string in CSV Format. I have both of the below statements working. I find the the lambda based version to be much cleaner. Magic String Version string csv = new ToCsvService<DateTime>(objs) .Exclude("Minute") .ChangeName("Millisecond", "Milli") .Format("Date", "d") .ToCsv(); vs. Lambda Version string csv = new ToCsvService<DateTime>(objs) .Exclude(p => p.Minute

.Net - Strategies to avoid magic string

断了今生、忘了曾经 提交于 2019-12-21 09:54:21
问题 In code at work, we have many uses of magic strings like the following code snippet: if (user.HasRight("Profile.View")) {...} So there are many places where we pass a string as a parameter to see if the user has a specific right. I don't like that because that generates a lot of magic strings. What would be a better way of doing it? Enum, Constant, class ? 回答1: In that specific case, use an Enum. There will be no magic strings and if the Enum changes (in a way that would break the magic

Magic strings in ASP.NET MVC

不羁岁月 提交于 2019-12-05 18:17:53
问题 I have a background in desktop software development and am getting started with learning ASP.NET MVC. In my default HomeController I have the Index action which has code that looks like this: if (!Request.IsAuthenticated) return RedirectToAction("Login", "Account"); In other words, redirect the user to "/account/login". The AccountController.Login action will then handle the user and send him back to the HomeController once he logs in successfully. This code smells to me, perhaps just because

Using a lambda expression to avoid using a “magic string” to specify a property

牧云@^-^@ 提交于 2019-12-05 08:08:21
I am writing a service to take a collection of objects of a particular type and output its primitive, string, and DateTime types to a string in CSV Format . I have both of the below statements working. I find the the lambda based version to be much cleaner. Magic String Version string csv = new ToCsvService<DateTime>(objs) .Exclude("Minute") .ChangeName("Millisecond", "Milli") .Format("Date", "d") .ToCsv(); vs. Lambda Version string csv = new ToCsvService<DateTime>(objs) .Exclude(p => p.Minute) .ChangeName(p => p.Millisecond, "Milli") .Format(p => p.Date, "d") .ToCsv(); Per Jon Skeet's

Magic strings in ASP.NET MVC

*爱你&永不变心* 提交于 2019-12-04 03:07:47
I have a background in desktop software development and am getting started with learning ASP.NET MVC. In my default HomeController I have the Index action which has code that looks like this: if (!Request.IsAuthenticated) return RedirectToAction("Login", "Account"); In other words, redirect the user to "/account/login". The AccountController.Login action will then handle the user and send him back to the HomeController once he logs in successfully. This code smells to me, perhaps just because I'm accustomed to doing things differently in desktop software. What if I change the name of the Login

Getting rid of hardcoded strings in JavaScript with ASP.NET MVC

寵の児 提交于 2019-12-01 11:01:34
We have a few problems in a project I am working on, where we have a lot of JavaScript files, where we have hardcoded URLs to controller actions. Hardcoded URLs are proned to mistyping Hardcoded URLs in JavaScript scripts will cause a breakage if the related controller or action's name is changed Tools like ReSharper (to my knowledge) can't statically analyse it's way to saying the an action is not used, if the URLs pointing to it are hardcoded. Question(s) How can we avoid using hardcoded URLs in JavaScript ? - are there any existing frameworks out there that could solve this problem ? Look

Compile Time Reflection in C#

萝らか妹 提交于 2019-11-28 18:20:35
I frequently write C# code that has to use magic strings to express property names. Everyone knows the problems with magic strings. They are very difficult to refactor, they have no compile time checking, and often they lead to hard-to-diagnose issues. Yet C#/.NET uses them all over the place to represent property/class/method names. This issue has persisted for years and years, and the only viable solution currently is to use an expression tree which is then parsed at run-time for the property name. This gets you satisfactory compile-time checking, but it complicates the code (requiring

Compile Time Reflection in C#

泪湿孤枕 提交于 2019-11-27 10:54:02
问题 I frequently write C# code that has to use magic strings to express property names. Everyone knows the problems with magic strings. They are very difficult to refactor, they have no compile time checking, and often they lead to hard-to-diagnose issues. Yet C#/.NET uses them all over the place to represent property/class/method names. This issue has persisted for years and years, and the only viable solution currently is to use an expression tree which is then parsed at run-time for the