Skip to content

Instantly share code, notes, and snippets.

View mizrael's full-sized avatar
😼
my cat owns the keyboard.

David Guida mizrael

😼
my cat owns the keyboard.
View GitHub Profile
@mizrael
mizrael / controller-promise.js
Created November 9, 2015 20:40
an AngularJs controller that uses a promise to subscribe to call a remote service and handle the results
var myApp = angular.module('myApp',[]);
myApp.controller('FooController', ['$scope', 'fooService', function($scope, fooService) {
var instance = this;
$scope.loadingStatus = 'none';
instance.onBarCompleted = function(){
$scope.loadingStatus = 'completed';
};
@mizrael
mizrael / controller-promises-unit-test-js
Last active November 9, 2015 21:23
how to test a controller that uses a promise to call a remote service
var mockFooService,
sut;
describe('FooController tests', function () {
beforeEach(function($q, $controller){
mockFooService = {
bar: function() {
return $q.reject({ data: { message: 'Error message' } });
}
@mizrael
mizrael / FakeAsyncCursor.cs
Created December 16, 2015 14:50
fake implementation of mongodb IAsyncCursor used for testing
public class FakeAsyncCursor<TEntity> : IAsyncCursor<TEntity>
{
private readonly IEnumerable<TEntity> _items;
public FakeAsyncCursor(IEnumerable<TEntity> items)
{
_items = items ?? Enumerable.Empty<TEntity>();
}
public IEnumerable<TEntity> Current
@mizrael
mizrael / IRepository.cs
Created December 16, 2015 22:23
basic C# interface for a MongoDB repository
public interface IRepository<TEntity>
{
string CollectionName { get; }
Task<long> CountAsync(FilterDefinition<TEntity> filter);
IFindFluent<TEntity, TEntity> Find(FilterDefinition<TEntity> filter);
IFindFluent<TEntity, TEntity> Find(Expression<Func<TEntity, bool>> filter);
Task<TEntity> FindOneAndReplaceAsync(FilterDefinition<TEntity> filter, TEntity replacement);
Task<TEntity> FindOneAndReplaceAsync(Expression<Func<TEntity, bool>> filter, TEntity replacement);
}
@mizrael
mizrael / IDbContext
Created December 19, 2015 10:22
basic C# interface for a MongoDB context
public interface IDbContext
{
IRepository<Entities.Post> Posts { get; }
IRepository<Entities.User> Users { get; }
IRepository<Entities.Tag> Tags { get; }
IRepository<Entities.Taxonomy> Taxonomies { get; }
}
@mizrael
mizrael / IRepositoryFactory.cs
Created December 29, 2015 09:57
basic C# interface for a MongoDB Repository Factory
public interface IRepositoryFactory
{
IRepository<TEntity> Create<TEntity>(RepositoryOptions options);
}
@mizrael
mizrael / IMongoDatabaseFactory.cs
Created December 29, 2015 10:07
basic C# interface for a MongoDB database Factory
public interface IMongoDatabaseFactory
{
IMongoDatabase Connect(string connectionString, string dbName);
}
@mizrael
mizrael / BaseCommandHandler.cs
Created March 3, 2016 15:19
base class for Command Handlers used to enforce command validation
public abstract class BaseCommandHandler<TCommand> : IAsyncNotificationHandler<TCommand>
{
private readonly IValidator<TCommand> _validator;
protected BaseCommandHandler(IValidator<TCommand> validator = null)
{
_validator = validator;
}
public async Task Handle(TCommand command)
@mizrael
mizrael / MongoDatabaseFactory.cs
Created December 29, 2015 10:49
simple Mongodb database factory implementation
public class MongoDatabaseFactory : IMongoDatabaseFactory
{
public IMongoDatabase Connect(string connectionString, string dbName)
{
if(string.IsNullOrWhiteSpace(connectionString))
throw new ArgumentNullException("connectionString");
if (string.IsNullOrWhiteSpace(dbName))
throw new ArgumentNullException("dbName");
var dbClient = new MongoClient(connectionString);
@mizrael
mizrael / wp_shortcode_redirect.php
Last active July 15, 2016 15:17
Wordpress: how to handle redirects in shortcode
class my_shortcode{
public function __construct()
{
add_shortcode('my_shortcode', array($this, 'render_form'));
add_action( 'template_redirect', array($this, 'handle_form_post') );
}
function render_form(){