Skip to content

Instantly share code, notes, and snippets.

View restlessmedia's full-sized avatar
🎯
Focusing

Phil Cooper restlessmedia

🎯
Focusing
View GitHub Profile
/// <summary>
/// Creates a connection adapter that cancels any <see cref="IDbCommand"/> created from it when the client disconnects from the response.
/// </summary>
private class ClientDisconnectedConnectionAdapter : IDbConnection
{
private readonly SqlConnection _connection;
public conn(SqlConnection sqlConnection)
{
_connection = sqlConnection ?? throw new ArgumentNullException(nameof(sqlConnection));
using ImageResizer;
using ImageResizer.Configuration;
using ImageResizer.Encoding;
using ImageResizer.Plugins;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace ImageResizer.CustomPlugins
public class DelegatingStream : Stream
{
Stream stream;
protected DelegatingStream(Stream stream)
{
if (stream == null)
throw new ArgumentNullException("stream");
this.stream = stream;
const createPaginator = (endpoint, resultKey) => {
const createRequestPageActionCreator = (endpoint, resultKey) => (page) => requestPage(endpoint, resultKey, page)
const requestPage = (page) => ({
type: 'REQUEST_PAGE',
payload: {
page
},
meta: {
public static class LinqExtensions
{
public static IQueryable<TSource> Page<TSource>(this IQueryable<TSource> source, IPageable pageable)
{
return Page<IQueryable<TSource>, TSource>(source, pageable);
}
public static IQueryable<TSource> Page<TSource>(this IQueryable<TSource> source, int page, int maxPerPage = 10)
{
return Page<IQueryable<TSource>, TSource>(source, page, maxPerPage);
@restlessmedia
restlessmedia / Example.cs
Created September 14, 2016 08:01
Entity Framwork GridReader for multiple result sets.
public DataModel GetDataModel(int id)
{
const string sql = "map.GetDataModel @id=@id";
using (DbContext context = new DbContext(_connectionString))
{
using (GridReader reader = new GridReader(context, sql, new SqlParameter("@id", id)))
{
DataModel model = reader.Read<DataModel>().FirstOrDefault();
model.NestedArray = reader.Read<Nested>().ToArray();
@restlessmedia
restlessmedia / CommandInterceptionOptions.cs
Last active September 7, 2016 15:18
Entity Framework DbCommandInterception for instrumentation.
public class CommandInterceptionOptions
{
public CommandInterceptionOptions()
{
WarningThreshold = TimeSpan.FromSeconds(2);
}
public TimeSpan WarningThreshold { get; set; }
public static CommandInterceptionOptions Default = new CommandInterceptionOptions();
@restlessmedia
restlessmedia / BindAsAttribute.cs
Last active August 24, 2016 07:55
.Net Mvc bind abstract properties
using System;
namespace MvcApplication1
{
public class BindAsAttribute : Attribute
{
public BindAsAttribute(Type modelType)
{
ModelType = modelType;
}
public abstract class AsyncTraceListener : TraceListener
{
public AsyncTraceListener()
{
_loggingThread = new Thread(new ThreadStart(ProcessQueue))
{
// this is performed from a bg thread, to ensure the queue is serviced from a single thread
IsBackground = true
};
_loggingThread.Start();
public class WebConfiguration
{
public WebConfiguration(string path = "~", bool autoSave = true, bool autoRefresh = true)
{
if (string.IsNullOrWhiteSpace(path))
throw new ArgumentNullException("path");
_autoSave = autoSave;
_autoRefresh = autoRefresh;
_configuration = Open(path);