Table of Contents

Class EfCoreDataSource<TMarker>

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

Provides a default implementation of the IEfCoreDataSource<TMarker> interface to support multiple implementations that does the actual I/O communication with a source of data using Microsoft Entity Framework Core.

public class EfCoreDataSource<TMarker> : EfCoreDataSource, IDisposable, IEfCoreDataSource<TMarker>, IEfCoreDataSource, IDataSource<TMarker>, IDataSource, IUnitOfWork<TMarker>, IUnitOfWork, IDependencyInjectionMarker<TMarker>

Type Parameters

TMarker

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

Inheritance
EfCoreDataSource<TMarker>
Implements
IDataSource<TMarker>
IUnitOfWork<TMarker>
Inherited Members

Examples

The DI-registered EfCoreDataSource<TContext> is the concrete IEfCoreDataSource bound by AddEfCoreDataSource. Resolve it as IEfCoreDataSource to access the DbContext factory or pass it to EF Core repositories. The example registers the data source with an in-memory provider and resolves it from the service provider.

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

namespace ExampleApp;

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

        using var provider = services.BuildServiceProvider();
        var source = provider.GetRequiredService<IEfCoreDataSource<CatalogMarker>>();

        return source is EfCoreDataSource<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

EfCoreDataSource(EfCoreDataSourceOptions<TMarker>)

Initializes a new instance of the EfCoreDataSource<TMarker> class.

public EfCoreDataSource(EfCoreDataSourceOptions<TMarker> options)

Parameters

options EfCoreDataSourceOptions<TMarker>

The EfCoreDataSourceOptions<TMarker> used to configure this instance.

See Also