Skip to content

Instantly share code, notes, and snippets.

View jehugaleahsa's full-sized avatar

Travis Parks jehugaleahsa

View GitHub Profile
@jehugaleahsa
jehugaleahsa / LTT.md
Last active February 15, 2024 14:53
LINQ Tricks and Techniques

Learn to Love LINQ

I've been using LINQ for years and I've come to deeply respect it as a technology. Unfortunately it has not been adopted by the .NET community as much as I would have expected. I honestly believe it is the single one feature that gives C# a competitive edge over many of the other general-purpose languages out there today. This document will go over some of the tricks I have learned to make the most of LINQ's powerful features. Hopefully you'll learn at least one trick along the way.

Query vs Method syntax

Yup, you can write your queries as a series of method calls or you can use the query syntax. Method syntax is fine when you're first starting out, but query syntax is more expressive in the long run. My experience has been that operations such as where, select, join, group by and order by should be expressed using query syntax and "reduce" operations should be done in method syntax. Unavoidably, you sometimes have to use method calls inside of your queries simply because th

@jehugaleahsa
jehugaleahsa / web-circles.md
Last active August 30, 2023 18:37
Razor and TypeScript

Once more around the bend

We're just going in circles...

If you have been a web developer for a while, you probably feel like you're going in circles. Going all the way back to CGI scripts, you were generating HTML using string literals, formatting and concatenation:

const message = "Hello, World!!!"
console.log("<html><body>" + message + "</body></html>");

Then someone decided it would be easier to inline code into HTML documents directly, using a more declarative approach:

@jehugaleahsa
jehugaleahsa / join.ps1
Last active July 7, 2023 19:56
PowerShell Script to Split Large Files
function join($path)
{
$files = Get-ChildItem -Path "$path.*.part" | Sort-Object -Property @{Expression={
$shortName = [System.IO.Path]::GetFileNameWithoutExtension($_.Name)
$extension = [System.IO.Path]::GetExtension($shortName)
if ($extension -ne $null -and $extension -ne '')
{
$extension = $extension.Substring(1)
}
[System.Convert]::ToInt32($extension)
@jehugaleahsa
jehugaleahsa / MEH.md
Last active January 1, 2023 18:48
Minimal Exception Handling

Minimal Exception Handling

Introduction

With so many other aspects of daily development completely unattainable by your average developer, exception handling is often ignore in lieu of bigger fish to fry. On the surface, exception handling appears sufficiently complicated enough that most developers have simply resolved to ignore it on a day-to-day basis or altogether. Only during the last weeks of development do they tack on a top-level try/catch block to avoid complete system failure or to log that "some" error occurred. Rather than a simple error dialog appearing to users, they are greeted with the "yellow screen of death". Even when the developer does manage to display an error dialog, the message is often vague or cryptic, e.g., "An error occurred".

We can do better than this. I believe the issue is that not a lot of focus has been given to providing simple, bare-minimum exception handling. For the majority of applications, exception handling only needs to satisfy the following requirements:

@jehugaleahsa
jehugaleahsa / loc.ps1
Created August 18, 2017 17:27
A simple PowerShell script to count the lines of code in a solution
$path = "C:\path\to\solution\folder"
$files = Get-ChildItem -Path $path -Include *.cs,*.ts,*.vb -Recurse `
| ? { $_.FullName -notmatch ".*\\obj\\.*" } `
| ? { $_.FullName -notmatch ".*\\node_modules\.*" }
$shortNames = $files | % {
$fullName = $_.FullName
$lines = [System.IO.File]::ReadAllLines($fullName)
$lines = $lines | ? { -not [System.String]::IsNullOrWhiteSpace($_) }
@jehugaleahsa
jehugaleahsa / ng-signalr.md
Last active March 28, 2022 14:49
Consume SignalR Using Angular 2+

Consume SignalR Using Angular 2+

The beautiful thing about the web was that this article is outdated before I even started writing it. I am going to show how I was able to encapsulate SignalR functionality behind a simple service that works nicely in an Angular 2+ environment. I find myself frequently ruminating about the fact that Angular 2+ is more of an "environment" than a framework. It's not just a handful of libraries strewn together - it literally drives your development process - pretty much forcing you to introduce Node.js, TypeScript and a build tool (eg., Webpack) into your daily activities. It also strongly reinforces how you organize your files and what you name them. It's so painfully opinionated and I love it!

Services

If you are working on an Angular 2+ application and you don't have a lot of services, you are doing something woefully wrong. One of the biggest parts of getting your head wrapped around Angular 2+ is familiarizing yourself with their new approach to dependency injection an

@jehugaleahsa
jehugaleahsa / preql.txt
Last active March 17, 2021 13:36
Query Language Examples
# Literals, such as booleans, integers, floats, and character strings are actually vectors of length 1.
# booleans:
true
# or
false
# integers (can have arbitrary _ as separators):
1, 45, 1_000_000
# floats
@jehugaleahsa
jehugaleahsa / models.md
Last active October 22, 2020 18:59
Good model design

Good model design

If you implement modern applications, you are probably familiar with the concept of view models and API models. Just to be clear, these are dumb classes normally made up of simple getters and setters (aka., properties) useful for passing data around. When working with REST services, these classes are usually serialized to JSON. You usually don't want them to have a whole lot of functionality since it won't get serialized. Instead, these are denormalized objects that save the UI or other clients from performing business logic.

About 5 years ago, I was working on a project where I had to quickly define a new set of models for the REST API. On a previous project, I had run into a lot of problems with deeply nested models - in other words, models containing other models. On this new project, I let that bad experience influence my decision making. To not nest my models, I flattened everything into one giant, denormalized model. The team immediately ran into issues, though, because we would hav

@jehugaleahsa
jehugaleahsa / MAC.md
Created January 24, 2017 01:09
Modern Architecture Concepts

Modern Architectural Concepts

Core Concepts

Dependency Injection

At the core of most modern software architecture is dependency injection. Rather than creating dependencies on-the-fly, all dependencies are injected into a class upon construction. This allows representations to be swapped out at runtime, making it easier to enable features based on a configuration file. This also enables the isolation of code, making it easier to test.

Dependency injection also puts a burden on the developer, making it clear when a class has too many dependencies and, therefore, responsibilities. This often acts as an early warning system that there is a design flaw. If taken seriously, DI can lead to smaller, more cohesive classes that are easier to test.

Below, you’ll find a section on dependency injection concepts that make modern architectures possible.

Onion Architecture

@jehugaleahsa
jehugaleahsa / join.cs
Created October 9, 2018 14:29
Join with And
public static string Join<TItem>(IEnumerable<TItem> items, string separator = ", ", string end = " and ")
{
var builder = new StringBuilder();
using (var enumerator = items.GetEnumerator())
{
if (enumerator.MoveNext())
{
var item = enumerator.Current;
if (enumerator.MoveNext())
{