Skip to content

Instantly share code, notes, and snippets.

View fractos's full-sized avatar
🌑
we are wolves we are not loved

Adam Christie fractos

🌑
we are wolves we are not loved
View GitHub Profile
@fractos
fractos / MongoDBUserStore.cs
Created April 30, 2015 21:22
MongoDBUserStore - Put (many)
public void Put(IEnumerable<User> users)
{
AssertIsStarted();
Task.Run(async () =>
await _collection.InsertManyAsync(
users.Select(x => x.ConvertUserToBson())))
.Wait();
}
@fractos
fractos / MongoDBUserStore.cs
Created April 30, 2015 21:21
MongoDBUserStore - Put
public void Put(User user)
{
AssertIsStarted();
Task.Run(async () =>
await _collection.ReplaceOneAsync(
new BsonDocument("_id", user.ID),
user.ConvertUserToBson()))
.Wait();
}
@fractos
fractos / MongoDBUserStore.cs
Created April 30, 2015 21:21
MongoDBUserStore - GetAll
public IEnumerable GetAll()
{
AssertIsStarted();
return
_collection.FindAsync(new BsonDocument())
.Result.ToListAsync()
.Result.Select(doc => doc.ConvertBsonToUser());
}
@fractos
fractos / MongoDBUserStore.cs
Last active August 29, 2015 14:20
MongoDBUserStore - Get
public User Get(string username)
{
AssertIsStarted();
return _collection
.Find(new BsonDocument("username", username))
.FirstAsync().Result.ConvertBsonToUser();
}
@fractos
fractos / MongoDBUserStore.cs
Created April 30, 2015 21:19
MongoDBUserStore - Start
public override void Start()
{
base.Start();
_collection = this.Database.GetCollection(_collectionName);
}
@fractos
fractos / MongoDBUserStore.cs
Created April 30, 2015 21:19
Top part of the MongoDBUserStore
private readonly string _collectionName;
private IMongoCollection _collection;
public MongoDBUserStore(string connStr, string dbName, string collectionName) : base(connStr, dbName)
{
_collectionName = collectionName;
}
@fractos
fractos / IUserStore.cs
Last active August 29, 2015 14:20
The IUserStore interface
using System.Collections.Generic;
using Inversion.Data;
using Harness.Example.Model;
namespace Harness.Example.Store
{
public interface IUserStore : IStore
{
@fractos
fractos / MongoDBUserEx.cs
Created April 30, 2015 21:16
Extension methods for BsonDocument conversion
using System.Linq;
using MongoDB.Bson;
using Harness.Example.Model;
namespace Harness.Example.Store
{
public static class MongoDBUserEx
{
public static BsonDocument ConvertUserToBson(this User user)
@fractos
fractos / User.cs
Created April 30, 2015 21:13
The new User.cs
using System;
using System.Xml;
using Newtonsoft.Json.Linq;
using Inversion;
namespace Harness.Example.Model
{
public class User : IData
{
@fractos
fractos / MongoDBStore.cs
Last active August 29, 2015 14:20
The MongoDBStore base implementation.
using MongoDB.Bson;
using MongoDB.Driver;
namespace Inversion.Data
{
public class MongoDBStore : StoreBase
{
protected IMongoDatabase Database;
private MongoClient _client;