Table of Contents

EfCoreDataSource Class

Definition

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

Provides a default implementation of the IEfCoreDataSource interface to support the actual I/O communication with a source of data using Microsoft Entity Framework Core.

public class EfCoreDataSource : Disposable, IDisposable, IEfCoreDataSource, IDataSource, IUnitOfWork
Inheritance
object
EfCoreDataSource
Implements
Derived
Inherited Members

Examples

This example shows how a custom EfCoreDataSource can wrap a dedicated DbContext for a catalog workflow.

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

namespace ExampleApp;

public sealed class CatalogWorkflow
{
    public CatalogDataSource CreateDataSource()
    {
        var options = new EfCoreDataSourceOptions
        {
            ContextConfigurator = builder => builder.EnableDetailedErrors(),
            ModelConstructor = modelBuilder => modelBuilder.Entity<Product>().HasKey(product => product.Id)
        };

        return new CatalogDataSource(new CatalogDbContext(options));
    }
}

public sealed class CatalogDataSource : EfCoreDataSource
{
    public CatalogDataSource(CatalogDbContext dbContext) : base(dbContext)
    {
    }

    public DbSet<Product> Products => Set<Product>();
}

public sealed class CatalogDbContext : DbContext
{
    private readonly EfCoreDataSourceOptions _options;

    public CatalogDbContext(EfCoreDataSourceOptions options)
    {
        _options = options;
    }

    public DbSet<Product> Products => Set<Product>();

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        _options.ContextConfigurator?.Invoke(optionsBuilder);
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        _options.ModelConstructor?.Invoke(modelBuilder);
    }
}

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

Name Description
EfCoreDataSource(DbContext)

Initializes a new instance of the EfCoreDataSource class.

EfCoreDataSource(EfCoreDataSourceOptions)

Initializes a new instance of the EfCoreDataSource class.

Properties

Name Description
DbContext

Gets the session of this data source.

Methods

Name Description
OnDisposeManagedResources()

Called when this object is being disposed by either Dispose() or Dispose(bool) having disposing set to true and Disposed is false.

SaveChangesAsync(Action<AsyncOptions>)

Saves the different IRepository<TEntity, TKey> implementations as one transaction towards a data store asynchronous.

Set<TEntity>()

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

See Also