Path: redb/redb.Core/Models/Entities/RedbList.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Models/Entities/RedbList.cs
----
using redb.Core.Models.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
namespace redb.Core.Models.Entities
{
///
/// REDB list entity with direct data storage (Aggregate Root).
/// Maps to _lists table in PostgreSQL.
///
public class RedbList : IRedbList
{
///
/// Unique list identifier.
///
[JsonPropertyName("id")]
public long Id { get; set; }
///
/// List name.
///
[JsonPropertyName("name")]
public string Name { get; set; } = string.Empty;
///
/// List alias (short name).
///
[JsonPropertyName("alias")]
public string? Alias { get; set; }
// === Items collection (Aggregate Root) ===
private List _items = new();
///
/// Collection of list items (read-only).
///
[JsonIgnore]
public IReadOnlyCollection Items => _items.AsReadOnly();
///
/// Internal items list (for provider).
///
internal List ItemsInternal => _items;
///
/// Set items collection (for mapping).
///
public void SetItems(IEnumerable items)
{
_items = items?.ToList() ?? new List();
}
// === Constructors ===
///
/// Default constructor for deserialization and mapping.
///
public RedbList()
{
}
///
/// Constructor with name.
///
public RedbList(string name, string? alias = null)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
Alias = alias;
}
// === Static factory methods ===
///
/// Create new list.
///
public static RedbList Create(string name, string? alias = null)
{
return new RedbList(name, alias);
}
// === DDD methods for Items ===
///
/// Add item to list (Aggregate Root pattern).
/// Automatically sets IdList.
///
public IRedbListItem AddItem(string value, long? idObject = null, string? alias = null)
{
var item = new RedbListItem
{
IdList = this.Id,
Value = value,
Alias = alias,
IdObject = idObject
};
_items.Add(item);
return item;
}
///
/// Add item with linked object (Aggregate Root pattern).
/// Automatically extracts Id from object.
///
public IRedbListItem AddItem(string value, IRedbObject linkedObject, string? alias = null)
{
var item = new RedbListItem(this, value, alias, linkedObject);
_items.Add(item);
return item;
}
///
/// Create item without adding to collection.
/// Useful for separate save.
///
public IRedbListItem CreateItem(string value, long? idObject = null, string? alias = null)
{
return new RedbListItem
{
IdList = this.Id,
Value = value,
Alias = alias,
IdObject = idObject
};
}
///
/// Create item with linked object.
///
public IRedbListItem CreateItem(string value, IRedbObject linkedObject, string? alias = null)
{
return new RedbListItem(this, value, alias, linkedObject);
}
///
/// Remove item from list.
///
public bool RemoveItem(IRedbListItem item)
{
if (item is RedbListItem redbItem)
{
return _items.Remove(redbItem);
}
var toRemove = _items.FirstOrDefault(i => i.Id == item.Id);
if (toRemove != null)
{
return _items.Remove(toRemove);
}
return false;
}
///
/// Find item by value.
///
public IRedbListItem? FindItemByValue(string value)
{
return _items.FirstOrDefault(i => i.Value == value);
}
///
/// Indexer to access list items by value.
///
public IRedbListItem? this[string value] => FindItemByValue(value);
///
/// Clear items collection.
///
public void ClearItems()
{
_items.Clear();
}
public override string ToString()
{
var aliasStr = !string.IsNullOrEmpty(Alias) ? $" ({Alias})" : "";
return $"List {Id}: {Name}{aliasStr}";
}
}
}