Author: andersmalmgren

Convention based Concurrency Management in Entity Framework Core

Who does not love convention over configuration? Whenever it makes sense I try to use it in my role as a system architect. It helps my programmers write more robust code out of the box.

Writing concurrency safe code is a corner stone in writing robust code today,  without it data quality can not be guaranteed. And when things go wrong you want to know who and when entities were updated so you can investigate what have gone wrong.

So what I did at my latest assignment was to force this onto the entities by convention using two markup interfaces ICreated and IUpdated(more…)

Using regex in Visual Studio

This is as much a note to self as a blog post :p

One developer in my team had done null == somevariable everywhere in our solution. I think unified code styles are important. So I wanted to remove them. Standard replace does not work here since we need to move the null after the variable name. So we need to use a regex.

Regex

We search for null and then any character as few as possible until we hit a space or a ending parenthesis. We then replace with group 1 ($1) which is the variable name == null and then group 2 ($2) which is either a space or a ending parenthesis.

This ensures above replace works with both if(null == somevar) and code like if(null == somevar && someotherstuff)

Update:
Here is a version on steroids that can do both equals and not equals
Regex v2

A proper thread safe memory cache

The Core 2.2 IMemoryCache is in theory thread safe. But if you call GetOrCreateAsync from multiple threads the factory Func will be called multiple times. Which could be a bad thing. A very simple fix to this is using a semaphore.

Declare it and only let one concurrent request be granted.

private readonly SemaphoreSlim _cacheLock = new SemaphoreSlim(1);

Let one request the cache and when done release the semaphore.

await _cacheLock.WaitAsync();
var data = await _cache.GetOrCreateAsync(key, entry => ...);
_cacheLock.Release();

Method color in Visual Studio 2019

I didn’t like the new color on methods in VS 2019. I tried to find which color I should change. I didn’t find one but you can revert to the 2017 style by disabling Tools > Options > Text Editor > C# > Advanced > Use enhanced colors for C# and Basic.

Sadly this reverts all of the 2019 color enhancements back to 2017. If any one know which color controls the method only please drop me a line.

Stub User.Identity.IsAuthenticated in ASP Core

I’m writing this article strictly because google do not have any obvious solutions in the hope it will be indexed and presented for fellow devs.

We use identity server and to make things easier in dev I want to stub it. ClaimsIdentity takes a second argument AuthenticationType. Its important you set this property. You can set it to what ever you like. Once set IsAuthenticated will return true.

            if (env.IsDevelopment())
            {
                app.Use(async (ctx, next) =>
                {
                   ctx.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, "local") }, "Authenticated"));
                   await next.Invoke();
                });
            }

Visitor pattern navigation support with ReSharper

I love the visitor pattern, it enables open/closed principle which is a great fundamental part of maintainability and clean code. You can read more about the pattern here. There is one down side of this pattern, and that is navigation. Consider this code.

_cqsClient.ExecuteCommand(new MyCommand());

If you navigate to ExecuteCommand you will just end up at some close to the metal code that executes your command handlers. And if you try to find all usages for the Command you will only find usages of its constructor (becasuse of the new keyword).

With vanilla ReSharper you need to first navigate to the class and then do a find all usages on the class declaration and navigate to the command handler from there. Very counter productive. But ReSharper is extendable!
(more…)

Flexible integration tests with dacpac support

Integration tests are an important aspect of software development, high code coverage does improve code quality. But the tests need to be flexible and fast so they do not hinder the developers in their daily work. On the build server speed doesn’t matter that much, but a good test suite must be fast enough so that the developers choose to use it instead of running the system manually to test their features. Thats how you get good code coverage. Sadly publishing a dacpac is anything but fast. But there are clever tactics you can apply to make it work good as your daily testing platform.
(more…)

Build server – client contracts with dotnet CLI

Now days type safety is common within the web-world with TypeScript and on the horizon WebAssembly with .NET Core (Blazor etc). I have for a long time advocated for the importance of this, especially when we are talking the contract between server and client. For example this T4 template that spits out C# CQS types as javascript.

T4 doesn’t play well with Dot Net Core, but we now have the dotnet CLI toolset we can use instead.
(more…)

Client – server event aggregation with React

I implemented my client/server event aggregation library in Vue yesterday and thought it was time for React. I must say React is alot different from Vue, and since I come from Knockout Vue was very easy to adopt. Though after som fiddling I got the hang of it. 😀

(more…)

Client – server event aggregation with Vue

I have ment to look into Vue for a long time. Finally I took my time and fell in love right away. The simplicity and modularity is just great. I also saw how easy you can extend it so I decided to implement a plugin for my SignalR event aggregation library.

(more…)