Table of Contents

Class EfCoreRepository<TEntity, TKey, TMarker>

Namespace
Savvyio.Extensions.DependencyInjection.EFCore
Assembly
Savvyio.Extensions.DependencyInjection.EFCore.dll

Provides a default implementation of the IPersistentRepository<TEntity, TKey, TMarker> interface to support multiple implementations that serves 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, TMarker> : EfCoreRepository<TEntity, TKey>, IPersistentRepository<TEntity, TKey, TMarker>, IPersistentRepository<TEntity, TKey>, IWritableRepository<TEntity, TKey, TMarker>, IWritableRepository<TEntity, TKey>, IReadableRepository<TEntity, TKey, TMarker>, IReadableRepository<TEntity, TKey>, ISearchableRepository<TEntity, TKey, TMarker>, ISearchableRepository<TEntity, TKey>, IDeletableRepository<TEntity, TKey, TMarker>, IRepository<TEntity, TKey, TMarker>, IDependencyInjectionMarker<TMarker>, 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.

TMarker

The type used to mark the implementation that this repository represents. Optimized for Microsoft Dependency Injection.

Inheritance
EfCoreRepository<TEntity, TKey>
EfCoreRepository<TEntity, TKey, TMarker>
Implements
IPersistentRepository<TEntity, TKey, TMarker>
IPersistentRepository<TEntity, TKey>
IWritableRepository<TEntity, TKey, TMarker>
IWritableRepository<TEntity, TKey>
IReadableRepository<TEntity, TKey, TMarker>
IReadableRepository<TEntity, TKey>
ISearchableRepository<TEntity, TKey, TMarker>
ISearchableRepository<TEntity, TKey>
IDeletableRepository<TEntity, TKey, TMarker>
IRepository<TEntity, TKey, TMarker>
IDeletableRepository<TEntity, TKey>
IRepository<TEntity, TKey>
Inherited Members

Examples

The DI-registered EfCoreRepository<TEntity, TKey, TContext> resolves as IRepository<TEntity, TKey> when registered via AddEfCoreRepository. The data source registered by AddEfCoreDataSource provides the context. The example registers and resolves the repository to verify the complete EF Core repository wiring.

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Savvyio;
using Savvyio.Extensions.DependencyInjection.Domain;
using Savvyio.Extensions.DependencyInjection.EFCore;

namespace ExampleApp;

public sealed class MarkerRepositoryExample
{
    public bool IsResolvedAsMarkedRepository()
    {
        var services = new ServiceCollection();
        services.AddEfCoreDataSource<CatalogMarker>(options =>
        {
            options.ContextConfigurator = builder => builder.EnableDetailedErrors();
            options.ModelConstructor = modelBuilder => modelBuilder.Entity<Product>().HasKey(product => product.Id);
        });
        services.AddEfCoreRepository<Product, Guid, CatalogMarker>();

        using var provider = services.BuildServiceProvider();
        var repository = provider.GetRequiredService<IPersistentRepository<Product, Guid, CatalogMarker>>();

        return repository is EfCoreRepository<Product, Guid, CatalogMarker>;
    }
}

public sealed class CatalogMarker
{
}

public sealed class Product : IIdentity<Guid>
{
    public Product(Guid id, string name)
    {
        Id = id;
        Name = name;
    }

    public Guid Id { get; }

    public string Name { get; private set; } = string.Empty;
}

Constructors

EfCoreRepository(IEfCoreDataSource<TMarker>)

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

public EfCoreRepository(IEfCoreDataSource<TMarker> source)

Parameters

source IEfCoreDataSource<TMarker>

The IEfCoreDataSource that handles actual I/O communication with a source of data.

See Also

EfCoreRepository<TEntity, TKey>
IPersistentRepository<TEntity, TKey, TMarker>