Skip to content

Instantly share code, notes, and snippets.

@wi7a1ian
wi7a1ian / DataFlowExamples.cs
Created October 7, 2019 10:09
Usage of three basic DataFlow types in C# #csharp
static void Main(string[] args)
{
var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromSeconds(10));
TestProducerConsumerViaBufferBlock(cts.Token);
TestProducerConsumerViaActionBlock(cts.Token);
TestParallelTransformViaTransformBlock(cts.Token);
}
@wi7a1ian
wi7a1ian / IMessageHub.cs
Last active September 25, 2019 10:05
Implementation of Event Aggregator Pattern #csharp c#
public interface IMessageHub : IDisposable
{
void RegisterGlobalHandler(Action<Type, object> onMessage);
void RegisterGlobalErrorHandler(Action<Guid, Exception> onError);
void Publish<T>(T message);
Guid Subscribe<T>(Action<T> action);
void Unsubscribe(Guid token);
bool IsSubscribed(Guid token);
void ClearSubscriptions();
}
@wi7a1ian
wi7a1ian / EventCommandExecuter.cs
Created September 24, 2019 20:49
InvokeCommandAction vs EventToCommand #wpf #csharp
public class EventCommandExecuter : TriggerAction<DependencyObject>
{
public EventCommandExecuter()
: this(CultureInfo.CurrentCulture)
{
}
public EventCommandExecuter(CultureInfo culture)
{
Culture = culture;
@wi7a1ian
wi7a1ian / Entity.cs
Last active September 12, 2019 10:27
Example of generic repo with basic CRUD that translates persistent entity model to business model in c# #csharp
public class Entity
{
public long Id { get; set; }
}
@wi7a1ian
wi7a1ian / GetAllRowsAsync.cs
Last active September 2, 2019 14:02
Generic async method for reading all db rows in c# #csharp
protected async Task<IEnumerable<T>> GetAllRowsAsync<T>(DbConnection conn, string query, Func<DbDataReader, Task<T>> asyncMapper, CancellationToken token)
where T : class
{
IList<T> result = new List<T>();
using (conn)
using (var cmd = conn.CreateCommand())
{
await conn.OpenAsync(token);
cmd.CommandText = query;

In a multithreaded environment, the lack of understanding and the resulting problems are greatly amplified, almost to the point of panic if you are paying attention. Programming in a functional style makes the state presented to your code explicit, which makes it much easier to reason about, and, in a completely pure system, makes thread race conditions impossible. - John Carmack

OO makes code understandable by encapsulating moving parts. FP makes code understandable by minimizing moving parts. - Michael Feathers

Characteristics of functional programming (from dev perspective)

First-class functions are typical for functional programming languages, that means, they behave like data. There have to be functions that can accept functions as an argument or return 'em. Therefore, they are called higher-order functions. Pure functions always return the same result when given the same arguments and can not have a side effect. They are the reason that Haskell is called a pure functional languag

@wi7a1ian
wi7a1ian / Usage.cs
Last active August 1, 2019 14:05
Example of fine interoop between .NET Core & C compatible ABI #csharp #cpp #rust
public Writer BeginWrite()
{
c_write_begin(out var writerHandle);
return new Writer(writerHandle);
}
@wi7a1ian
wi7a1ian / HellWithPLINQ.cs
Last active July 25, 2019 10:37
PLINQ having different behaviour than LINQ when trying to obtain first element of filtered collection #csharp
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int ctrS = 0, ctrP = 0, ctrWP = 0;
Enumerable.Range(0, 1_000_000).Where( x => { ++ctrS; return x == 0; }).First();
@wi7a1ian
wi7a1ian / MapSQLiteRowIntoGenericTypeViaName.cs
Last active July 19, 2019 09:24
Map data row from db reader into generic type #csharp
public void MapRowTo<T>(SQLiteDataReader reader, ref T obj)
{
string propertyName;
for (int columnIdx = 0; columnIdx < reader.FieldCount; columnIdx++)
{
propertyName = reader.GetName(columnIdx);
var prop = typeof(T).GetRuntimeProperty(propertyName);
if (prop == null)
@wi7a1ian
wi7a1ian / ReadExpandoObjectFromSQLite.cs
Last active July 19, 2019 09:16
Dynamic data read from db using ADO.NET for SQLite in c# #csharp #sqlite
public IList<ExpandoObject> ReadFromSQLite(string databasePath, string query)
{
var results = new List<ExpandoObject>();
using (var connection = new SQLiteConnection($"Data Source={databasePath}"))
{
connection.Open();
using (SQLiteCommand cmd = connection.CreateCommand())
{