Skip to content

Commit

Permalink
[Refactor] Move entity configurations to separate assembly and apply …
Browse files Browse the repository at this point in the history
…them in DbContext
  • Loading branch information
Edrisym committed Nov 28, 2024
1 parent e0c2079 commit 01bfb1c
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 79 deletions.
45 changes: 45 additions & 0 deletions EWallet.Api/Data/CurrencyConfigurations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace EWallet.Api.Data;

public class CurrencyConfigurations : IEntityTypeConfiguration<Currency>
{
public void Configure(EntityTypeBuilder<Currency> builder)
{
builder.ToTable(SchemaTable.Currencies);

builder.HasKey(x => x.Id);

builder.Property(x => x.Id)
.ValueGeneratedNever()
.HasConversion(id => id.Value,
value => CurrencyId.From(value))
.HasColumnType(DefaultColumnType.Nvarchar36);

builder.Property(x => x.Ratio)
.IsRequired()
.HasColumnType(DefaultColumnType.Decimal);

builder.Property(x => x.CreatedOnUtc)
.IsRequired();

builder.Property(x => x.ModifiedOnUtc)
.IsRequired(false);

builder.Property(x => x.Code)
.HasMaxLength(10) // BASEDGODS,HOTDOGE -- BTC,USD,EUR
.IsUnicode(false);

builder.Property(x => x.Name) // Dollar,Euro,Lira
.HasMaxLength(100)
.IsRequired()
.IsUnicode(false);

builder.HasIndex(c => c.Code)
.HasDatabaseName("IX_Currencies_Code")
.IsClustered(false);

builder.HasIndex(e => new { e.Code, e.Name })
.IsUnique();
}
}
47 changes: 47 additions & 0 deletions EWallet.Api/Data/WalletConfigurations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace EWallet.Api.Data;

public class WalletConfigurations : IEntityTypeConfiguration<Wallet>
{
public void Configure(EntityTypeBuilder<Wallet> builder)
{
builder.ToTable(SchemaTable.Wallets);

builder.HasKey(w => w.Id);

builder.Property(x => x.Id)
.ValueGeneratedNever()
.HasConversion(id => id.Value,
value => WalletId.From(value))
.HasColumnType(DefaultColumnType.Nvarchar36);

builder.Property(w => w.Balance)
.IsRequired()
.HasColumnType(DefaultColumnType.Decimal);

builder.Property(w => w.StatusId)
.IsRequired();

builder.Property(w => w.CreatedOnUtc)
.IsRequired();

builder.Property(w => w.ModifiedOnUtc)
.IsRequired(false);

builder.HasOne(w => w.Currency)
.WithMany()
.HasForeignKey(w => w.CurrencyId)
.OnDelete(DeleteBehavior.Restrict);

builder.Property(x => x.CurrencyId)
.ValueGeneratedNever()
.HasConversion(id => id.Value,
value => CurrencyId.From(value))
.HasColumnType(DefaultColumnType.Nvarchar36);

builder.HasIndex(w => w.StatusId)
.HasDatabaseName("IX_Wallets_StatusId")
.IsClustered(false);
}
}
83 changes: 4 additions & 79 deletions EWallet.Api/Data/WalletDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using EWallet.Api.Data;

public class WalletDbContext(DbContextOptions<WalletDbContext> options) : DbContext(options)
{
public DbSet<Wallet> Wallets { get; set; }
Expand All @@ -6,84 +8,7 @@ public class WalletDbContext(DbContextOptions<WalletDbContext> options) : DbCont

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Currency>(builder =>
{
builder.ToTable(SchemaTable.Currencies);

builder.HasKey(x => x.Id);

builder.Property(x => x.Id)
.ValueGeneratedNever()
.HasConversion(id => id.Value,
value => CurrencyId.From(value))
.HasColumnType(DefaultColumnType.Nvarchar36);

builder.Property(x => x.Ratio)
.IsRequired()
.HasColumnType(DefaultColumnType.Decimal);

builder.Property(x => x.CreatedOnUtc)
.IsRequired();

builder.Property(x => x.ModifiedOnUtc)
.IsRequired(false);

builder.Property(x => x.Code)
.HasMaxLength(10) // BASEDGODS,HOTDOGE -- BTC,USD,EUR
.IsUnicode(false);

builder.Property(x => x.Name) // Dollar,Euro,Lira
.HasMaxLength(100)
.IsRequired()
.IsUnicode(false);

builder.HasIndex(c => c.Code)
.HasDatabaseName("IX_Currencies_Code")
.IsClustered(false);

builder.HasIndex(e => new { e.Code, e.Name })
.IsUnique();
});

modelBuilder.Entity<Wallet>(builder =>
{
builder.ToTable(SchemaTable.Wallets);

builder.HasKey(w => w.Id);

builder.Property(x => x.Id)
.ValueGeneratedNever()
.HasConversion(id => id.Value,
value => WalletId.From(value))
.HasColumnType(DefaultColumnType.Nvarchar36);

builder.Property(w => w.Balance)
.IsRequired()
.HasColumnType(DefaultColumnType.Decimal);

builder.Property(w => w.StatusId)
.IsRequired();

builder.Property(w => w.CreatedOnUtc)
.IsRequired();

builder.Property(w => w.ModifiedOnUtc)
.IsRequired(false);

builder.HasOne(w => w.Currency)
.WithMany()
.HasForeignKey(w => w.CurrencyId)
.OnDelete(DeleteBehavior.Restrict);

builder.Property(x => x.CurrencyId)
.ValueGeneratedNever()
.HasConversion(id => id.Value,
value => CurrencyId.From(value))
.HasColumnType(DefaultColumnType.Nvarchar36);

builder.HasIndex(w => w.StatusId)
.HasDatabaseName("IX_Wallets_StatusId")
.IsClustered(false);
});
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfigurationsFromAssembly(typeof(WalletConfigurations).Assembly);
}
}

0 comments on commit 01bfb1c

Please sign in to comment.