Skip to content

Commit

Permalink
implemented Do method for the ChangesFrame to replace Update/Save/Loa…
Browse files Browse the repository at this point in the history
…d methods
  • Loading branch information
AlexNav73 committed Mar 19, 2024
1 parent 9fc3c8f commit c468d56
Show file tree
Hide file tree
Showing 20 changed files with 207 additions and 257 deletions.
8 changes: 4 additions & 4 deletions build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,28 +133,28 @@ internal partial class Build : NukeBuild
DotNetPack(s => s
.SetProject(Solution.CoreCraft)
.Apply(PackSettingsBase)
.SetVersion(MakePreviewIfNeeded("0.6.0", "0.6.7"))
.SetVersion(MakePreviewIfNeeded("0.6.0", "0.6.8"))
.SetDescription("A core library to build cross-platform and highly customizable domain models")
.AddPackageTags("Model", "Domain"));
DotNetPack(s => s
.SetProject(Solution.CoreCraft_Generators)
.Apply(PackSettingsBase)
.SetVersion(MakePreviewIfNeeded("0.6.0", "0.6.7"))
.SetVersion(MakePreviewIfNeeded("0.6.0", "0.6.8"))
.SetDescription("Roslyn Source Generators for generating domain models using 'CoreCraft' library")
.AddPackageTags("Model", "Domain", "SourceGenerator", "Generator"));
DotNetPack(s => s
.SetProject(Solution.CoreCraft_Storage_Sqlite)
.Apply(PackSettingsBase)
.SetVersion(MakePreviewIfNeeded("0.6.0", "0.6.7"))
.SetVersion(MakePreviewIfNeeded("0.6.0", "0.6.8"))
.SetDescription("SQLite storage implementation for 'CoreCraft' library")
.AddPackageTags("Model", "Domain", "SQLite"));
DotNetPack(s => s
.SetProject(Solution.CoreCraft_Storage_Json)
.Apply(PackSettingsBase)
.SetVersion(MakePreviewIfNeeded("0.3.0", "0.3.7"))
.SetVersion(MakePreviewIfNeeded("0.3.0", "0.3.8"))
.SetDescription("Json storage implementation for 'CoreCraft' library")
.AddPackageTags("Model", "Domain", "Json"));
Expand Down
51 changes: 6 additions & 45 deletions src/CoreCraft.SourceGeneration/Generators/ModelShardGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,7 @@ private void DefineChangesFrameClass(ModelShard modelShard)
code.EmptyLine();
DefineMergeMethod(modelShard);
code.EmptyLine();
ImplementUpdateMethod(modelShard);
code.EmptyLine();
ImplementSaveMethod(modelShard);
code.EmptyLine();
ImplementLoadMethod(modelShard);
ImplementDoMethod(modelShard);
code.EmptyLine();
});

Expand Down Expand Up @@ -438,56 +434,21 @@ void DefineMergeMethod(ModelShard modelShard)
});
}

void ImplementUpdateMethod(ModelShard modelShard)
{
code.WriteLine($"public void Update(IRepository repository)");
code.Block(() =>
{
foreach (var collection in modelShard.Collections)
{
code.WriteLine($"repository.Update({collection.Name});");
}
code.EmptyLine();
foreach (var relation in modelShard.Relations)
{
code.WriteLine($"repository.Update({relation.Name});");
}
});
}

void ImplementSaveMethod(ModelShard modelShard)
{
code.WriteLine($"public void Save(long changeId, IHistoryRepository repository)");
code.Block(() =>
{
foreach (var collection in modelShard.Collections)
{
code.WriteLine($"repository.Save(changeId, {collection.Name});");
}
code.EmptyLine();
foreach (var relation in modelShard.Relations)
{
code.WriteLine($"repository.Save(changeId, {relation.Name});");
}
});
}

