.NET tanker & tips

.Net, jQuery og andre nørdede emner

Is Blazor the new Angular killer?

May 20
by steffen 20. May 2019 14:44

As a part of my attendance at the SDD conference in London, I have decided to write small blog posts summing up some of the impressions from the talks I attend. The format will be more rough since the posts has been written during the day and finished in the evening at the hotel.

This post has been written as a short summary of Dino Esposito's pre-conference workshop at https://sddconf.com/

Microsoft new toy

At the Build conference in May 2019, Microsoft announced that the experimental Blazor project, would be picked up as a fully supported product. Version 1.0 is expected to launch together with .NET Core 3.0 in September 2019. 

Blazor is a brand new way of developing web applications, where the new WebAssembly standard is used to run C# code directly in the browser. This means that there is no need for Javascript og Typescript, since the WebAssembly sandbox has the same rights as Javascript and therefore can interact with the DOM and all the other stuff that can be done from the browser. The neat trick is that you can do everything in C#.

The current version is 0.9 and has some features, but there is missing some vital implementations e.g. authentication. The current version does show a little bit about some of the thoughts the Microsoft team has about the upcoming product. 

In the broad perspective there are two different flavours of Blazor: client only Blazor and server side Blazor.

Server side Blazor

The server side Blazor approach is expected to be shipped together with .NET Core 3.0, while it is unknown when client side Blazor will be shipped.

The server side approach is built using SignalR. When the server is hit SignalR will create a WebSocket connection from the browser to the server and this channel will be used to transfer the rendered HTML from the server to the client throughout the session. 

This approach solves the issue with the huge initial download in the client side approach and also looks a lot more like a vanilla flavour ASP.NET MVC Core application, making this approach very comfortable for developers who already knows this. 

The downside is that the server will have to maintain many concurrent WebSockets and you have to handle disconnect scenarios gracefully. However this issue is the same across most applications using the WebSocket technology.

Client only Blazor

The client only approach is running on a version of Mono that can interpret C# in the WebAssembly sandbox. When the server is hit with the first request, all the pages from the website and all the assemblies that is needed to run those pages are sent back to the client. The downside here is that the initial load is quite long pending your download speed. The upside side - which is pretty cool - is that for all subsequent actions the user makes on the page, the content is loaded instantly without hitting the server (unless you explicitly ask to hit the server), because everything has been loaded upfront. 

Depending on your scenario this could be a very attractive tradeoff. It actually makes you web application work a lot like an app, since all the code needed for running the application has been downloaded to the computer/device. 

The obvious downside is that if you have many pages and/or use many different assemblies the initial download will bloat very fast and will degrade the user experience.

Angular killer?

The new Blazor framework has a lot going for it. It is directly developed by Microsoft, it cuts away the need for having a codebase with multiple languages (e.g. C#, javascript, Typscript), removing the need for thrid party frontend frameworks lige Angular and React.

In the current form I do not believe that it will kill neither Angular, React or Vue - it is simply not good enought. But it will definitely be a competitor, the question is how big a competitor it will be. A lot depends on how the 1.0 version will look like. The version that I have seen demoed today in May 2019 is not ready to take on the mature Angular, React and Vue frameworks. 

The server side Blazor seems like a good idea. I like the fact that all code is in C#, which makes it easy to maintain, it's possible to unit test everything and the conceptual model is easy to understand.

The client only Blazor confuses me a little bit more. I find it har to figure out what Microsoft are trying to achieve with this approach. It could make sense to use as a substitute for native app's - but since it is running in the browser it will lack all the cool bells and whistles that native apps have. The fact that you have to download all the pages and dependencies to run it in the client does not sound like a sound idea to me. 

What will the final verdict be? We'll know when Microsoft launces version 1.0.


Tags: , ,

c# | Blazor

Don't let Tony Stark down - use enums!

November 04
by steffen 4. November 2018 16:56

Photo credit Marvel Studio

As a software developer you have many tools in our toolbox, that help create powerful and useful software applications. One of the most important concepts in software development is the principle of simplicity aka. the KISS principle. The more simple your code is, the easier it is to understand and to change, when a change request comes (and it will come sooner or later).


One of the most simple concepts that we have is the concept of flags. A flag is a marker that enable the program to recognize different program states and to decide how to proceed in the program flow. The most simple flag is the boolean, which has two states; true or false. But often the state of the program has more than two states and for this scenario the Enum solves the need perfectly.


An example of the use of enum, could be the software that defines which type of drivetrain a car has. A normal car is either front wheel drive, rear wheel drive or four wheel drive. This can be clearly represented with the following enum.


enum DriveTrain







The drivetrain of a car can be clearly stated with the use of the DriveTrain enum.


This is all good and well, but as it turns out you work for Stark Industries and mr Stark has come up with a brand new design for a car, which can direct power to all four wheels independently. At any given time, depending on the situation, any combination of the four wheels can have power. It could be only the left frontwheel, the two right wheel or all four wheels. This requirement could be solved by adding a enum value for each of the wheel combinations, however this is not the most elegant solution.


Instead it is possible to use a neat little feature in C#, where it is possible to use the bitwise operators. First we will change our enum to reflect each wheel instead.



enum DriveTrain


    LeftFrontWheel = 1,

    RightFrontWheel = 2,

    LeftRearWheel = 4,

    RightRearWheel = 8



Let's start with looking at what we can do with this and we'll look into the nitty gritty stuff afterwards.


With this new enum we could do something like this:

DriveTrain wheelsWithPower = DriveTrain.LeftFrontWheel | DriveTrain.RightRearWheel;


A call to wheelsWithPower.ToString() returns "LeftFrontWheel, RightRearWheel" to indicate which of the enum values are set in the variable wheelsWithPower. If we have the need to query if a certain flag is set in the variabel we can do this with


bool powerToLeftRear = wheelsWithPower.HasFlag(DriveTrain.LeftRearWheel);


Very simple and concise!


Now let's dig in to some of the details. The most important thing to notice is the fact, that each enum value has a designated value (1, 2, 4 and 8). These number must be a power of 2. If this is not done the code will not work as expected. This has to do with the implementation of the HasFlags, which is a bitwise AND operation and hence each value must have it's own bit.


The second thing to notice is the [Flags] attribute. This is not needed for the HasFlags to work. This is only for getting nicer feedback while debugging. If the attribute was not added to the enum, a call to ToString() like above would return 9 instead of "LeftFrontWheel, RightRearWheel". Again this has to do with the underlying implementation, which rely on bit wize logic. The HasFlags works regardless of the Flags attribute.


Now mr. Stark can have his car, which alternates between powering each wheel independently and can go kick some bad guy ass.


c# | General