InMemory DataSource
Transient in-memory data storage with optional persistence for testing, prototyping, and runtime-only operations
Source file -
DataManagementEngineStandard/InMemory/InMemoryDataSource.cs (450 lines)
Table of Contents
Overview
InMemoryDataSource stores data in memory without external database dependencies, implementing both IDataSource and IInMemoryDB interfaces.
Use Cases
- Unit testing — No external database setup needed
- Prototyping — Rapid development without schema migrations
- Runtime caches — Temporary data for workflows and lookups
- Development — Local dev without persistent storage overhead
- ETL staging — Intermediate data before transformation
Characteristics
| Feature | Behavior |
|---|---|
| Persistence | Optional — saves to JSON file if configured |
| Schema | Dynamic EntityStructure definitions |
| CRUD | Via IDataSource methods (throwing stubs by default) |
| Storage path | %ProgramData%/TheTechIdea/Beep/InMemory/{name}/ |
| File formats | createscripts.json (schema), InMemoryStructures.json |
Class Definition
public class InMemoryDataSource : IDataSource, IInMemoryDB, IDisposable
{
public InMemoryDataSource(
string datasourceName,
IDMEEditor dmeEditor,
IDMLogger logger,
DataSourceType databaseType,
IErrorsInfo errorObject);
// State flags
public bool IsStructureCreated { get; set; }
public bool IsCreated { get; set; }
public bool IsLoaded { get; set; }
public bool IsSaved { get; set; }
public bool IsSynced { get; set; }
// Paths (static)
public static string BeepDataPath { get; }
public static string InMemoryPath { get; }
public static string Filepath { get; }
public static string InMemoryStructuresfilepath { get; }
// IDataSource implementation
public string DatasourceName { get; set; }
public DataSourceType DatasourceType { get; set; }
public DatasourceCategory Category { get; set; } = DatasourceCategory.INMEMORY;
public ConnectionState ConnectionStatus { get; set; }
public IDMEEditor DMEEditor { get; set; }
public IErrorsInfo ErrorObject { get; set; }
public IDMLogger Logger { get; set; }
public List<string> EntitiesNames { get; set; }
public List<EntityStructure> Entities { get; set; }
public ETLScriptHDR CreateScript { get; set; }
public List<EntityStructure> InMemoryStructures { get; set; }
}
Properties
| Property | Type | Description |
|---|---|---|
IsStructureCreated | bool | Schema has been created (can be false even if folder exists) |
IsCreated | bool | Database file/directory created on disk |
IsLoaded | bool | Data loaded into memory from file |
IsSaved | bool | Current state saved to file |
IsSynced | bool | Data synchronized with source |
CreateScript | ETLScriptHDR | DDL script for entity creation, saved with copy scripts |
InMemoryStructures | List<EntityStructure> | Persisted entity definitions (backup of Entities) |
DatasourceType | DataSourceType | Set by constructor (DataSourceType.NONE by default) |
Category | DatasourceCategory | Always DatasourceCategory.INMEMORY |
ConnectionStatus | ConnectionState | Closed → Open on OpenDatabaseInMemory() |
Lifecycle
- Construction — Calls
Createfolder(), sets up directory structure at%ProgramData%/TheTechIdea/Beep/InMemory/{name}/ - Open —
Openconnection()callsOpenDatabaseInMemory() - Load —
LoadStructure()readscreatescripts.jsonandInMemoryStructures.json - Create —
CreateStructure()runs ETL create script - Load data —
LoadData()runs ETL import script - Save —
SaveStructure()writes entities to JSON files - Close —
Closeconnection()sets ConnectionStatus to Closed
var props = new ConnectionProperties
{
ConnectionName = "TempData",
DatabaseType = DataSourceType.InMemory,
Category = DatasourceCategory.INMEMORY
};
editor.ConfigEditor.AddDataConnection(props);
var ds = editor.GetDataSource("TempData");
ds.Openconnection(); // Calls OpenDatabaseInMemory → ConnectionState.Open
Structure Management
// Load from persisted JSON files
var result = ds.LoadStructure(progress, token);
// Reads crateescripts.json → CreateScript
// Reads InMemoryStructures.json → InMemoryStructures
// OnLoadStructure event fires after successful load
// Create entities from script
var result = ds.CreateStructure(progress, token);
// Runs DMEEditor.ETL.RunCreateScript() if structure not yet created
// Marks IsStructureCreated = true
// Save current state to files
var result = ds.SaveStructure();
// Copies Entities → InMemoryStructures
// Generates create scripts via DMEEditor.ETL.GetCreateEntityScript()
// Generates copy data scripts via DMEEditor.ETL.GetCopyDataEntityScript()
// Serializes to createscripts.json and InMemoryStructures.json
// OnSaveStructure event fires
File Persistence
InMemoryDataSource has optional persistence via JSON files:
| File | Content | Purpose |
|---|---|---|
createscripts.json | ETLScriptHDR | DDL scripts for entity creation with copy data scripts |
InMemoryStructures.json | List<EntityStructure> | Persisted entity definitions |
Storage path is created automatically by the Createfolder() method:
// Path hierarchy:
// %ProgramData%/TheTechIdea/Beep/InMemory/{datasourceName}/
// ├── createscripts.json
// └── InMemoryStructures.json
// The folder is created during construction:
private void Createfolder(string datasourcename)
{
string basePath = Environment.GetFolderPath(
Environment.SpecialFolder.CommonApplicationData);
// Creates: {basePath}/TheTechIdea/Beep/InMemory/{name}/
Data Operations
// Load data from ETL import script
ds.CreateScript = importScript;
var result = ds.LoadData(progress, token);
// Runs DMEEditor.ETL.RunImportScript() if folder exists
// Marks IsLoaded = true
// Sync (placeholder, throws NotImplementedException in current version)
var result = ds.SyncData(progress, token);
// Refresh (placeholder)
var result = ds.RefreshData(progress, token);
// IDataSource CRUD (throwing stubs by default)
// These throw NotImplementedException unless overridden:
var entities = ds.GetEntitiesList();
var data = ds.GetEntity("Customers", new List<AppFilter>());
var result = ds.InsertEntity("Customers", data);
var result = ds.UpdateEntity("Customers", data);
var result = ds.DeleteEntity("Customers", data);
CRUD stubs
Core CRUD methods are throwing stubs. Concrete implementations override these in datasource-specific subclasses (e.g., SQLiteDataSource).
Events
| Event | Args | Fires When |
|---|---|---|
OnLoadData | PassedArgs | Data loaded from ETL script |
OnLoadStructure | PassedArgs | Structure loaded from JSON files |
OnSaveStructure | PassedArgs | Structure persisted to files |
OnSyncData | PassedArgs | Data synchronized |
OnCreateStructure | PassedArgs | Structure created via ETL |
OnRefreshData | PassedArgs | All data refreshed |
OnRefreshDataEntity | PassedArgs | Entity-specific refresh |
PassEvent | PassedArgs | General event relay (IDataSource) |
Constructors
public InMemoryDataSource(
string datasourceName, // Unique name
IDMEEditor dmeEditor, // Editor reference
IDMLogger logger, // Logger instance
DataSourceType databaseType,// e.g., DataSourceType.SqlLite
IErrorsInfo errorObject // Error tracking
)
// On construction:
// 1. Sets identity properties (DatasourceName, DatasourceType, etc.)
// 2. Calls Createfolder(datasourceName) to set up directory
// 3. Initializes empty Entities, EntitiesNames, CreateScript lists
Usage Examples
Basic Setup
var props = new ConnectionProperties
{
ConnectionName = "TestData",
DatabaseType = DataSourceType.InMemory,
Category = DatasourceCategory.INMEMORY
};
editor.ConfigEditor.AddDataConnection(props);
var ds = editor.GetDataSource("TestData");
ds.Openconnection();
// Define schema
ds.Entities.Add(new EntityStructure
{
EntityName = "TestTable",
Fields = new List<EntityField>
{
new EntityField { FieldName = "Id", FieldType = "int" },
new EntityField { FieldName = "Name", FieldType = "string" }
}
});
With Persistence
ds.Entities.Add(entityDef);
// Save structure to disk
ds.SaveStructure();
// Files created at:
// %ProgramData%/TheTechIdea/Beep/InMemory/TestData/
// createscripts.json
// InMemoryStructures.json
// Later: reload from disk
ds.Openconnection();
ds.LoadStructure(progress, token);
// Entities restored from InMemoryStructures.json
ETL Staging
// Create ETL script for schema
ds.CreateScript = editor.ETL.GetCreateEntityScript(entities);
ds.CreateStructure(progress, token);
// Create import script and load data
ds.CreateScript = importScript;
ds.LoadData(progress, token);
Unit Testing
[TestClass]
public class CustomerServiceTests
{
private IDMEEditor _editor;
private IDataSource _ds;
[TestInitialize]
public void Setup()
{
_editor = new DMEEditor();
var props = new ConnectionProperties
{
ConnectionName = "TestDB",
DatabaseType = DataSourceType.InMemory
};
_editor.ConfigEditor.AddDataConnection(props);
_ds = _editor.GetDataSource("TestDB");
_ds.Openconnection();
}
[TestMethod]
public void CanInsertCustomer()
{
var service = new CustomerService(_editor);
var result = service.AddCustomer(new Customer { Name = "John" });
Assert.IsTrue(result.Success);
}
[TestCleanup]
public void Cleanup()
{
_ds.Closeconnection();
}
}