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 / iippp.md
Last active February 9, 2017 03:31
The Inane Insistence on Patterns Pertaining to Persistance

The Inane Insistence on Patterns Pertaining to Persistance

An annoying pattern

I have been noticing an annoying pattern lately. Whenever I hear about a new project starting, more often than not, I see someone on the team jump into "DB architect" mode. For those individuals, literally every last ounce of energy goes into designing the database. Worse, they want sole ownership and literally forego all other development activities until they are satisfied.

It has been a long time since I've seen a requirements document that was thorough enough or accurate enough that I could devise a perfect database on day one of the project. I have two guesses why some developers are drawn to early database design: 1) it gives them a sense of control over the project, thinking it establishes them in some essential role; or 2) they have been burned too many times in the past with bad database designs.

My experience is that quality software projects grow organically. When you start a new project in Visual Studio, you're n

@jehugaleahsa
jehugaleahsa / ATTS.md
Last active February 2, 2017 22:03
A Transition to Statelessness

A Transition to Statelessness

Introduction

In 2012, I took a psychedelic journey into the world of functional programming. I started learning some ML-based languages, including F#, OCAML and Haskell. I also discovered Scala, which is now one of my all-time favorite languages.

I think too many people stop learning functional programming before they get into the really interesting parts. Filter/map/reduce is just the tip of the iceberg. Even if that is all you end up learning, you should at a minimum take to heart Command Query Separation. But the real meat of functional programming comes in the form of closures, partial application, currying, high-order functions and immutability. Check out this C#:

Func<int, int> getAdder(int value)
{
 return i =&gt; i + value;
@jehugaleahsa
jehugaleahsa / EventIdLayoutRenderer.cs
Last active January 26, 2017 16:42
Custom NLog Json formatter driven by Exception.Data
[LayoutRenderer("event-id-json-data")]
public class EventIdLayoutRenderer : LayoutRenderer
{
private const string _eventIdKey = "EventId";
private static JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
};
@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 / 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 / arm.scala
Created October 5, 2016 17:25
Scala ARM
import scala.language.reflectiveCalls
def using[TResource <: { def close(): Unit }, TResult](resource: => TResource)(action: TResource => TResult): TResult = {
val instance = resource
try {
action(instance)
} finally {
instance.close()
}
}
@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 / ConnectionLifetimeManager.cs
Created August 24, 2016 14:21
Connection Lifetime Manager
public class ConnectionLifetimeManager : IDisposable
{
private readonly IDbConnection connection;
private readonly bool needsClosed;
public ConnectionLifetimeManager(IDbConnection connection)
{
this.connection = connection;
if (connection.State == ConnectionState.Closed)
{
@jehugaleahsa
jehugaleahsa / SqlBulkCopier.cs
Created August 24, 2016 14:20
SqlBulkCopy Wrapper
public class SqlBulkCopier<T>
{
private readonly string tableName;
private readonly Dictionary<PropertyInfo, PropertyMapping> mappings;
public SqlBulkCopier(string tableName)
{
if (String.IsNullOrWhiteSpace(tableName))
{
throw new ArgumentNullException("tableName");