Posts

Showing posts with the label development

C# Magical Syntax

Introduction This post is an update to an old one on my  old blog  and it was inspired by my latest posts on C# versus Java . You may not have realized that some of the patterns that you’ve been using for ages in your C# programs are based on conventions, rather than on a specific API. What I mean by this is, some constructs in C# are based on some magical methods with well-defined names which are not defined in any base class or interface, but yet just work. Let’s see what they are. Enumerating Collections You are probably used to iterating through collections using the foreach statement. If you are, you may know that foreach actually wraps a call to a method called GetEnumerator , like the one that is defined by the IEnumerable and IEnumerable<T> interfaces. Thus, you might think, the magic occurs because the collection implements one or the two, but you would be wrong: it turns out that in order to iterate through a class using foreach all it takes is that the c...

What's New in .NET 9 and C# 13

Introduction .NET 9 and C# 13 have been released some days ago , so it's time for my own resume of the new features. There are tons of improvements, quite a few related to performance, so, beware, if you want the whole thing, you should read the official documentation , these are just my personal choices! Semi-Auto Properties C# 13 introduced a new (experimental as of now, you need to turn on the preview language features for the project) keyword called field , which can be used, in auto-properties , to access the auto-generated field.  To set the language version to preview , add this to your .csproj file: <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net9.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <LangVersion>preview</LangVersion> </PropertyGroup> And to make it work: public DateTime Date { get { return field; } ...

A Search Proxy in .NET

Introduction We all use Internet search. Nowadays, bookmarks have become somewhat outdated, as the modern search engines are so good at finding what we want, that they have become somewhat obsolete. But, what if we need to perform a search in code? We can obviously send an HTTP request to our search engine of choice and then parse the result, but it would be useful if there was a library that could do it for us. And this is how Search.NET was born! Concepts First, let's define a contract for a search proxy in .NET. A concrete proxy implementation will be specific to a search engine, such as Google , or Bing , but the contract will be the same. Here is what I propose, a contract named ISearch : public interface ISearch {     Task<SearchResult> Search(string query, CancellationToken cancellationToken = default); Task<SearchResult> Search(string query, QueryOptions options, CancellationToken cancellationToken = default); } As you can see, it has essentially o...

Domain Events with .NET

Introduction Domain Events  is a pattern and concept made popular by Domain Driven Design . A Domain Event is something that happened on your system and you want interested-parties to know about. It is a way to publish information in a decoupled, usually asynchronous, way. A publisher publishes (raises) an event, and interested subscribers receive a notification and act accordingly. A subscriber can also be a publisher, of course. In this post I'm going to present a Domain Events library for .NET. It's not the first time that I write about decoupling messages and subscribers in an application, I implemented a few years ago the Postal.NET library which does something similar. Mind you, there are many implementations out there, MediatR coming first to my mind, but this is mine. I obviously got some ideas from other implementations, but mine is substantially different from all the others. Concepts First, a few concepts: An event is just some class that implements IDomainEvent A...