Class EfCoreRepository<TEntity, TKey>
- Namespace
- Savvyio.Extensions.EFCore
- Assembly
- Savvyio.Extensions.EFCore.dll
Provides a default implementation of the IPersistentRepository<TEntity, TKey> interface to serve as an abstraction layer before the actual I/O communication with a source of data using Microsoft Entity Framework Core.
public class EfCoreRepository<TEntity, TKey> : IPersistentRepository<TEntity, TKey>, IWritableRepository<TEntity, TKey>, IReadableRepository<TEntity, TKey>, ISearchableRepository<TEntity, TKey>, IDeletableRepository<TEntity, TKey>, IRepository<TEntity, TKey> where TEntity : class, IIdentity<TKey>
Type Parameters
TEntityThe type of the entity.
TKeyThe type of the key that uniquely identifies the entity.
- Inheritance
-
EfCoreRepository<TEntity, TKey>
- Implements
-
IPersistentRepository<TEntity, TKey>IWritableRepository<TEntity, TKey>IReadableRepository<TEntity, TKey>ISearchableRepository<TEntity, TKey>IDeletableRepository<TEntity, TKey>IRepository<TEntity, TKey>
- Derived
Examples
EfCoreRepository<TEntity, TKey, TContext> is the EF Core aggregate repository base class for types that extend IAggregateRoot<TKey>. Subclass it, supply the aggregate type and key type, and inject the aggregate data source. The example creates a concrete repository and exercises a simple add-and-find workflow using an in-memory provider.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Savvyio;
using Savvyio.Extensions.EFCore;
namespace ExampleApp;
public sealed class CatalogWorkflow
{
public Task<IEnumerable<Product>> LoadFeaturedProductsAsync()
{
var source = new CatalogDataSource(new EfCoreDataSourceOptions
{
ContextConfigurator = builder => builder.EnableDetailedErrors(),
ModelConstructor = modelBuilder => modelBuilder.Entity<Product>().HasKey(product => product.Id)
});
var repository = new CatalogRepository(source);
repository.Add(new Product(Guid.NewGuid(), "Starter kit", true));
return repository.FindFeaturedAsync();
}
}
public sealed class CatalogDataSource : EfCoreDataSource
{
public CatalogDataSource(EfCoreDataSourceOptions options) : base(options)
{
}
}
public sealed class CatalogRepository : EfCoreRepository<Product, Guid>
{
public CatalogRepository(IEfCoreDataSource source) : base(source)
{
}
public Task<IEnumerable<Product>> FindFeaturedAsync()
{
return FindAllAsync(product => product.IsFeatured);
}
}
public sealed class Product : IIdentity<Guid>
{
public Product(Guid id, string name, bool isFeatured)
{
Id = id;
Name = name;
IsFeatured = isFeatured;
}
public Guid Id { get; }
public string Name { get; private set; } = string.Empty;
public bool IsFeatured { get; private set; }
}
Constructors
EfCoreRepository(IEfCoreDataSource)
Initializes a new instance of the EfCoreRepository<TEntity, TKey> class.
public EfCoreRepository(IEfCoreDataSource source)
Parameters
sourceIEfCoreDataSourceThe IEfCoreDataSource that handles actual I/O communication with a source of data.
Properties
Set
Gets the associated DbSet<TEntity> for this repository.
protected DbSet<TEntity> Set { get; }
Property Value
- DbSet<TEntity>
The associated DbSet<TEntity> for this repository.
Methods
Add(TEntity)
Marks the specified entity to be added in the data store when SaveChangesAsync(Action<AsyncOptions>) is called.
public virtual void Add(TEntity entity)
Parameters
entityTEntityThe entity to add.
AddRange(IEnumerable<TEntity>)
Marks the specified entities to be added in the data store when SaveChangesAsync(Action<AsyncOptions>) is called.
public virtual void AddRange(IEnumerable<TEntity> entities)
Parameters
entitiesIEnumerable<TEntity>The entities to add.
FindAllAsync(Expression<Func<TEntity, bool>>, Action<AsyncOptions>)
Finds all entities matching the specified predicate asynchronous.
public virtual Task<IEnumerable<TEntity>> FindAllAsync(Expression<Func<TEntity, bool>> predicate = null, Action<AsyncOptions> setup = null)
Parameters
predicateExpression<Func<TEntity, bool>>The predicate that matches the entities to retrieve.
setupAction<AsyncOptions>The AsyncOptions which may be configured.
Returns
- Task<IEnumerable<TEntity>>
A Task<TResult> that represents the asynchronous operation. The task result either contains the matching entities of the operation or an empty sequence if no match was found.
GetByIdAsync(TKey, Action<AsyncOptions>)
Loads the entity from the specified id asynchronous.
public virtual Task<TEntity> GetByIdAsync(TKey id, Action<AsyncOptions> setup = null)
Parameters
idTKeyThe key that uniquely identifies the entity.
setupAction<AsyncOptions>The AsyncOptions which may be configured.
Returns
- Task<TEntity>
A Task<TResult> that represents the asynchronous operation. The task result either contains the entity of the operation or
nullif not found.
Remove(TEntity)
Marks the specified entity to be removed from the data store when SaveChangesAsync(Action<AsyncOptions>) is called.
public virtual void Remove(TEntity entity)
Parameters
entityTEntityThe entity to remove.
RemoveRange(IEnumerable<TEntity>)
Marks the specified entities to be removed from the data store when SaveChangesAsync(Action<AsyncOptions>) is called.
public virtual void RemoveRange(IEnumerable<TEntity> entities)
Parameters
entitiesIEnumerable<TEntity>The entities to remove.