Table of Contents

EfCoreDataSource<TMarker> Class

Definition

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

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
object
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

Name Description
EfCoreDataSource(EfCoreDataSourceOptions<TMarker>)

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

See Also