Inside StorageLists

When I began writing the back end for After, I started with Entity Framework/SQL Server.  I wanted to use the opportunity to teach myself the Code-First approach, as I’ve previously only done Database-First.

I really enjoyed working with Code-First, and it was a worthwhile investment of time.  However, I soon found that  SQL Server isn’t well-equipped to handle multiple persistent connections that need to share a static context.  (Please refrain from saying, “Duh, I could have told you that!”  :D)

Instead of searching for something else, I decided to get creative and write my own.  I already had my data modeled in such a way that I could reference everything by ID/PK, and I liked how Code-First used the DbContext class and DbSets.  So I put those concepts into StorageLists.

The StorageList is basically a List<> that writes items to disk after a set period of time of not being accessed.  This timeout can be adjusted, but the default is 3 minutes.  Also, the item must pass the PersistenceFilter, if specified, or else is held in memory and never written to disk.  Items written to disk are serialized to JSON.

Your models for each StorageList must implement IStorageItem, which adds the StorageID property and LastAccessed.

Here’s the example use that’s in the readme:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using StorageLists;

namespace MyProject
    public class World
        // Static data context.
        public static World Current { get; set; } = new World();
        public World()
            // Set the folder storage location for each StorageList.
            var server = HttpContext.Current.Server;
            Locations.FolderPath = server.MapPath("~/App_Data/World_Data/Locations");
            People.FolderPath = server.MapPath("~/App_Data/World_Data/People");
            // Only locations with a population greater than 5000 will be written to disk.
            Locations.PersistenceFilter = new Predicate<Location>(loc => loc.Population > 5000);
        public StorageList<Location> Locations { get; set; } = new StorageList<Location>();
        public StorageList<Person> People { get; set; } = new StorageList<Person>();

Add comment