Posts

Java vs C# - Part 3

Introduction Updated records, thanks, Tim Purdum , for your review! This is my third post on the syntax of Java versus that of C#. It's been very long since I wrote the first two, and a lot has changed in C# (and something in Java); I updated the original ones, which you can find  here  (will be moving to this blog shortly), and this one will cover what's new. It's going to be a bit longer, as there are lots of features to cover, especially coming from C#. Again, this is not about each frameworks' specific APIs or features, it's just about how the C# and the Java languages differ. Because I'm a .NET person, I will give examples in C# only. I'm going to talk about: Init-only properties Primary constructors Tuples Interface members and default interface methods Partial properties Async/await Expression body members Pattern matching Local functions Records Global imports Type aliases Implicitly-typed variables Top-level statements Conditional member access Null...

Simple State Machine Updates

Introduction Some of you may remember my SimpleStateMachine project; I blogged about it here and here . It is available on GitHub and Nuget . I made a few changes to it: An alternative way to configure states Adding state to states (pun intended) Converting attributes to code state machines The possibility to clear state transitions (unsure about this one, but, it's here, at least for now) Let's see how these work. Building a State Machine We used to have two ways for building a simple state machine: From attributes From code I'm going to introduce a new one, but, first, here is quick recap for the existing methods. From Attributes As simple as adding attributes to enumeration fields: public enum TicketState { [InitialState] [Transitions<TicketState>(TicketState.Ready, TicketState.Closed)] Created, [Transitions<TicketState>(TicketState.InProgress, TicketState.Blocked, TicketState.Closed)] Ready, [Transitions<TicketState>(TicketSt...

.NET Collections

Introduction Sometimes, the proper choice of a collection can greatly impact the performance of your application. For example, there are collection types that are more appropriate for insertions, others that allow faster lookups, and so on. Plus, you must decide if you want indexed collections or not, and if you want to have generic collections, in order to have compile-time type checking. The .NET BCL comes with several general-purpose collection classes. Here is a list of all the collection classes as of .NET 9 and their intended use. Namespaces Namespace Purpose System.Collections Basic types, non-generic collections and interfaces System.Collections.Generic Generic interfaces and collections System.Collections.ObjectModel Observable and read-only collections System.Collections.Specialized Specialised collections System.Collections.Concurrent Concurrent (thread-safe) collections ( System.Collections.Concurrent Nuget package ) System.Collections.Immutable Immutable collections ( Syst...

Introducing RazorSharpener

Image
Introduction Sharpener: a person or device that makes something sharp  (Cambridge Dictionary). RazorSharpener  is a Razor compiler and renderer. It can load a .razor file and compile it into a .NET class that implements IComponent , the base interface for Razor components . This can be used, for example, to build a template engine, as Razor allows us to combine contents and code. Usage RazorSharpener  is composed of just two classes: RazorCompiler : used to compile a .razor source file ( Razor component ) into a .NET class RazorRenderer : renders a  Razor component  type into a string Razor Compiler To compile a .razor file is very simple: var compiler = new RazorCompiler(); var asm = compiler.Compile("RenderMessage.razor"); As you can see, the Compile method takes a file from the current folder and produces an assembly, which is how .NET "wraps" generated code. If there is any compilation error, an exception is thrown. The generated assembly will...

Value Generators in EF Core

Introduction EF Core has the concept of value generators. Very simply, they provide values for properties, either the primary key or another one. Wasn't too long ago that this feature - configurable value generators - was missing from EF Core, but now we have it, and it works well. We still don't have all of the id generators offered by NHibernate , but, we have something, and, what's best, we can build our own! Strategies versus Value Generators Before value generators were around, we could specify a generation or update strategy for properties, like the id. Strategies were a hint for the database provider, they didn't exactly specify how the value would be generated. The strategies are: None : the value is never generated Identity : the value is generated only when the record is first inserted Computed : the value is generated when the row is either inserted or modified Usually,  Identity  meant that something like auto-increment or SQL Server IDENTITY will be used, ...