Skip to content

Instantly share code, notes, and snippets.

@malloy045
Last active April 3, 2020 12:24
Show Gist options
  • Save malloy045/10812b8f7f488a9a1e39c8602d461b1e to your computer and use it in GitHub Desktop.
Save malloy045/10812b8f7f488a9a1e39c8602d461b1e to your computer and use it in GitHub Desktop.
Interesting Gists - C#, Angular, Dart/Flutter

Overview

This is a list of interesting code snippets that I've created when dabbling in C#, Angular, and most recently Dart/Flutter.

C#

  • Sempahore - The same user data was "refreshed" when mobile users used a certain feature in our mobile app AND when administrators used a certain feature on their dashboard. This caused a race condition which resulted in duplicate entries in our database. This was one approach to stop the bleeding.
  • MVC and API routing (one web project) - A requirement of this project was to only use one web project. It was part of a suite containing a mobile app and an angular administrative portal. I was on the fence with this decision - since separating the Angular front end (hosted via the MVC controllers) and the API would lend itself nicely to scaling the API if mobile usage increased. But this was part of a monolith and the overhead of multiple web projects in the CI/CD pipeline outweighed the preemptive separation of these things. This is an example of how I achieved flexible routing for both the MVC and API controllers.
  • API route that serves images - In this snippet we are retrieving and serving images that were stored in a SQL database. It demonstrates the flexibility of our APIs and how they don't just always have to serve json or xml data.
  • Swagger/Swashbuckle - Demonstrates how to generate Swagger/OpenAPI documentation from C# xml contents including customizations to account for a non-traditional REST API design. These customizations include swagger filters and a custom C# attribute to describe custom header parameters.
  • Autofac Modules - Autofac DI/IoC examples
  • Indexer - Indexer I used as a simple caching construct. There are libraries that do this better, but I thought this was worth saving.
  • Async Task Logging - An example of using ContinueWith to log the execution time of individual async tasks that are executed using Task.WhenAll.

SQL

  • Geography/spatial data query - Demonstrates how to use SQL's geography spatial data type to compare sets of coordinates. The ultimate goal was to find the closest known zip code based on a set of coordinates reported from a mobile app.

Angular

  • Auth guard and interceptor (auto-refresh tokens) - I was tasked with securing an Angular front end using an oAuth2-like experience (access and refresh tokens) as well as implementing role-based route protection. Refreshing the tokens without dropping pending HTTP requests or impacting UX was important. This was achieved by using an Angular HTTP interceptor. It intercepts all HTTP requests that the application makes. It has logic to detect if the current request is to our app's API and validates/refreshes the tokens automatically without impacting the original request. It also has a blocking mechanism to halt all subsequent requests if a token refresh request is currently happening (so these subsequent requests all get the same set of fresh tokens).
  • API Routing toggle - An easy way to toggle your API calls between a real API or mocked data files (like when using ng serve).

Dart/Flutter

  • Build Script - This is still new territory for me, but this seems to work well. This script builds (or creates if one doesn't exist) a flutter project from scratch. Includes options to choose a platform and clean severity (flutter -> clobber).
  • Install Script - intended for automated/consistent installation across multiple machines/build agents.
  • Http transaction - simple example showing how we make and consume an Http request. Includes a json-to-dart model de-serialization approach and some utility methods that made reading the http response stream easier. UPDATE: this is a little obsolete now, we found a better way to parse json responses, but I enjoyed writing this utility
  • Dealing with non-production self-signed certs - I imagine most organizations have self-signed certs floating around on non-production boxes. I happen to have to deal with a lot of them. I see a lot of chatter on the web telling people to trust all certs all the time - but that is horrible. I have an HttpOverride that contains a whitelist of "ignorable" host names that ONLY is used in non-release builds.
  • Dependency Injection example in bloc/repo layers - This was an example used to illustrate to a junior team member how to abstract our bloc and repository code layers to achieve DI.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment