Table of Contents

EfCoreRepository<TEntity, TKey> Class

Definition

Namespace
Savvyio.Extensions.EFCore
Assemblies
Savvyio.Extensions.EFCore.dll
Source
src/Savvyio.Extensions.EFCore/EfCoreRepository.cs

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

TEntity

The type of the entity.

TKey

The type of the key that uniquely identifies the entity.

Inheritance
object
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

Name Description
EfCoreRepository(IEfCoreDataSource)

Initializes a new instance of the EfCoreRepository<TEntity, TKey> class.

Properties

Name Description
Set

Gets the associated DbSet<TEntity> for this repository.

Methods

Name Description
Add(TEntity)

Marks the specified entity to be added in the data store when SaveChangesAsync(Action<AsyncOptions>) is called.

AddRange(IEnumerable<TEntity>)

Marks the specified entities to be added in the data store when SaveChangesAsync(Action<AsyncOptions>) is called.

FindAllAsync(Expression<Func<TEntity, bool>>, Action<AsyncOptions>)

Finds all entities matching the specified predicate asynchronous.

GetByIdAsync(TKey, Action<AsyncOptions>)

Loads the entity from the specified id asynchronous.

Remove(TEntity)

Marks the specified entity to be removed from the data store when SaveChangesAsync(Action<AsyncOptions>) is called.

RemoveRange(IEnumerable<TEntity>)

Marks the specified entities to be removed from the data store when SaveChangesAsync(Action<AsyncOptions>) is called.

See Also

IPersistentRepository<TEntity, TKey>