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 / 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 / DbContext.cs
Last active September 2, 2019 20:10
simple mongodb database context implementation
public class DbContext : IDbContext
{
public DbContext(IRepositoryFactory repoFactory, string connectionString, string dbName)
{
if (string.IsNullOrWhiteSpace(connectionString))
throw new ArgumentNullException("connectionString");
if (string.IsNullOrWhiteSpace(dbName))
throw new ArgumentNullException("dbName");
this.Posts = repoFactory.Create<Entities.Video>(new RepositoryOptions(connectionString, dbName, "posts"));
@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 / 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 / Repository.cs
Last active July 17, 2020 12:14
simple mongodb repository implementation
public class Repository<TEntity> : IRepository<TEntity>
{
private readonly IMongoCollection<TEntity> _collection;
public Repository(IMongoCollection<TEntity> collection)
{
if (null == collection)
throw new ArgumentNullException("collection");
_collection = collection;
@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 / FakeFindFluent.cs
Created December 16, 2015 14:49
fake implementation of mongodb IFindFluent used for testing
public class FakeFindFluent<TEntity> : IFindFluent<TEntity, TEntity>
{
private readonly IEnumerable<TEntity> _items;
public FakeFindFluent(IEnumerable<TEntity> items)
{
_items = items ?? Enumerable.Empty<TEntity>();
}
public Task<TEntity> FirstOrDefaultAsync(CancellationToken cancellationToken)
@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 / 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';
};
var myClassInstaceDeserialized = JsonConvert.DeserializeObject<MyClass>(jsonData, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects
});