Posts

Showing posts from May, 2025

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, ...

ASP.NET Core Pitfalls - Action Constraint Order

Introduction When we have more than one action method in MVP or Web API that can match a given request, the request may fail or land on an unwanted method. By default, a number of items is used to figure out which method to call: The HTTP verb The action method name The route template The action method parameters The request content type An  action constaint  may be needed to select the right one; an action constraint is an implementation of IActionConstraint , and is normally added through an attribute or a convention. Some examples of built-in action constrains include: [Consumes] attribute: for selecting the method based on the Content-Type request header HttpMethodActionConstraint : legacy, don't bother OverloadActionConstraint : the method selected must match all required parameters If you want to build your own, you should inherit from  ActionMethodSelectorAttribute  and implement  IsValidForRequest . Problem Sometimes, however, just applying an acti...

A Simple State Machine in .NET - Adding Code-based Implementation

Introduction On a previous post , I introduced my SimpleStateMachine project. Essentially, it provided an easy way to define a state machine from enumeration values. The version presented used attributes on these enumeration values to build up the state machine. Because this can sometimes be intrusive, and we may not want to "pollute" our enumerations, I decided to implement another way to define a state machine from an enumeration. State Machines from Code So, I came up with this interface: public interface IStateMachine<T> where T : Enum { T GetInitialState(); IStateMachine<T> CanTransitionTo(T state, params IReadOnlyCollection<T> otherStates); IEnumerable<T> GetTransitions(T state); bool IsFinalState(T state); bool IsInitialState(T state); } It provides basically the same operations that were previously available: Get the initial state of an enumeration ( GetInitialState ) Check if an enumeration value is the initial ( IsInitial...

PhD

As some of you may know, I was recently accepted to the PhD programme in Informatics Engineering of the Informatics Engineering Department of the University of Coimbra ! I was very happy about it, as there weren't that many vacancies and I don't have an academic/scientific background - essentially, I'm a software developer -, and it was something I long wanted to do. I still don't have a subject or advisor(s), this is something that I need to address in the next few months, before classes start, in September. So, I guess this is to say that I may be writing some posts about it, as I go along; all will have the #phd tag. And that's it! ;-)