2023-02-28 14:14:45 +00:00
using Elsa.Common.Entities ;
2022-09-04 15:04:14 +00:00
using Elsa.Features.Abstractions ;
using Elsa.Features.Services ;
using Microsoft.EntityFrameworkCore ;
using Microsoft.Extensions.DependencyInjection ;
2022-12-30 12:11:29 +00:00
namespace Elsa.EntityFrameworkCore.Common ;
2022-09-04 15:04:14 +00:00
2023-05-28 14:40:10 +00:00
/// <summary>
/// Base class for features that require Entity Framework Core.
/// </summary>
/// <typeparam name="TDbContext">The type of the database context.</typeparam>
2022-09-04 15:04:14 +00:00
public abstract class PersistenceFeatureBase < TDbContext > : FeatureBase where TDbContext : DbContext
{
2023-05-28 14:40:10 +00:00
/// <inheritdoc />
2022-09-04 15:04:14 +00:00
protected PersistenceFeatureBase ( IModule module ) : base ( module )
{
}
2023-05-28 14:40:10 +00:00
/// <summary>
/// Gets or sets a value indicating whether to use context pooling.
/// </summary>
2023-01-12 10:59:31 +00:00
public bool UseContextPooling { get ; set ; }
2023-05-28 14:40:10 +00:00
/// <summary>
/// Gets or sets a value indicating whether to run migrations.
/// </summary>
2023-01-12 10:59:31 +00:00
public bool RunMigrations { get ; set ; } = true ;
2023-05-28 14:40:10 +00:00
/// <summary>
/// Gets or sets the lifetime of the <see cref="IDbContextFactory{TContext}"/>. Defaults to <see cref="ServiceLifetime.Singleton"/>.
/// </summary>
2022-09-04 15:04:14 +00:00
public ServiceLifetime DbContextFactoryLifetime { get ; set ; } = ServiceLifetime . Singleton ;
2023-05-28 09:54:12 +00:00
2023-05-28 14:40:10 +00:00
/// <summary>
/// Gets or sets the callback used to configure the <see cref="DbContextOptionsBuilder"/>.
/// </summary>
2023-05-28 09:54:12 +00:00
public Action < IServiceProvider , DbContextOptionsBuilder > DbContextOptionsBuilder = ( _ , options ) = > options
. UseSqlite ( "Data Source=elsa.sqlite.db;Cache=Shared;" , sqlite = > sqlite
. MigrationsAssembly ( "Elsa.EntityFrameworkCore.Sqlite" )
. MigrationsHistoryTable ( ElsaDbContextBase . MigrationsHistoryTable , ElsaDbContextBase . ElsaSchema ) ) ;
2022-09-04 15:04:14 +00:00
2023-05-28 14:40:10 +00:00
/// <inheritdoc />
2022-09-04 15:04:14 +00:00
public override void ConfigureHostedServices ( )
{
2023-01-12 10:59:31 +00:00
if ( RunMigrations )
2023-01-13 18:10:20 +00:00
Module . ConfigureHostedService < RunMigrationsHostedService < TDbContext > > ( - 100 ) ; // Migrations need to run before other hosted services that depend on DB access.
2022-09-04 15:04:14 +00:00
}
2023-05-28 14:40:10 +00:00
/// <inheritdoc />
2022-09-04 15:04:14 +00:00
public override void Apply ( )
{
2023-01-12 10:59:31 +00:00
if ( UseContextPooling )
2022-09-04 15:04:14 +00:00
Services . AddPooledDbContextFactory < TDbContext > ( DbContextOptionsBuilder ) ;
else
Services . AddDbContextFactory < TDbContext > ( DbContextOptionsBuilder , DbContextFactoryLifetime ) ;
}
2023-05-28 14:40:10 +00:00
/// <summary>
/// Adds a store to the service collection.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <typeparam name="TStore">The type of the store.</typeparam>
2022-09-04 15:04:14 +00:00
protected void AddStore < TEntity , TStore > ( ) where TEntity : class where TStore : class
{
Services
. AddSingleton < Store < TDbContext , TEntity > > ( )
. AddSingleton < TStore > ( )
;
}
2023-02-28 14:14:45 +00:00
2023-05-28 14:40:10 +00:00
/// <summary>
/// Adds an entity store to the service collection.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <typeparam name="TStore">The type of the store.</typeparam>
2023-02-28 14:14:45 +00:00
protected void AddEntityStore < TEntity , TStore > ( ) where TEntity : Entity where TStore : class
{
Services
. AddSingleton < EntityStore < TDbContext , TEntity > > ( )
. AddSingleton < TStore > ( )
;
}
2022-09-04 15:04:14 +00:00
}