Skip to main content

Interface IRepository<T>

Generic repository interface defining basic CRUD operations for a specific entity type.

public interface IRepository<T> where T : class

Type Parameters​

NameTypeDescription
TThe type of the entity managed by the repository. Must be a class.

Examples​

Defining a SQLite-backed entity and performing CRUD via DatabaseConnector.

Implement IPrimaryKey so [GetRepository](../Classes/DatabaseConnector.mdx) accepts the type. The [PrimaryKey][AutoIncrement]` attributes are read by SQLiteContext to create the backing table correctly; omitting them causes insert collisions on the integer key column.

// Entity — satisfies the IPrimaryKey and new() constraints:
[SQLite.Table("session_log")]
public class SessionLogEntry : IPrimaryKey
{
[SQLite.PrimaryKey]
[SQLite.AutoIncrement]
public int Id { get; set; }
public string ScenarioName { get; set; }
public float DurationSeconds { get; set; }
}

// Persist a new entry at the end of a scenario:
var repo = DatabaseConnector.Current.GetRepository();
repo.Write(new SessionLogEntry { ScenarioName = "Urban01", DurationSeconds = 134.5f });

// Retrieve a specific entry by its auto-assigned primary key:
SessionLogEntry entry = repo.ReadByID(1);

// Enumerate all entries for post-session export:
List all = repo.ReadAll();

// Remove a stale entry:
repo.Delete(entry);

Methods​

Delete(T)​

Deletes the specified entity from the database.

void Delete(T entity)

Parameters​

NameTypeDescription
entityTThe entity to delete.

ReadAll()​

Retrieves all entities of type T from the database.

List<T> ReadAll()

Returns​

List<T> — A list of all entities.

ReadByID(int)​

Retrieves an entity by its unique identifier.

T ReadByID(int id)

Parameters​

NameTypeDescription
idintThe unique identifier of the entity.

Returns​

T — The entity if found; otherwise, null.

Write(T)​

Persists the given entity to the database. If the entity does not exist, it will be inserted;
if it already exists, it will be replaced.

void Write(T entity)

Parameters​

NameTypeDescription
entityTThe entity to write.