Navigation Menu

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 / MainPage.xaml
Created May 5, 2018 16:24
TreeView template to use an item template
<Style x:Key="TreeViewStyle1" TargetType="TreeView">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TreeView">
<TreeViewList x:Name="ListControl" AllowDrop="False"
CanReorderItems="False"
CanDragItems="False"
ItemContainerStyle="{StaticResource TreeViewItemStyle}"
ItemTemplate="{StaticResource CultureItemDataTemplate}">
@christiannagel
christiannagel / CulturesViewModel.cs
Created May 5, 2018 15:44
CulturesViewModel - initialize a list of cultures to display in a tree-view
public class CulturesViewModel : INotifyPropertyChanged
{
public CulturesViewModel() => SetupCultures();
// ... INotifyPropertyChanged Implementation
private void SetupCultures()
{
var cultureDataDict = CultureInfo.GetCultures(CultureTypes.AllCultures)
.OrderBy(c => c.Name)
@christiannagel
christiannagel / _CookieConsentPartial.cshtml
Created May 24, 2018 10:26
Cookie Consent View Implementation - ASP.NET Core 2.1
@using Microsoft.AspNetCore.Http.Features
@{
var consentFeature = Context.Features.Get<ITrackingConsentFeature>();
var showBanner = !consentFeature?.CanTrack ?? false;
var cookieString = consentFeature?.CreateConsentCookie();
}
@if (showBanner)
{
@christiannagel
christiannagel / UseClientHandlerSample.cs
Created June 8, 2018 08:02
Add a HttpClientHandler to a HTTP Client Factory configuration
private ServiceProvider ConfigureServices()
{
var services = new ServiceCollection();
services.AddLogging(builder =>
{
builder.AddFilter((category, level) => true);
builder.AddConsole(options => options.IncludeScopes = true);
});
services.AddHttpClient("cni", client =>
{
@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 / 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 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)