void ImplementLoadMethod(ModelShard modelShard)
void ImplementDoMethod(ModelShard modelShard)
{
code.WriteLine($"public void Load(long changeId, IHistoryRepository repository)");
code.WriteLine($"public void Do<T>(T operation)");
code.WithIndent(c => c.WriteLine("where T : IChangesFrameOperation"));
code.Block(() =>
{
foreach (var collection in modelShard.Collections)
{
code.WriteLine($"repository.Load(changeId, {collection.Name});");
code.WriteLine($"operation.OnCollection({collection.Name});");
}
code.EmptyLine();
foreach (var relation in modelShard.Relations)
{
code.WriteLine($"repository.Load(changeId, {relation.Name});");
code.WriteLine($"operation.OnRelation({relation.Name});");
}
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/CoreCraft.Storage.Json/JsonStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using CoreCraft.Persistence;
using CoreCraft.Persistence.History;
using CoreCraft.Persistence.Lazy;
using CoreCraft.Persistence.Operations;
using CoreCraft.Storage.Json.Model;
using Newtonsoft.Json;

Expand Down Expand Up @@ -43,7 +44,7 @@ public void Update(IEnumerable<IChangesFrame> modelChanges)

foreach (var change in modelChanges.Cast<IChangesFrameEx>())
{
change.Update(repository);
change.Do(new UpdateChangesFrameOperation(repository));
}

_jsonFileHandler.WriteModelShardsToFile(_path, shards, _settings);
Expand Down
30 changes: 15 additions & 15 deletions src/CoreCraft.Storage.Sqlite/SqliteRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text.Json.Serialization.Metadata;
using CoreCraft.ChangesTracking;
using CoreCraft.Core;
using CoreCraft.Persistence.Operations;
using Microsoft.Data.Sqlite;
using static CoreCraft.Storage.Sqlite.QueryBuilder;

Expand Down Expand Up @@ -38,8 +39,7 @@ public IDbTransaction BeginTransaction()

public int GetDatabaseVersion()
{
using var command = _connection.CreateCommand();
command.CommandText = QueryBuilder.Migrations.GetDatabaseVersion;
using var command = CreateCommand(QueryBuilder.Migrations.GetDatabaseVersion);

Log(command);

Expand All @@ -50,17 +50,19 @@ public int GetDatabaseVersion()

public void SetDatabaseVersion(int version)
{
using var command = _connection.CreateCommand();
command.CommandText = QueryBuilder.Migrations.SetDatabaseVersion(version);
using var command = CreateCommand(QueryBuilder.Migrations.SetDatabaseVersion(version));

Log(command);

command.ExecuteNonQuery();
}

public void ExecuteNonQuery(string query)
{
using var command = _connection.CreateCommand();
command.CommandText = query;
using var command = CreateCommand(query);

Log(command);

command.ExecuteNonQuery();
}

Expand Down Expand Up @@ -287,8 +289,7 @@ public void Load<TParent, TChild>(IMutableRelation<TParent, TChild> relation, IE
return;
}

using var command = _connection.CreateCommand();
command.CommandText = Relations.Select(relation.Info);
using var command = CreateCommand(Relations.Select(relation.Info));

Log(command);

Expand All @@ -311,6 +312,8 @@ public void Load<TEntity, TProperties>(long changeId, ICollectionChangeSet<TEnti
{
using var command = CreateCommand(History.SelectCollectionTable(changeId, InferName(changes.Info)));

Check warning on line 313 in src/CoreCraft.Storage.Sqlite/SqliteRepository.cs

View check run for this annotation

Codecov / codecov/patch

src/CoreCraft.Storage.Sqlite/SqliteRepository.cs#L313

Added line #L313 was not covered by tests

Log(command);

Check warning on line 315 in src/CoreCraft.Storage.Sqlite/SqliteRepository.cs

View check run for this annotation

Codecov / codecov/patch

src/CoreCraft.Storage.Sqlite/SqliteRepository.cs#L315

Added line #L315 was not covered by tests

using var reader = command.ExecuteReader();

Check warning on line 317 in src/CoreCraft.Storage.Sqlite/SqliteRepository.cs

View check run for this annotation

Codecov / codecov/patch

src/CoreCraft.Storage.Sqlite/SqliteRepository.cs#L317

Added line #L317 was not covered by tests
while (reader.Read())
{
Expand All @@ -320,8 +323,6 @@ public void Load<TEntity, TProperties>(long changeId, ICollectionChangeSet<TEnti
var newProperty = Deserialize<TProperties>(reader, 3);

Check warning on line 323 in src/CoreCraft.Storage.Sqlite/SqliteRepository.cs

View check run for this annotation

Codecov / codecov/patch

src/CoreCraft.Storage.Sqlite/SqliteRepository.cs#L320-L323

Added lines #L320 - L323 were not covered by tests

changes.Add(action, entity!, oldProperty, newProperty);

Check warning on line 325 in src/CoreCraft.Storage.Sqlite/SqliteRepository.cs

View check run for this annotation

Codecov / codecov/patch

src/CoreCraft.Storage.Sqlite/SqliteRepository.cs#L325

Added line #L325 was not covered by tests

Log(command);
}
}

Check warning on line 327 in src/CoreCraft.Storage.Sqlite/SqliteRepository.cs

View check run for this annotation

Codecov / codecov/patch

src/CoreCraft.Storage.Sqlite/SqliteRepository.cs#L327

Added line #L327 was not covered by tests

Expand All @@ -331,6 +332,8 @@ public void Load<TParent, TChild>(long changeId, IRelationChangeSet<TParent, TCh
{
using var command = CreateCommand(History.SelectRelationTable(changeId, InferName(changes.Info)));

Check warning on line 333 in src/CoreCraft.Storage.Sqlite/SqliteRepository.cs

View check run for this annotation

Codecov / codecov/patch

src/CoreCraft.Storage.Sqlite/SqliteRepository.cs#L333

Added line #L333 was not covered by tests

Log(command);

Check warning on line 335 in src/CoreCraft.Storage.Sqlite/SqliteRepository.cs

View check run for this annotation

Codecov / codecov/patch

src/CoreCraft.Storage.Sqlite/SqliteRepository.cs#L335

Added line #L335 was not covered by tests

using var reader = command.ExecuteReader();

Check warning on line 337 in src/CoreCraft.Storage.Sqlite/SqliteRepository.cs

View check run for this annotation

Codecov / codecov/patch

src/CoreCraft.Storage.Sqlite/SqliteRepository.cs#L337

Added line #L337 was not covered by tests
while (reader.Read())
{
Expand All @@ -339,8 +342,6 @@ public void Load<TParent, TChild>(long changeId, IRelationChangeSet<TParent, TCh
var child = Deserialize<TChild>(reader, 2);

Check warning on line 342 in src/CoreCraft.Storage.Sqlite/SqliteRepository.cs

View check run for this annotation

Codecov / codecov/patch

src/CoreCraft.Storage.Sqlite/SqliteRepository.cs#L340-L342

Added lines #L340 - L342 were not covered by tests

changes.Add(action, parent!, child!);

Check warning on line 344 in src/CoreCraft.Storage.Sqlite/SqliteRepository.cs

View check run for this annotation

Codecov / codecov/patch

src/CoreCraft.Storage.Sqlite/SqliteRepository.cs#L344

Added line #L344 was not covered by tests

Log(command);
}
}

Check warning on line 346 in src/CoreCraft.Storage.Sqlite/SqliteRepository.cs

View check run for this annotation

Codecov / codecov/patch

src/CoreCraft.Storage.Sqlite/SqliteRepository.cs#L346

Added line #L346 was not covered by tests

Expand All @@ -360,7 +361,7 @@ public IEnumerable<IModelChanges> LoadChanges(IEnumerable<IModelShard> shards)
foreach (var shard in shards.Cast<IFrameFactory>())
{
var change = (IChangesFrameEx)shard.Create();
change.Load(timestamp, this);
change.Do(new LoadChangesFrameOperation(timestamp, this));

Check warning on line 364 in src/CoreCraft.Storage.Sqlite/SqliteRepository.cs

View check run for this annotation

Codecov / codecov/patch

src/CoreCraft.Storage.Sqlite/SqliteRepository.cs#L363-L364

Added lines #L363 - L364 were not covered by tests
if (change.HasChanges())
{
modelChanges.AddOrGet(change);

Check warning on line 367 in src/CoreCraft.Storage.Sqlite/SqliteRepository.cs

View check run for this annotation

Codecov / codecov/patch

src/CoreCraft.Storage.Sqlite/SqliteRepository.cs#L367

Added line #L367 was not covered by tests
Expand Down Expand Up @@ -456,8 +457,7 @@ private void Delete<TEntity>(SqliteCommand command, TEntity entity)

private bool Exists(string name)
{
using var command = _connection.CreateCommand();
command.CommandText = IfTableExists(name);
using var command = CreateCommand(IfTableExists(name));

Log(command);

Expand Down
3 changes: 2 additions & 1 deletion src/CoreCraft.Storage.Sqlite/SqliteStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using CoreCraft.Persistence;
using CoreCraft.Persistence.History;
using CoreCraft.Persistence.Lazy;
using CoreCraft.Persistence.Operations;
using CoreCraft.Storage.Sqlite.Migrations;
using System.Data;

Expand Down Expand Up @@ -48,7 +49,7 @@ public void Update(IEnumerable<IChangesFrame> modelChanges)
{
foreach (var change in modelChanges.Cast<IChangesFrameEx>())
{
change.Update(repository);
change.Do(new UpdateChangesFrameOperation(repository));
}
});
}
Expand Down
35 changes: 4 additions & 31 deletions src/CoreCraft/ChangesTracking/IChangesFrameEx.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using CoreCraft.Persistence;
using CoreCraft.Persistence.History;

namespace CoreCraft.ChangesTracking;
namespace CoreCraft.ChangesTracking;

/// <summary>
/// An extension of the <see cref="IChangesFrame"/> which used internally in the <see cref="DomainModel"/>
Expand Down Expand Up @@ -54,32 +51,8 @@ public interface IChangesFrameEx : IChangesFrame
IChangesFrame Invert();

/// <summary>
/// Saves the changes frame using the provided repository.
/// </summary>
/// <param name="repository">The repository used to save the changes frame.</param>
void Update(IRepository repository);

/// <summary>
/// Saves the changes stored in this frame to the specified repository.
/// TODO: write documentation
/// </summary>
/// <param name="changeId">A unique identifier for the change set.</param>
/// <param name="repository">The <see cref="IHistoryRepository" /> instance used to persist the changes.</param>
/// <remarks>
/// This method persists the changes tracked within this <see cref="IChangesFrame"/> instance to the history storage using the provided `repository`.
/// - The `changeId` parameter allows for associating the changes with a specific event or action.
/// - The `repository` parameter is an <see cref="IHistoryRepository" /> instance responsible for handling the storage and retrieval of history data.
/// </remarks>
void Save(long changeId, IHistoryRepository repository);

/// <summary>
/// Loads changes history from the specified repository for the given change identifier.
/// </summary>
/// <param name="changeId">A unique identifier for the change set to load.</param>
/// <param name="repository">The <see cref="IHistoryRepository" /> instance used to retrieve the changes.</param>
/// <remarks>
/// This method retrieves changes associated with the provided `changeId` from the storage using the specified `repository`.
/// - The `changeId` parameter specifies the unique identifier of the change set to be loaded.
/// - The `repository` parameter is an <see cref="IHistoryRepository" /> instance responsible for providing access to changes.
/// </remarks>
void Load(long changeId, IHistoryRepository repository);
/// <param name="operation"></param>
void Do<T>(T operation) where T : IChangesFrameOperation;
}
29 changes: 29 additions & 0 deletions src/CoreCraft/ChangesTracking/IChangesFrameOperation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace CoreCraft.ChangesTracking;

/// <summary>
/// TODO: write documentation
/// </summary>
public interface IChangesFrameOperation
{
/// <summary>
/// </summary>
/// <typeparam name="TEntity">A type of an entity</typeparam>
/// <typeparam name="TProperties">A type of properties</typeparam>
/// <param name="collection">A base collection which will be wrapped in this method</param>
/// <returns>A new collection with adjusted behavior</returns>
void OnCollection<TEntity, TProperties>(
ICollectionChangeSet<TEntity, TProperties> collection)
where TEntity : Entity
where TProperties : Properties;

/// <summary>
/// </summary>
/// <typeparam name="TParent">A type of a parent entity</typeparam>
/// <typeparam name="TChild">A type of a child entity</typeparam>
/// <param name="relation">A base relation which will be wrapped in this method</param>
/// <returns>A new relation with adjusted behavior</returns>
void OnRelation<TParent, TChild>(
IRelationChangeSet<TParent, TChild> relation)
where TParent : Entity
where TChild : Entity;
}
2 changes: 1 addition & 1 deletion src/CoreCraft/ChangesTracking/IModelChanges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ bool TryGetFrame<T>(out T frame)
IModelChanges Invert();

/// <summary>
///
/// TODO: write documentation
/// </summary>
/// <param name="repository"></param>
void Save(IHistoryRepository repository);
Expand Down
3 changes: 2 additions & 1 deletion src/CoreCraft/ChangesTracking/ModelChanges.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using CoreCraft.Persistence.History;
using CoreCraft.Persistence.Operations;

namespace CoreCraft.ChangesTracking;

Expand Down Expand Up @@ -83,7 +84,7 @@ public void Save(IHistoryRepository repository)
{
foreach (var frame in _frames)
{
frame.Save(_timestamp, repository);
frame.Do(new SaveChangesFrameOperation(_timestamp, repository));

Check warning on line 87 in src/CoreCraft/ChangesTracking/ModelChanges.cs

View check run for this annotation

Codecov / codecov/patch

src/CoreCraft/ChangesTracking/ModelChanges.cs#L87

Added line #L87 was not covered by tests
}
}

Expand Down
33 changes: 33 additions & 0 deletions src/CoreCraft/Persistence/Operations/LoadChangesFrameOperation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Diagnostics.CodeAnalysis;
using CoreCraft.ChangesTracking;
using CoreCraft.Persistence.History;

namespace CoreCraft.Persistence.Operations;

/// <summary>
/// TODO: write documentation
/// </summary>
/// <param name="timestamp"></param>
/// <param name="repository"></param>
[ExcludeFromCodeCoverage]
public readonly struct LoadChangesFrameOperation(long timestamp, IHistoryRepository repository) : IChangesFrameOperation
{
private readonly long _timestamp = timestamp;
private readonly IHistoryRepository _repository = repository;

/// <inheritdoc />
public void OnCollection<TEntity, TProperties>(ICollectionChangeSet<TEntity, TProperties> collection)
where TEntity : Entity
where TProperties : Properties
{
_repository.Load(_timestamp, collection);
}

/// <inheritdoc />
public void OnRelation<TParent, TChild>(IRelationChangeSet<TParent, TChild> relation)
where TParent : Entity
where TChild : Entity
{
_repository.Load(_timestamp, relation);
}
}
Loading

0 comments on commit c468d56

Please sign in to comment.