Class EfCoreDataStore<T>
- Namespace
- Savvyio.Extensions.EFCore
- Assembly
- Savvyio.Extensions.EFCore.dll
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
TThe type of the DTO.
- Inheritance
-
EfCoreDataStore<T>
- Implements
-
IDataStore<T>
- 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
EfCoreDataStore(IEfCoreDataSource)
Initializes a new instance of the EfCoreDataStore<T> class.
public EfCoreDataStore(IEfCoreDataSource source)
Parameters
sourceIEfCoreDataSourceThe IEfCoreDataSource that handles actual I/O communication with a source of data.
Properties
Set
Gets a DbSet<TEntity> that can be used to query and save instances of T.
protected DbSet<T> Set { get; }
Property Value
- DbSet<T>
The 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.
protected IUnitOfWork UnitOfWork { get; }
Property Value
- IUnitOfWork
The IUnitOfWork that can be used to save instances of
T.
Methods
CreateAsync(T, Action<AsyncOptions>)
Creates the specified dto asynchronous in the associated IEfCoreDataSource.
public virtual Task CreateAsync(T dto, Action<AsyncOptions> setup = null)
Parameters
dtoTThe object to create in the associated IEfCoreDataSource.
setupAction<AsyncOptions>The AsyncOptions which may be configured.
Returns
DeleteAsync(T, Action<AsyncOptions>)
Deletes the specified dto asynchronous in the associated IEfCoreDataSource.
public virtual Task DeleteAsync(T dto, Action<AsyncOptions> setup = null)
Parameters
dtoTThe object to delete in the associated IEfCoreDataSource.
setupAction<AsyncOptions>The AsyncOptions which may be configured.
Returns
FindAllAsync(Action<EfCoreQueryOptions<T>>)
Finds all objects matching the specified setup asynchronous in the associated IEfCoreDataSource.
public virtual Task<IEnumerable<T>> FindAllAsync(Action<EfCoreQueryOptions<T>> setup = null)
Parameters
setupAction<EfCoreQueryOptions<T>>The EfCoreQueryOptions<T> which may be configured.
Returns
- Task<IEnumerable<T>>
A Task<TResult> that represents the asynchronous operation. The task result either contains the matching objects of the operation or an empty sequence if no match was found.
GetByIdAsync(object, Action<AsyncOptions>)
Loads the object from the specified id asynchronous.
public virtual Task<T> GetByIdAsync(object id, Action<AsyncOptions> setup = null)
Parameters
idobjectThe key that uniquely identifies the object.
setupAction<AsyncOptions>The AsyncOptions which may be configured.
Returns
- Task<T>
A Task<TResult> that represents the asynchronous operation. The task result either contains the object of the operation or
nullif not found.
UpdateAsync(T, Action<AsyncOptions>)
Updates the specified dto asynchronous in the associated IEfCoreDataSource.
public virtual Task UpdateAsync(T dto, Action<AsyncOptions> setup = null)
Parameters
dtoTThe object to update in the associated IEfCoreDataSource.
setupAction<AsyncOptions>The AsyncOptions which may be configured.