EfCoreDataStore<T> Class
Definition
- Namespace
- Savvyio.Extensions.EFCore
- Assemblies
- 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
-
objectEfCoreDataStore<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
| 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 |
| UnitOfWork | Gets a IUnitOfWork that can be used to save instances of |
Methods
| Name | Description |
|---|---|
| CreateAsync(T, Action<AsyncOptions>) | Creates the specified |
| DeleteAsync(T, Action<AsyncOptions>) | Deletes the specified |
| FindAllAsync(Action<EfCoreQueryOptions<T>>) | Finds all objects matching the specified |
| GetByIdAsync(object, Action<AsyncOptions>) | Loads the object from the specified |
| UpdateAsync(T, Action<AsyncOptions>) | Updates the specified |