Skip to content

Commit

Permalink
Allowing to override OnCreating and OnUpdating methods in MongoFramew…
Browse files Browse the repository at this point in the history
…ork Repository
  • Loading branch information
tsutomi committed May 4, 2023
1 parent ca509c6 commit f5bdfe1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
14 changes: 14 additions & 0 deletions src/Deveel.Repsotiory.MongoFramework/Data/MongoRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ public async Task DropAsync(CancellationToken cancellationToken = default) {

#region Create

protected virtual TEntity OnCreating(TEntity entity) {
return entity;
}

public async Task<string> CreateAsync(TEntity entity, CancellationToken cancellationToken = default) {
ThrowIfDisposed();
cancellationToken.ThrowIfCancellationRequested();
Expand All @@ -230,6 +234,8 @@ public async Task<string> CreateAsync(TEntity entity, CancellationToken cancella
}

try {
entity = OnCreating(entity);

DbSet.Add(entity);
await DbSet.Context.SaveChangesAsync(cancellationToken);

Expand Down Expand Up @@ -260,6 +266,8 @@ public async Task<IList<string>> CreateAsync(IEnumerable<TEntity> entities, Canc
cancellationToken.ThrowIfCancellationRequested();

try {
entities = entities.Select(OnCreating);

DbSet.AddRange(entities);
await DbSet.Context.SaveChangesAsync(cancellationToken);

Expand All @@ -275,6 +283,10 @@ public async Task<IList<string>> CreateAsync(IEnumerable<TEntity> entities, Canc

#region Update

protected TEntity OnUpdating(TEntity entity) {
return entity;
}

public async Task<bool> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default) {
if (entity is null)
throw new ArgumentNullException(nameof(entity));
Expand All @@ -298,6 +310,8 @@ public async Task<bool> UpdateAsync(TEntity entity, CancellationToken cancellati
if (entry == null || entry.State == EntityEntryState.Deleted)
return false;

entity = OnUpdating(entity);

DbSet.Update(entity);
var updated = entry.State == EntityEntryState.Updated;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@

using Microsoft.Extensions.Logging.Abstractions;
using EphemeralMongo;

using Mongo2Go;
using Microsoft.Extensions.Logging.Abstractions;

using MongoDB.Driver;

namespace Deveel.Data {
public class MongoFrameworkTestFixture : IDisposable {
private MongoDbRunner mongo;
private IMongoRunner mongo;

public MongoFrameworkTestFixture() {
mongo = MongoDbRunner.Start(logger: NullLogger.Instance, singleNodeReplSet: false);
mongo = MongoRunner.Run(new MongoRunnerOptions {
UseSingleNodeReplicaSet = true,
KillMongoProcessesWhenCurrentProcessExits = true
});
}

public string ConnectionString => mongo.ConnectionString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

<ItemGroup>
<PackageReference Include="Bogus" Version="34.0.2" />
<PackageReference Include="EphemeralMongo6" Version="1.1.0" />
<PackageReference Include="MartinCostello.Logging.XUnit" Version="0.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.16" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Mongo2Go" Version="3.1.3" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down

0 comments on commit f5bdfe1

Please sign in to comment.