Class InMemoryRepositoryBase<T>
- Namespace
- Core.CliniCore.Repositories.InMemory
- Assembly
- Core.CliniCore.dll
Abstract base class for in-memory repository implementations. Provides thread-safe CRUD operations using a dictionary backing store.
Design Notes:
- Uses lock-based synchronization for thread safety
- Dictionary keyed by entity Id for O(1) lookups
- Derived classes implement Search() method for entity-specific search logic
public abstract class InMemoryRepositoryBase<T> : IRepository<T> where T : class, IIdentifiable
Type Parameters
TEntity type implementing IIdentifiable
- Inheritance
-
InMemoryRepositoryBase<T>
- Implements
-
IRepository<T>
- Derived
- Inherited Members
Fields
_entities
protected readonly Dictionary<Guid, T> _entities
Field Value
- Dictionary<Guid, T>
_lock
protected readonly object _lock
Field Value
Properties
Count
Gets the count of entities in the repository
public int Count { get; }
Property Value
Methods
Add(T)
Adds a new entity to the repository
public virtual void Add(T entity)
Parameters
entityTThe entity to add
Exceptions
- ArgumentNullException
If entity is null
- InvalidOperationException
If entity with same Id already exists
Delete(Guid)
Deletes an entity from the repository by Id
public virtual void Delete(Guid id)
Parameters
idGuidThe Id of the entity to delete
Exceptions
- KeyNotFoundException
If entity doesn't exist
Exists(Guid)
Checks if an entity with the given Id exists
public bool Exists(Guid id)
Parameters
idGuid
Returns
GetAll()
Gets all entities in the repository
public virtual IEnumerable<T> GetAll()
Returns
- IEnumerable<T>
An enumerable of all entities (snapshot at time of call)
GetById(Guid)
Gets an entity by its unique identifier
public virtual T? GetById(Guid id)
Parameters
idGuidThe entity's GUID
Returns
- T
The entity if found, null otherwise
Search(string)
Searches for entities matching the query string. Must be implemented by derived classes for entity-specific search logic.
public abstract IEnumerable<T> Search(string query)
Parameters
querystringThe search query string
Returns
- IEnumerable<T>
Entities matching the query
Update(T)
Updates an existing entity in the repository
public virtual void Update(T entity)
Parameters
entityTThe entity with updated values
Exceptions
- ArgumentNullException
If entity is null
- KeyNotFoundException
If entity doesn't exist