SignalR for ASP.NET Core 2.0 with Aurelia and Webpack

Just today (September 15, 2017) Microsoft announced the first alpha version of SignalR for ASP.NET Core 2.0. Now these are great news! I immediately had to try this out with my favorite way of building web apps: The Aurelia framework. I can already say: It works like a charm. But let me start from the beginning.

TL;DR If you do not want to follow this tutorial and just want the code, go ahead, have a look at my Aurelia Playground over on GitHub!

Setting up a simple application

I used a skeleton I once created to have an easy starting point with Aurelia. Initially the skeleton was set up using the Aurelia template coming with the the ASP.NET Core SPA templates (dotnet new aurelia). I upgraded Bootstrap to the shiny new version 4 but that’s basically all I changed. Feel free to use it as a starting point for your projects!

For this project I am going to use this template as a baseline. Go ahead and clone https://github.com/Spontifixus/aurelia-playground.git to get started and read on to build your first ASP.NET Core 2.0 SignalR application!

weiterlesen...

Reflection ohne Reflection: Properties lesen und setzen per Databinding

Nehmen wir an, wir entwickeln ein UserControl, dass die DependencyProperty TextPath vom Typ string bereitstellt. Diese Property kann der Anwender im XAML-Code setzen um das Steuerelement zu veranlassen um den Wert einer bestimmten Property seines DataContexts auszulesen oder zu setzen. Das riecht danach, das Problem mit handelsüblicher Reflection zu lösen:

var propertyInfo = this.DataContext
    .GetType()
    .GetProperty(this.TextPath);
var result = propertyInfo.GetValue(this.DataContext, null);

Das klappte auch hervorragend. Bis einer der Anwender der Komponente auf die (absolut nachvollziehbare) Idee kam, den Namen der auszulesenden Property under Verwendung der Property Path Syntax anzugeben:

<MyControl TextPath="Address.City" />

Und dann stellt man plötzlich fest, dass man mit Reflection in diesem Fall nicht sehr weit kommt. Theoretisch wäre eine Lösung zwar möglich, aber nur, wenn man bereit ist einen Parser für die Property Path Syntax zu schreiben und noch mehr Reflection in den Ring zu schicken.

Es gibt allerdings eine Komponente in Silverlight und im .NET-Framework die diese Funktionalität schon fix und fertig liefert: Die Klasse System.Windows.Data.Binding. Das Prinzip ist einfach: Man bindet die Quelle (in diesem Fall den DataContext des Steuerelements) unter Verwendung des angegebenen Pfades an eine Property einer Helferklasse und lasse das Binding seine Arbeit tun. Wie das konkret vonstatten geht?

weiterlesen...

Wenn es watschelt und quakt: Ducktyping in C#

“When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.”
— James Whitcomb Riley

Oder auf gut Deutsch: “Wenn ich einen Vogel sehe, der wie eine Ente watschelt und wie eine Ente quakt, dann ist dieser Vogel für mich eine Ente.” Das selbe Prinzip das James Whitcomb Riley auf die Enten angewandt hat, kann man auch in der Software-Entwicklung anwenden. Zwei Fragen stellen sich dabei zwangsläufig. Ist das wirklich eine gute Idee? Und wenn ja, wie geht das?

weiterlesen...