using System.Collections.Generic;
public class Episode : IModel
public int Id { get; set; }
public class Location : IModel
public int Id { get; set; }
public class Character : IModel
public int Id { get; set; }
public static class DataAccessLayer
public static Episode GetEpisode(int id)
return new Episode(){ Id = id };
public static Location GetLocation(int id)
return new Location(){ Id = id };
public static Character GetCharacter(int id)
return new Character(){ Id = id };
public static Episode[] GetEpisodes()
return new []{ GetEpisode(1), GetEpisode(2), GetEpisode(3) };
public static Location[] GetLocations()
return new []{ GetLocation(1), GetLocation(2), GetLocation(3) };
public static Character[] GetCharacters()
return new []{ GetCharacter(1), GetCharacter(2), GetCharacter(3) };
public Dictionary<int, IModel> dictionary = new Dictionary<int, IModel>();
public IModel[] list = {};
public static class Cache
public delegate IModel GetOneDelegate(int id);
static Dictionary<CacheType, GetOneDelegate> getOne = new Dictionary<CacheType, GetOneDelegate>()
{ CacheType.Episode, DataAccessLayer.GetEpisode },
{ CacheType.Location, DataAccessLayer.GetLocation },
{ CacheType.Character, DataAccessLayer.GetCharacter }
public delegate IModel[] GetAllDelegate();
static Dictionary<CacheType, GetAllDelegate> getAll = new Dictionary<CacheType, GetAllDelegate>()
{ CacheType.Episode, DataAccessLayer.GetEpisodes },
{ CacheType.Location, DataAccessLayer.GetLocations },
{ CacheType.Character, DataAccessLayer.GetCharacters }
static Dictionary<CacheType, CacheData> cache = new Dictionary<CacheType, CacheData>()
{ CacheType.Episode, new CacheData() },
{ CacheType.Location, new CacheData() },
{ CacheType.Character, new CacheData() }
public static T GetOne<T>(CacheType type, int id) where T : IModel
var dictionary = cache[type].dictionary;
if (dictionary.TryGetValue(id, out IModel model))
IModel newModel = getOne[type](id);
dictionary.Add(newModel.Id, newModel);
public static IModel[] GetAll<T>(CacheType type) where T : IModel
var list = cache[type].list;
var newList = getAll[type]();
public static void Main()
Console.WriteLine("Hello World");
var episode = Cache.GetOne<Episode>(CacheType.Episode, 1);
var location = Cache.GetOne<Location>(CacheType.Location, 1);
var character = Cache.GetOne<Character>(CacheType.Character, 1);
var episode2 = Cache.GetOne<Episode>(CacheType.Episode, 1);
Console.WriteLine(episode.Id);
Console.WriteLine(location);
Console.WriteLine(character);
Console.WriteLine(episode2.Id);
var locations = Cache.GetAll<Location>(CacheType.Location);
foreach (var loc in locations)
Console.WriteLine(loc.Id);