2sxc 16.07 is LTS with the new Typed mode. This is one of the most important updates ever. Time to use Razor for precision shaving...
2sxc 16.07 is LTS - meaning Long-Term-Stable / Long-Term-Support. We are already using it in production, and so can you. The big features that are finally stable are the Typed coding mode, which will revolutionize how you work and probably increase productivity by a factor of 2!
- Read more about our LTS policy
- Get your 2sxc from the github releases
Typed Mode - The Big Deal
This code will result in an error, which looks like this:
As you can see, the compiler is unhappy about line 14. But the developer can now waste countless hours trying to figure out what to do about this - when in reality, the problem started on line 9. Here's why:
- The object Content is dynamic - so Razor can't determine at runtime, what it will be.
- Because of this, the code AsAdam(...) will also be treated as dynamic - meaning that the variable folder will be dynamic - even though AsAdam(...) will always return an IFolder.
- but the compiler cannot assume that, so it treats folder and folder.Files as dynamic
- As a result, the code compiles as if everything is ok, even though a compiler could have spotted the problem much earlier. It will break at runtime, and usually in a randomly different place than expected.
The fix is be to help the compiler on line 9 - but this is really hard to figure out 😠:
And this is the equivalent, Typed version. If something is wrong, you'll know right away:
Recap: The Problems with Dynamic Razor
- The compiler can't help with bugs on dynamic variable, resulting in errors which are really hard to figure out
- When you do get an error, it's often reported far away from the original cause.
- dynamic variables are like a virus - any code that use them will be treated as dynamic
- the more code you have, the harder it gets to figure out what's going on
- together with LINQ (which uses Extension Methods) this get's really, really messy
- 2sxc has a bunch of magic to compensate for this - but if that didn't suffice, the problem often got even harder to find
Solution: Going Typed and Cleaning up History
The new typed mode is active when inheriting the new base class RazorTyped (or ApiTyped / CodeTyped). With this change, we also renamed a bunch of variables which have also caused confusions. For example:
- No more "Content". The item which belongs to the the current Razor is called MyItem. If we get a list, it's MyItems. The Header is now MyHeader.
- No more "Settings". It's either AllSettings or App.Settings.
- No more "Resources". It's AllResources or App.Resources.
- No more "Data". It's either MyData or App.Data.
- There a lot more My* - like MyContext, MyModel, MyPage, MyView and more.
- No more "AsDynamic(...)". This super-magical method did so many things that the code looked nice, but was impossible to debug. It's now AsItem(...), AsItems(...) or Kit.Json.ToTyped(string).
There's a lot more to this - best check out the docs.
We spent more than a 4 full weeks on the tutorial app, so that you'll be able to see the code in both old and new from now on.
▶️ Check out the latest tutorial app now 🚀 - go.2sxc.org/tut
▶️ especially the new Quick Reference 💡 - go.2sxc.org/quickref
Get it now!
💝 from Switzerland and Croatia,
iJungleboy, Tonci, Duje