Skip to content

Instantly share code, notes, and snippets.

@trevorhreed
Last active August 29, 2015 14:17
Show Gist options
  • Save trevorhreed/e3dd03eb9421edd809f7 to your computer and use it in GitHub Desktop.
Save trevorhreed/e3dd03eb9421edd809f7 to your computer and use it in GitHub Desktop.
/*
Author: Trevor
Date: 2015.03.27
A mock database class. All models should inherit from mdb.MockModel
mdb.Get<T>(Guid id)
- Returns the model with an id matching 'id' from the mock database
mdb.All<T>()
- Returns all models of type 'T' from the mock database
mdb.Put(T model)
- Saves (creates or updates) 'model' to the mock database
mdb.Del(T model)
- Deletes 'model' from the mock database
mdb.Del(Guid id)
- Deletes the model with 'id' from the mock database
model.put()
- Saves (creates or updates) 'model' to the mock database (alias for mdb.Put)
model.del()
- Deletes 'model' object from the mock database (alias for mdb.Del)
*/
using System;
using System.Collections.Generic;
using System.Linq;
namespace Byutv2.Areas.Admin.Helpers
{
public class mdb
{
public class MockModel
{
public Guid Id { get; set; }
public void put()
{
mdb.Put(this);
}
public void del()
{
mdb.Del(this);
}
}
public static Dictionary<Guid, MockModel> _models = new Dictionary<Guid, MockModel>();
public static T Get<T>(Guid id) where T : MockModel
{
if (!_models.ContainsKey(id)) return null;
return (T)_models[id];
}
public static List<T> All<T>() where T : MockModel
{
return _models.Where(entry => entry.Value.GetType() == typeof(T)).Cast<T>().ToList();
}
public static void Put<T>(T model) where T : MockModel
{
if (model == null) return;
if (_models.ContainsKey(model.Id))
{
_models[model.Id] = model;
}
else
{
_models.Add(model.Id, model);
}
}
public static void Del(MockModel model)
{
Del(model.Id);
}
public static void Del(Guid id)
{
_models.Remove(id);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment