Strongly Typed Code in 2sxc 17+
Strongly Typed Code is the newest way to write C# and Razor code in 2sxc 17+. It extends the Typed Code. As such, it's fully compatible, but ever more type-safe and easier to code and debug.
What does Strongly Typed Code Look Like?
@inherits AppCode.Razor.ProductRazor
<h1>@MyProduct.Title</h1>
@MyProduct.Picture("Screenshot", imgClass: "float-right")
<ul>
<li>Teaser: @MyProduct.Teaser</li>
<li>Launched: @MyProduct.Launched.Year</li>
<li>Authors: @MyProduct.Authors.Count()</li>
<li>Maker: @MyProduct.Maker.Name</li>
</ul>
@MyItem.Html("Description")
This combines the best of all worlds:
- Simple syntax, easy to read - just like the dynamic code
- Fully typed, so you get IntelliSense and the compiler will catch many more issues - even better than the typed code
- Great dev experience in VS Code - you will see if a field like
Authors
exists or not - Great performance, as it's all compiled at runtime
- Various APIs help you bridge the gap, such as the
As<Product>()
method - The auto-generated Content-Types are really smart - eg.
Authors
is aList<Author>
andMaker
is a singleCompany
The Parts that Make it Work
- The new Copilot generates classes for each Content-Type in the
AppCode.Data
namespace (you can easily extend them) - The
As<T>
andAsList<T>
methods help you convert data into strongly typed objects - HotBuild automatically compiles the code on-the-fly, so you can just save and refresh the page to see changes
- Everything in the
/AppCode
folder - or in editions such as/staging/AppCode
will be compiled and provided to your razor - New base classes such as
RazorTyped<TModel>
make data-handover to sub-components easy - New
Customize
APIs help you create new Razor base classes and hide much of the complexity from your Razor templates
TODO: Sample Apps
As of now (2024-03) we have not published any Apps with this convention yet, as we're still fine-tuning the details. But we will, ASAP!
Activate Strongly Typed Mode
Old Dynamic, new Typed and brand-new Strongly-Typed code can coexist in the same app. Each Razor / C# file can decide which mode it wants to use. To be in typed mode, your Razor/C# must inherit from a typed base class like this:
- You must use at least 2sxc 17.03 or later
- Razor files should begin with any one of these:
@inherits Custom.Hybrid.RazorTyped
@inherits Custom.Hybrid.RazorTyped<TModel>
@inherits AppCode.Razor.AppRazor
- assuming you have made a custom base class@inherits AppCode.Razor.AppRazor<TModel>
- assuming you have made a custom base class
- C# files should have something like
public class YourClassName : Custom.Hybrid.CodeTyped
public class YourClassName : AppCode.Services.ServiceBase
- WebApi files should be like
public class YourControllerName : Custom.Hybrid.ApiTyped
- You should generate your data classes using the Copilot in the Admin UI
Tip
Changing the base class will completely change the APIs you have available if you're coming from dynamic code. If you already used typed code, then this is already done.
Configure Visual Studio Code for IntelliSense
Now that everything is typed, we highly recommend you setup VSCode to provide IntelliSense.
👉 Check out the VS Code Setup Docs