Skip to content

Instantly share code, notes, and snippets.

@jonocairns
jonocairns / c#-integration-client-rest-sharp
Last active August 29, 2015 14:15
Example integration client
public class IntegrationClient : IIntegrationClient
{
private readonly ISettings _settings;
public IntegrationClient(ISettings settings)
{
if (settings == null) throw new ArgumentNullException("settings");
_settings = settings;
}
public async Task<T> ExecuteGetRequest<T>(RestRequest request)
{
@jonocairns
jonocairns / redis-try-get-set
Last active August 29, 2015 14:15
Cache wrapper for redis which will try get a value from the cache - if it doesn't exist it will perform the method you supply and set that as the cache value
//cache
public async Task<T> TryGet<T>(string key, Func<Task<T>> ifCacheMissAction)
{
if (ifCacheMissAction == null) throw new ArgumentNullException("ifCacheMissAction");
if (string.IsNullOrEmpty(key)) throw new ArgumentNullException("key");
if (_isEnabled)
{
RedisValue redisValue = Cache.StringGet(key);
// the following is a bit yolo
if (redisValue.HasValue && redisValue != "{}")
@jonocairns
jonocairns / angular-ts-controller
Last active August 29, 2015 14:20
template for an angular controller - written in typescript
'use strict';
module app.common {
export interface IExampleController {
doSomething(value: string): void;
}
class ExampleController {
// properties here
public exampleProp: boolean;
'use strict';
module app.services {
export interface IExampleService {
get<T>(): T
}
export class ExampleService implements IExampleService {
constructor(private $http: ng.IHttpService) {
}
'use strict';
module app.common {
export class Guid {
public static newGuid(): string {
return this.generateGuid();
}
@jonocairns
jonocairns / angular-exception-handler
Created April 30, 2015 20:58
Relies on a logging service to be created which points to an API which can log to something like raygun
'use strict';
module app.exception {
angular.module('app.exception')
.config(['$provide', ($provide: ng.auto.IProvideService) => {
$provide.decorator('$exceptionHandler', ['$delegate', 'app.services.LoggingService', '$injector',
($delegate: ng.IExceptionHandlerService, logger: app.services.ILoggingService, $injector: any) => (exception: any) => {
var errorMessage = '';
if (!_.isUndefined(exception) && !_.isNull(exception)) {
'use strict';
module app.common {
export function spinner(): ng.IDirective {
return {
restrict: 'E',
replace: true,
templateUrl: 'views/templates/spinner.html' // this template can contain an spinner SVG
};
}
'use strict';
module app.common {
export function stopEvent(): ng.IDirective {
return {
restrict: 'A',
link: (scope: any, element: any) => {
element.bind('click', (event: any) => {
// other ng-click handlers shouldn't be triggered
event.stopPropagation(event);
});
'use strict';
module app.services {
export interface ILoggingService {
logError(errorMessage: string): void;
tryClearErrors(): void;
}
export class LoggingService implements ILoggingService {
constructor(private $injector: any, private connectivityService: app.services.IConectivityService) {
public static class ListExtensions
{
public static IEnumerable<IEnumerable<T>> Chunk<T>(this IEnumerable<T> source, int chunkSize)
{
Argument.CheckIfNull(source, "source");
Argument.CheckIfNull(chunkSize, "chunkSize");
if (chunkSize <= 0)
{
throw new ArgumentException("chunkSize should be positive", "chunkSize");