Init
This commit is contained in:
53
Prefab.Catalog.Api/Data/AppDb.cs
Normal file
53
Prefab.Catalog.Api/Data/AppDb.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Prefab.Catalog.Domain.Entities;
|
||||
using Prefab.Data;
|
||||
using Prefab.Handler;
|
||||
|
||||
namespace Prefab.Catalog.Api.Data;
|
||||
|
||||
public class AppDb : PrefabDb, IPrefabDb,
|
||||
Prefab.Catalog.Data.IModuleDb,
|
||||
Prefab.Catalog.Data.IModuleDbReadOnly
|
||||
{
|
||||
public AppDb(DbContextOptions<AppDb> options, IHandlerContextAccessor accessor)
|
||||
: base(options, accessor)
|
||||
{
|
||||
}
|
||||
|
||||
protected AppDb(DbContextOptions options, IHandlerContextAccessor accessor)
|
||||
: base(options, accessor)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void PrefabOnModelCreating(ModelBuilder builder)
|
||||
{
|
||||
builder.ApplyConfigurationsFromAssembly(typeof(Prefab.Catalog.Data.IModuleDb).Assembly);
|
||||
}
|
||||
|
||||
protected override void PrefabOnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
// Additional write-context configuration can be applied here if needed.
|
||||
}
|
||||
|
||||
public DbSet<Category> Categories => Set<Category>();
|
||||
|
||||
public DbSet<Product> Products => Set<Product>();
|
||||
|
||||
public DbSet<OptionDefinition> OptionDefinitions => Set<OptionDefinition>();
|
||||
|
||||
public DbSet<OptionValue> OptionValues => Set<OptionValue>();
|
||||
|
||||
public DbSet<OptionTier> OptionTiers => Set<OptionTier>();
|
||||
|
||||
public DbSet<OptionRuleSet> OptionRuleSets => Set<OptionRuleSet>();
|
||||
|
||||
public DbSet<OptionRuleCondition> OptionRuleConditions => Set<OptionRuleCondition>();
|
||||
|
||||
public DbSet<VariantAxisValue> VariantAxisValues => Set<VariantAxisValue>();
|
||||
|
||||
public DbSet<AttributeDefinition> AttributeDefinitions => Set<AttributeDefinition>();
|
||||
|
||||
public DbSet<ProductAttributeValue> ProductAttributeValues => Set<ProductAttributeValue>();
|
||||
|
||||
public DbSet<ProductCategory> ProductCategories => Set<ProductCategory>();
|
||||
}
|
||||
20
Prefab.Catalog.Api/Data/AppDbReadOnly.cs
Normal file
20
Prefab.Catalog.Api/Data/AppDbReadOnly.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Prefab.Handler;
|
||||
|
||||
namespace Prefab.Catalog.Api.Data;
|
||||
|
||||
public class AppDbReadOnly(DbContextOptions<AppDbReadOnly> options, IHandlerContextAccessor accessor) : AppDb(options, accessor), Prefab.Data.IPrefabDbReadOnly,
|
||||
Prefab.Catalog.Data.IModuleDbReadOnly
|
||||
{
|
||||
protected override void PrefabOnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
base.PrefabOnConfiguring(optionsBuilder);
|
||||
optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
|
||||
}
|
||||
|
||||
public override int SaveChanges()
|
||||
=> throw new InvalidOperationException("This database context is read-only. Saving changes is not allowed.");
|
||||
|
||||
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
|
||||
=> throw new InvalidOperationException("This database context is read-only. Saving changes is not allowed.");
|
||||
}
|
||||
16
Prefab.Catalog.Api/Data/CatalogDbContextFactory.cs
Normal file
16
Prefab.Catalog.Api/Data/CatalogDbContextFactory.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Prefab.Catalog.Data;
|
||||
|
||||
namespace Prefab.Catalog.Api.Data;
|
||||
|
||||
internal sealed class CatalogDbContextFactory(
|
||||
IDbContextFactory<AppDb> writeFactory,
|
||||
IDbContextFactory<AppDbReadOnly> readFactory) : ICatalogDbContextFactory
|
||||
{
|
||||
public async ValueTask<IModuleDb> CreateWritableAsync(CancellationToken cancellationToken = default) =>
|
||||
await writeFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
public async ValueTask<IModuleDbReadOnly> CreateReadOnlyAsync(CancellationToken cancellationToken = default) =>
|
||||
await readFactory.CreateDbContextAsync(cancellationToken);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user