Table of Contents

EfCoreDataStore<T> Class

Definition

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

Provides a default implementation of the IPersistentDataStore<T, TOptions> 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 EfCoreDataStore<T> : IPersistentDataStore<T, EfCoreQueryOptions<T>>, IWritableDataStore<T>, IReadableDataStore<T>, ISearchableDataStore<T, EfCoreQueryOptions<T>>, IDeletableDataStore<T>, IDataStore<T> where T : class

Type Parameters

T

The type of the DTO.

Inheritance
object
EfCoreDataStore<T>
Implements
Derived

Examples

EfCoreDataStore<T, TContext> provides EF Core–backed create, read, update, delete, and search operations. Subclass it with the entity type and context type, then inject the matching IEfCoreDataSource as the constructor argument. The example creates a concrete order data store and verifies it can be instantiated with the data source.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Savvyio;
using Savvyio.Extensions.EFCore;

namespace ExampleApp;

public sealed class CatalogQueries
{
    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 store = new CatalogDataStore(source);
        return store.FindFeaturedAsync();
    }
}

public sealed class CatalogDataSource : EfCoreDataSource
{
    public CatalogDataSource(EfCoreDataSourceOptions options) : base(options)
    {
    }
}

public sealed class CatalogDataStore : EfCoreDataStore<Product>
{
    public CatalogDataStore(IEfCoreDataSource source) : base(source)
    {
    }

    public Task<IEnumerable<Product>> FindFeaturedAsync()
    {
        return FindAllAsync(options => options.Predicate = 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
EfCoreDataStore(IEfCoreDataSource)

Initializes a new instance of the EfCoreDataStore<T> class.

Properties

Name Description
Set

Gets a DbSet<TEntity> that can be used to query and save instances of T.

UnitOfWork

Gets a IUnitOfWork that can be used to save instances of T.

Methods

Name Description
CreateAsync(T, Action<AsyncOptions>)

Creates the specified dto asynchronous in the associated IEfCoreDataSource.

DeleteAsync(T, Action<AsyncOptions>)

Deletes the specified dto asynchronous in the associated IEfCoreDataSource.

FindAllAsync(Action<EfCoreQueryOptions<T>>)

Finds all objects matching the specified setup asynchronous in the associated IEfCoreDataSource.

GetByIdAsync(object, Action<AsyncOptions>)

Loads the object from the specified id asynchronous.

UpdateAsync(T, Action<AsyncOptions>)

Updates the specified dto asynchronous in the associated IEfCoreDataSource.

See Also