Skip to content

Instantly share code, notes, and snippets.

View christiannagel's full-sized avatar
💭
working on the new book Pragmatic Microservices with C# and Azure

Christian Nagel christiannagel

💭
working on the new book Pragmatic Microservices with C# and Azure
View GitHub Profile
@christiannagel
christiannagel / Books.cshtml
Created July 3, 2018 11:58
Blazor Books component accessing an Azure Function returning Book objects
@page "/books"
@using BooksLib
@inject HttpClient Http
<h1>Books Sample</h1>
<p>This component demonstrates fetching data from Azure Functions.</p>
<p>Status: @message</p>
@christiannagel
christiannagel / BooksFunction.cs
Created July 2, 2018 21:14
Azure Function returning a list of Book objects
public static class BooksFunction
{
[FunctionName("BooksFunction")]
public static IActionResult Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequest req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
return new OkObjectResult(GetBooks());
}
@christiannagel
christiannagel / Book.cs
Created July 2, 2018 21:07
Book class in a shared .NET Standard Library
public class Book
{
public Book() { }
public Book(int bookId, string title, string publisher)
=> (BookId, Title, Publisher) = (bookId, title, publisher);
public int BookId { get; set; }
public string Title { get; set; }
public string Publisher { get; set; }
@christiannagel
christiannagel / Program.cs
Last active June 19, 2018 15:55
C# 8: extended pattern matching with the switch expression - property pattern, recursive pattern
static string M3(Shape shape)
=> shape switch
{
CombinedShape (var shape1, var (pos, _)) => $"combined shape - shape1: {shape1.Name}, pos of shape2: {pos}",
{ Size: (200, 200), Position: var pos } => $"shape with size 200x200 at position {pos.x}:{pos.y}",
Ellipse (var pos, var size) => $"Ellipse with size {size} at position {pos}",
Rectangle (_, var size) => $"Rectangle with size {size}",
_ => "another shape"
};
@christiannagel
christiannagel / Program.cs
Created June 19, 2018 15:34
C# 8 switch expression
static string M2(Shape shape)
=> shape switch
{
Shape s when s.Size.height > 100 => $"large shape with size {s.Size} at position {s.Position}",
Ellipse e => $"Ellipse with size {e.Size} at position {e.Position}",
Rectangle r => $"Rectangle with size {r.Size} at position {r.Position}",
_ => "another shape"
};
@christiannagel
christiannagel / Program.cs
Created June 19, 2018 11:40
Sample Main method for pattern matching
static void Main()
{
var r1 = new Rectangle(position: (200, 200), size: (200, 200));
var e1 = new Ellipse(position: (80, 1400), size: (80, 140));
var shapes = new Shape[]
{
r1,
e1,
new Circle((40, 60), 90),
new CombinedShape(r1, e1)
@christiannagel
christiannagel / Shape.cs
Created June 19, 2018 11:27
Shape class used by pattern matching samples
public abstract class Shape
{
public (int x, int y) Position { get; }
public (int height, int width) Size { get; }
public Shape((int x, int y) position, (int height, int width) size)
=> (Position, Size) = (position, size);
public void Deconstruct(out (int x, int y) position, out (int x, int y) size)
=> (position, size) = (Position, Size);
@christiannagel
christiannagel / Program.cs
Created June 19, 2018 11:23
C# 7 pattern matching with the switch statement and type patterns with a filter
static string M1(Shape shape)
{
switch (shape)
{
case Shape s when s.Size.height > 100:
return $"large shape with size {s.Size} at position {s.Position}";
case Ellipse e:
return $"Ellipse with size {e.Size} at position {e.Position}";
case Rectangle r:
return $"Rectangle with size {r.Size} at position {r.Position}";
@christiannagel
christiannagel / Program.cs
Created June 15, 2018 06:06
Using a C# 8 library from a C# 7 application
var glory = new NewAndGlory();
string s1 = glory.GetANullString();
string s2 = glory.GetAString();
string s3 = glory.PassAString(null); // having a NullReferenceException here!
@christiannagel
christiannagel / Program.cs
Created June 15, 2018 05:51
Assigning nullable to non-nullable with C# 8
public string GetIsbn2(Book book)
=> book.Isbn ?? string.Empty;