Skip to content

Commit e70c805

Browse files
committed
Put mapping in its own place
1 parent f45fa89 commit e70c805

File tree

9 files changed

+104
-22
lines changed

9 files changed

+104
-22
lines changed

LinkDotNet.Domain/Enumeration.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ public abstract class Enumeration<TEnumeration>
1010
{
1111
private string key;
1212

13+
protected Enumeration()
14+
{
15+
}
16+
1317
protected Enumeration(string key)
1418
{
1519
if (string.IsNullOrWhiteSpace(key))

LinkDotNet.Domain/ProficiencyLevel.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ public class ProficiencyLevel : Enumeration<ProficiencyLevel>
66
public static readonly ProficiencyLevel Proficient = new(nameof(Proficient));
77
public static readonly ProficiencyLevel Expert = new(nameof(Expert));
88

9-
public ProficiencyLevel(string key)
9+
private ProficiencyLevel()
10+
: base()
11+
{
12+
}
13+
14+
private ProficiencyLevel(string key)
1015
: base(key)
1116
{
1217
}

LinkDotNet.Domain/Skill.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ namespace LinkDotNet.Domain
44
{
55
public class Skill
66
{
7-
public Skill(string name, string iconUrl, string capability, ProficiencyLevel proficiencyLevel)
7+
private Skill()
8+
{
9+
}
10+
11+
private Skill(string name, string iconUrl, string capability, ProficiencyLevel proficiencyLevel)
812
{
913
IconUrl = iconUrl;
1014
Name = name;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using LinkDotNet.Domain;
4+
5+
namespace LinkDotNet.Infrastructure.Persistence
6+
{
7+
public interface ISkillRepository
8+
{
9+
Task<IList<Skill>> GetAllAsync();
10+
11+
Task StoreAsync(Skill skill);
12+
13+
Task DeleteAsync(string id);
14+
}
15+
}
Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using LinkDotNet.Domain;
2+
using LinkDotNet.Infrastructure.Persistence.Sql.Mapping;
23
using Microsoft.EntityFrameworkCore;
34

45
namespace LinkDotNet.Infrastructure.Persistence.Sql
@@ -13,30 +14,18 @@ public BlogDbContext(DbContextOptions options)
1314

1415
public DbSet<BlogPost> BlogPosts { get; set; }
1516

17+
public DbSet<Tag> Tags { get; set; }
18+
1619
public DbSet<ProfileInformationEntry> ProfileInformationEntries { get; set; }
1720

21+
public DbSet<Skill> Skills { get; set; }
22+
1823
protected override void OnModelCreating(ModelBuilder modelBuilder)
1924
{
20-
modelBuilder.Entity<BlogPost>()
21-
.HasKey(c => c.Id);
22-
modelBuilder.Entity<BlogPost>()
23-
.Property(c => c.Id)
24-
.ValueGeneratedOnAdd();
25-
26-
modelBuilder.Entity<BlogPost>()
27-
.HasMany(t => t.Tags)
28-
.WithOne()
29-
.OnDelete(DeleteBehavior.Cascade);
30-
31-
modelBuilder.Entity<Tag>()
32-
.Property(c => c.Id)
33-
.ValueGeneratedOnAdd();
34-
35-
modelBuilder.Entity<ProfileInformationEntry>()
36-
.HasKey(c => c.Id);
37-
modelBuilder.Entity<ProfileInformationEntry>()
38-
.Property(c => c.Id)
39-
.ValueGeneratedOnAdd();
25+
modelBuilder.ApplyConfiguration(new BlogPostConfiguration());
26+
modelBuilder.ApplyConfiguration(new TagsConfiguration());
27+
modelBuilder.ApplyConfiguration(new ProfileInformationEntryConfiguration());
28+
modelBuilder.ApplyConfiguration(new SkillConfiguration());
4029
}
4130
}
4231
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using LinkDotNet.Domain;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
4+
5+
namespace LinkDotNet.Infrastructure.Persistence.Sql.Mapping
6+
{
7+
public class BlogPostConfiguration : IEntityTypeConfiguration<BlogPost>
8+
{
9+
public void Configure(EntityTypeBuilder<BlogPost> builder)
10+
{
11+
builder.HasKey(c => c.Id);
12+
builder.Property(c => c.Id).ValueGeneratedOnAdd();
13+
14+
builder.HasMany(t => t.Tags)
15+
.WithOne()
16+
.OnDelete(DeleteBehavior.Cascade);
17+
}
18+
}
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using LinkDotNet.Domain;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
4+
5+
namespace LinkDotNet.Infrastructure.Persistence.Sql.Mapping
6+
{
7+
public class ProfileInformationEntryConfiguration : IEntityTypeConfiguration<ProfileInformationEntry>
8+
{
9+
public void Configure(EntityTypeBuilder<ProfileInformationEntry> builder)
10+
{
11+
builder.HasKey(c => c.Id);
12+
builder.Property(c => c.Id).ValueGeneratedOnAdd();
13+
}
14+
}
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using LinkDotNet.Domain;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
4+
5+
namespace LinkDotNet.Infrastructure.Persistence.Sql.Mapping
6+
{
7+
public class SkillConfiguration : IEntityTypeConfiguration<Skill>
8+
{
9+
public void Configure(EntityTypeBuilder<Skill> builder)
10+
{
11+
builder.HasKey(s => s.Id);
12+
builder.Property(s => s.Id).ValueGeneratedOnAdd();
13+
builder.Property(s => s.ProficiencyLevel)
14+
.HasConversion(to => to.Key, from => ProficiencyLevel.Create(from));
15+
}
16+
}
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using LinkDotNet.Domain;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
4+
5+
namespace LinkDotNet.Infrastructure.Persistence.Sql.Mapping
6+
{
7+
public class TagsConfiguration : IEntityTypeConfiguration<Tag>
8+
{
9+
public void Configure(EntityTypeBuilder<Tag> builder)
10+
{
11+
builder.Property(c => c.Id).ValueGeneratedOnAdd();
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)