Skip to content

Instantly share code, notes, and snippets.

View forcewake's full-sized avatar

Pavel Nasovich forcewake

View GitHub Profile
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RLUtils
{
/// <summary>
/// Creates an Comparer for a type
/// </summary>
@forcewake
forcewake / EnumerableExtensions.cs
Created March 7, 2014 06:30
Sorting expression dynamically using LINQ
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
public static class EnumerableExtensions
{
public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> collection,
string columnName, SortDirection direction = SortDirection.Ascending)
{
ParameterExpression param = Expression.Parameter(typeof(T), "x"); // x
@forcewake
forcewake / EnumerableExtensions.cs
Created March 20, 2014 13:28
Permutations extension
public static class EnumerableExtensions
{
public static IEnumerable<IEnumerable<T>> Permutations<T>(this IEnumerable<T> source)
{
if (source == null)
throw new ArgumentNullException("source");
// Ensure that the source IEnumerable is evaluated only once
return permutations(source.ToArray());
}
public interface IFactory<in TKey, TValue>
{
/// <summary>
/// Registers an item
/// </summary>
/// <param name="key">Item to register</param>
/// <param name="value">The object to be returned</param>
void Register(TKey key, TValue value);
/// <summary>
using System;
using System.Linq.Expressions;
using System.Reflection;
namespace Expression.Runner
{
/// <summary>
/// Extensions to <see cref="LambdaExpression"/>
/// </summary>
public static class ExpressionExtensions
public class TransactionProvider : ITransactionProvider<SqlTransaction>
{
private const string TransactionField = "StoreTransaction";
private const BindingFlags BindingFlags = System.Reflection.BindingFlags.FlattenHierarchy | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.InvokeMethod
| System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.NonPublic;
public SqlTransaction GetTransaction()
{
var sqlTransaction = (SqlTransaction)this.transaction.GetType()
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
namespace Test.Lib
{
/// <summary>
@forcewake
forcewake / NullSafeStringComparer.cs
Created May 5, 2014 18:13
Safe compare null-string
public class NullSafeStringComparer : StringComparer
{
private readonly StringComparer baseStringComparer;
public static NullSafeStringComparer NullSafeInvariantCulture
{
get
{
return new NullSafeStringComparer(InvariantCulture);
}
namespace Framework.Jobs.Arguments
{
using System;
using System.Linq.Expressions;
public static class StaticReflection
{
public static string GetMemberName<T>(
this T instance,
Expression<Func<T, object>> expression)

NPoco/PetaPoco stored procedures with named strong type parameters

StoredProcedures.tt file automatically generates a C# code file with calsses and methods corresponding to your database stored procedure definitions. This is then used to simply call stored procedures within NPoco/PetaPoco and avoid magic strings and parameter type guessing. It also supports output parameters.

Stored procedures naming conventions

In order to have your stored procedure calls well structured there are particular yet simple naming conventions you should follow when creating your stored procedures:

  1. Name your stored procedures as ClassName_Method
  2. If a particular stored procedure shouldn't be parsed you should omit underscore character in its name; this will make it private from the perspective of your C# as it will only be accessible to other stored procedures but won't be parsed.