Skip to content

Commit

Permalink
Expose Ids to domain;
Browse files Browse the repository at this point in the history
Use FlakeId (long in database) as part of preparations for future web functionality;
  • Loading branch information
Neakita committed Oct 8, 2023
1 parent cdd2c36 commit 6c92bf5
Show file tree
Hide file tree
Showing 31 changed files with 91 additions and 27 deletions.
3 changes: 2 additions & 1 deletion SightKeeper.Data/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.ApplyConfiguration(new ProfileItemClassConfiguration());
modelBuilder.ApplyConfiguration(new WeightsConfiguration());
modelBuilder.Entity<Asset>().Navigation(asset => asset.Screenshot).AutoInclude();
modelBuilder.Entity<WeightsData>().HasShadowKey();
modelBuilder.Entity<WeightsData>().HasKey(weightsData => weightsData.Id);
modelBuilder.Entity<WeightsData>().HasFlakeId(weightsData => weightsData.Id);
modelBuilder.Entity<PTData>().ToTable("PTData");
modelBuilder.Entity<ONNXData>().ToTable("ONNXData");
}
Expand Down
3 changes: 2 additions & 1 deletion SightKeeper.Data/Configuration/AssetConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public sealed class AssetConfiguration : IEntityTypeConfiguration<Asset>
{
public void Configure(EntityTypeBuilder<Asset> builder)
{
builder.HasKey(asset => asset.Id);
builder.HasFlakeId(asset => asset.Id);
builder.ToTable("Assets");
builder.HasShadowKey();
}
}
3 changes: 2 additions & 1 deletion SightKeeper.Data/Configuration/DataSetConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public sealed class DataSetConfiguration : IEntityTypeConfiguration<DataSet>
{
public void Configure(EntityTypeBuilder<DataSet> builder)
{
builder.HasShadowKey();
builder.HasKey(dataSet => dataSet.Id);
builder.HasFlakeId(dataSet => dataSet.Id);
builder.HasIndex(dataSet => dataSet.Name).IsUnique();
builder.HasOne(dataSet => dataSet.ScreenshotsLibrary).WithOne(library => library.DataSet).HasPrincipalKey<DataSet>();
builder.HasOne(dataSet => dataSet.WeightsLibrary).WithOne(library => library.DataSet).HasPrincipalKey<DataSet>();
Expand Down
3 changes: 2 additions & 1 deletion SightKeeper.Data/Configuration/DetectorItemConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ public sealed class DetectorItemConfiguration : IEntityTypeConfiguration<Detecto
{
public void Configure(EntityTypeBuilder<DetectorItem> builder)
{
builder.HasKey(item => item.Id);
builder.HasFlakeId(item => item.Id);
builder.ToTable("DetectorItems");
builder.HasShadowKey();
builder.OwnsOne(item => item.Bounding, boundingBuilder =>
{
boundingBuilder.Property(bounding => bounding.Left).HasColumnName("BoundingLeft");
Expand Down
3 changes: 2 additions & 1 deletion SightKeeper.Data/Configuration/GameConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public sealed class GameConfiguration : IEntityTypeConfiguration<Game>
{
public void Configure(EntityTypeBuilder<Game> builder)
{
builder.HasShadowKey();
builder.HasKey(game => game.Id);
builder.HasFlakeId(game => game.Id);
}
}
3 changes: 2 additions & 1 deletion SightKeeper.Data/Configuration/ImageConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public sealed class ImageConfiguration : IEntityTypeConfiguration<Image>
{
public void Configure(EntityTypeBuilder<Image> builder)
{
builder.HasShadowKey();
builder.HasKey(image => image.Id);
builder.HasFlakeId(image => image.Id);
builder.ToTable("Images");
}
}
3 changes: 2 additions & 1 deletion SightKeeper.Data/Configuration/ItemClassConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public sealed class ItemClassConfiguration : IEntityTypeConfiguration<ItemClass>
{
public void Configure(EntityTypeBuilder<ItemClass> builder)
{
builder.HasKey(itemClass => itemClass.Id);
builder.HasFlakeId(itemClass => itemClass.Id);
builder.ToTable("ItemClasses");
builder.HasShadowKey();
}
}
3 changes: 2 additions & 1 deletion SightKeeper.Data/Configuration/ProfileConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public sealed class ProfileConfiguration : IEntityTypeConfiguration<Profile>
{
public void Configure(EntityTypeBuilder<Profile> builder)
{
builder.HasShadowKey();
builder.HasKey(profile => profile.Id);
builder.HasFlakeId(profile => profile.Id);
builder.HasMany(profile => profile.ItemClasses).WithOne().IsRequired();
builder.HasIndex(profile => profile.Name).IsUnique();
builder.Navigation(profile => profile.Weights).AutoInclude();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public sealed class ProfileItemClassConfiguration : IEntityTypeConfiguration<Pro
{
public void Configure(EntityTypeBuilder<ProfileItemClass> builder)
{
builder.HasShadowKey();
builder.HasKey(profileItemClass => profileItemClass.Id);
builder.HasFlakeId(profileItemClass => profileItemClass.Id);
builder.ToTable("ProfileItemClasses");
builder.HasIndex("ProfileId", nameof(ProfileItemClass.Index)).IsUnique();
builder.HasIndex("ProfileId", "ItemClassId").IsUnique();
Expand Down
3 changes: 2 additions & 1 deletion SightKeeper.Data/Configuration/ScreenshotConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ public sealed class ScreenshotConfiguration : IEntityTypeConfiguration<Screensho
{
public void Configure(EntityTypeBuilder<Screenshot> builder)
{
builder.HasKey(screenshot => screenshot.Id);
builder.HasFlakeId(screenshot => screenshot.Id);
builder.ToTable("Screenshots");
builder.HasShadowKey();
builder.HasOne(screenshot => screenshot.Asset).WithOne(asset => asset.Screenshot).HasPrincipalKey<Screenshot>();
builder.HasOne(screenshot => screenshot.Image).WithOne().HasPrincipalKey<Screenshot>().IsRequired();
builder.Navigation(screenshot => screenshot.Asset).AutoInclude();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public sealed class ScreenshotsLibraryConfiguration : IEntityTypeConfiguration<S
{
public void Configure(EntityTypeBuilder<ScreenshotsLibrary> builder)
{
builder.HasShadowKey();
builder.HasKey(library => library.Id);
builder.HasFlakeId(library => library.Id);
builder.ToTable("ScreenshotsLibraries");
}
}
3 changes: 2 additions & 1 deletion SightKeeper.Data/Configuration/WeightsConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public sealed class WeightsConfiguration : IEntityTypeConfiguration<Weights>
{
public void Configure(EntityTypeBuilder<Weights> builder)
{
builder.HasShadowKey();
builder.HasKey(weights => weights.Id);
builder.HasFlakeId(weights => weights.Id);
builder
.HasMany(weights => weights.Assets)
.WithMany()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ public sealed class WeightsLibraryConfiguration : IEntityTypeConfiguration<Weigh
{
public void Configure(EntityTypeBuilder<WeightsLibrary> builder)
{
builder.HasKey(library => library.Id);
builder.HasFlakeId(library => library.Id);
builder.ToTable("WeightsLibraries");
builder.HasShadowKey();
builder.Navigation(library => library.DataSet).AutoInclude();
builder.Navigation(library => library.Weights).AutoInclude();
}
Expand Down
13 changes: 8 additions & 5 deletions SightKeeper.Data/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System.Linq.Expressions;
using FlakeId;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace SightKeeper.Data;

public static class Extensions
{
private const string IdPropertyName = "Id";

public static EntityTypeBuilder<TEntity> HasShadowKey<TEntity>(this EntityTypeBuilder<TEntity> builder) where TEntity : class
public static EntityTypeBuilder<TEntity> HasFlakeId<TEntity>(this EntityTypeBuilder<TEntity> builder, Expression<Func<TEntity, Id>> propertyExpression) where TEntity : class
{
builder.Property<long>(IdPropertyName);
builder.HasKey(IdPropertyName);
builder.Property(propertyExpression)
.HasValueGenerator<FlakeIdGenerator>()
.HasConversion<long>(id => id, number => new Id(number));
return builder;
}

Expand Down
11 changes: 11 additions & 0 deletions SightKeeper.Data/FlakeIdGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using FlakeId;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.ValueGeneration;

namespace SightKeeper.Data;

public sealed class FlakeIdGenerator : ValueGenerator<Id>
{
public override bool GeneratesTemporaryValues => false;
public override Id Next(EntityEntry entry) => Id.Create();
}
1 change: 1 addition & 0 deletions SightKeeper.Data/SightKeeper.Data.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<PackageReference Include="DynamicData" Version="7.14.2" />
<PackageReference Include="FlakeId" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0-preview.6.23329.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="8.0.0-preview.6.23329.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.0-preview.6.23329.4" />
Expand Down
4 changes: 3 additions & 1 deletion SightKeeper.Domain.Model/Common/Asset.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using SightKeeper.Domain.Model.Detector;
using FlakeId;
using SightKeeper.Domain.Model.Detector;
using SightKeeper.Domain.Model.Exceptions;

namespace SightKeeper.Domain.Model.Common;

public sealed class Asset
{
public Id Id { get; private set; }
public DataSet DataSet { get; private set; }
public Screenshot Screenshot { get; private set; }
public AssetUsage Usage { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion SightKeeper.Domain.Model/Common/Game.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
namespace SightKeeper.Domain.Model.Common;
using FlakeId;

namespace SightKeeper.Domain.Model.Common;

public sealed class Game
{
public Id Id { get; private set; }
public string Title { get; set; }
public string ProcessName { get; set; }

Expand Down
5 changes: 4 additions & 1 deletion SightKeeper.Domain.Model/Common/Image.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
namespace SightKeeper.Domain.Model.Common;
using FlakeId;

namespace SightKeeper.Domain.Model.Common;

public sealed class Image
{
public Id Id { get; private set; }
public byte[] Content { get; private set; }

public Image(byte[] content)
Expand Down
2 changes: 2 additions & 0 deletions SightKeeper.Domain.Model/Common/ItemClass.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Diagnostics.CodeAnalysis;
using CommunityToolkit.Diagnostics;
using FlakeId;
using SightKeeper.Domain.Model.Detector;

namespace SightKeeper.Domain.Model.Common;

public sealed class ItemClass
{
public Id Id { get; private set; }
public DataSet DataSet { get; private set; }
public string Name { get; set; }
public uint Color { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions SightKeeper.Domain.Model/DataSet/DataSet.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Diagnostics.CodeAnalysis;
using CommunityToolkit.Diagnostics;
using FlakeId;
using SightKeeper.Domain.Model.Common;

namespace SightKeeper.Domain.Model;

public sealed class DataSet
{
public Id Id { get; private set; }
public string Name { get; set; }
public string Description { get; set; }
public Game? Game { get; set; }
Expand Down
4 changes: 3 additions & 1 deletion SightKeeper.Domain.Model/DataSet/Weights/Weights.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using SightKeeper.Domain.Model.Common;
using FlakeId;
using SightKeeper.Domain.Model.Common;

namespace SightKeeper.Domain.Model;

public sealed class Weights
{
public Id Id { get; private set; }
public DateTime CreationDate { get; private set; }
public WeightsLibrary Library { get; private set; }
public ONNXData ONNXData { get; private set; }
Expand Down
5 changes: 4 additions & 1 deletion SightKeeper.Domain.Model/DataSet/Weights/WeightsData.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
namespace SightKeeper.Domain.Model;
using FlakeId;

namespace SightKeeper.Domain.Model;

public abstract class WeightsData
{
public Id Id { get; private set; }
public byte[] Content { get; private set; }

protected WeightsData(byte[] content)
Expand Down
2 changes: 2 additions & 0 deletions SightKeeper.Domain.Model/DataSet/Weights/WeightsLibrary.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using CommunityToolkit.Diagnostics;
using FlakeId;
using SightKeeper.Domain.Model.Common;

namespace SightKeeper.Domain.Model;

public sealed class WeightsLibrary
{
public Id Id { get; private set; }
public DataSet DataSet { get; private set; }
public IReadOnlyCollection<Weights> Weights => _weights;

Expand Down
4 changes: 3 additions & 1 deletion SightKeeper.Domain.Model/Detector/DetectorItem.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using SightKeeper.Domain.Model.Common;
using FlakeId;
using SightKeeper.Domain.Model.Common;

namespace SightKeeper.Domain.Model.Detector;

public sealed class DetectorItem
{
public Id Id { get; private set; }
public Asset Asset { get; private set; }
public ItemClass ItemClass { get; set; }
public Bounding Bounding { get; private set; }
Expand Down
2 changes: 2 additions & 0 deletions SightKeeper.Domain.Model/Profile/Profile.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using CommunityToolkit.Diagnostics;
using FlakeId;
using SightKeeper.Domain.Model.Common;

namespace SightKeeper.Domain.Model;

public sealed class Profile
{
public Id Id { get; private set; }
public string Name { get; set; }
public string Description { get; set; }

Expand Down
4 changes: 3 additions & 1 deletion SightKeeper.Domain.Model/Profile/ProfileItemClass.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using SightKeeper.Domain.Model.Common;
using FlakeId;
using SightKeeper.Domain.Model.Common;

namespace SightKeeper.Domain.Model;

public sealed class ProfileItemClass
{
public Id Id { get; private set; }
public ItemClass ItemClass { get; private set; }
public byte Index { get; internal set; }
public ItemClassActivationCondition ActivationCondition { get; set; }
Expand Down
4 changes: 3 additions & 1 deletion SightKeeper.Domain.Model/Screenshots/Screenshot.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using SightKeeper.Domain.Model.Common;
using FlakeId;
using SightKeeper.Domain.Model.Common;

namespace SightKeeper.Domain.Model;

public sealed class Screenshot
{
public Id Id { get; private set; }
public Image Image { get; private set; }
public DateTime CreationDate { get; private set; }
public Asset? Asset { get; internal set; }
Expand Down
2 changes: 2 additions & 0 deletions SightKeeper.Domain.Model/Screenshots/ScreenshotsLibrary.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Reactive.Linq;
using System.Reactive.Subjects;
using CommunityToolkit.Diagnostics;
using FlakeId;

namespace SightKeeper.Domain.Model;

public sealed class ScreenshotsLibrary
{
public Id Id { get; private set; }
public IObservable<Screenshot> ScreenshotAdded => _screenshotAdded.AsObservable();
private readonly Subject<Screenshot> _screenshotAdded = new();
public IObservable<Screenshot> ScreenshotRemoved => _screenshotRemoved.AsObservable();
Expand Down
1 change: 1 addition & 0 deletions SightKeeper.Domain.Model/SightKeeper.Domain.Model.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<PackageReference Include="CommunityToolkit.Diagnostics" Version="8.2.0" />
<PackageReference Include="FlakeId" Version="1.1.2" />
<PackageReference Include="System.Reactive" Version="6.0.0" />
</ItemGroup>

Expand Down
8 changes: 7 additions & 1 deletion SightKeeper.Services/ProfilesObservableRepository.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Reactive.Disposables;
using CommunityToolkit.Diagnostics;
using DynamicData;
using Serilog;
using SightKeeper.Commons;
using SightKeeper.Domain.Model;
using SightKeeper.Domain.Services;
Expand Down Expand Up @@ -35,6 +36,11 @@ private void LoadProfiles(ProfilesDataAccess profilesDataAccess)
{
profilesDataAccess.LoadProfiles().Subscribe(AddProfile);
}
private void AddProfile(Profile profile) => _profiles.Add(profile);
private void AddProfile(Profile profile)
{
Log.Debug("Profile Id: {Id}", profile.Id);
_profiles.Add(profile);
}

private void RemoveProfile(Profile profile) => Guard.IsTrue(_profiles.Remove(profile));
}

0 comments on commit 6c92bf5

Please sign in to comment.