Skip to content

Commit

Permalink
Adjustments to the behavior of tracking entities in the EntityRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
tsutomi committed Oct 25, 2023
1 parent 9bfb1fe commit 428aea2
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 3 deletions.
19 changes: 18 additions & 1 deletion src/Deveel.Repository.EntityFramework/Data/EntityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ internal EntityRepository(DbContext context, ITenantInfo? tenantInfo, ILogger? l
/// </summary>
protected virtual DbSet<TEntity> Entities => Context.Set<TEntity>();

/// <summary>
/// Gets a value indicating if the repository is tracking the changes
/// to the entities returned by the queries.
/// </summary>
protected bool IsTrackingChanges => Entities.Local != null ||
Context.ChangeTracker.QueryTrackingBehavior != QueryTrackingBehavior.NoTracking;

/// <summary>
/// Gets the information about the tenant that the repository is using to access the data.
/// </summary>
Expand Down Expand Up @@ -324,7 +331,17 @@ public virtual async Task<bool> UpdateAsync(TEntity entity, CancellationToken ca

Logger.TraceUpdatingEntity(typeof(TEntity), entityId, TenantId);

var entry = Context.Entry(entity);
if (!IsTrackingChanges) {
var existing = await FindByKeyAsync(entityId, cancellationToken);
if (existing == null) {
Logger.WarnEntityNotFound(typeof(TEntity), entityId, TenantId);
return false;
}

var entry = Context.Entry(existing);
entry.CurrentValues.SetValues(entity);
entry.State = EntityState.Modified;
}

var count = await Context.SaveChangesAsync(cancellationToken);

Expand Down
1 change: 1 addition & 0 deletions src/Deveel.Repository.Manager/Data/EntityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using System.ComponentModel.DataAnnotations;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;

using Deveel.Data.Caching;

Expand Down
9 changes: 9 additions & 0 deletions src/Deveel.Repository.Manager/OperationResult_1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ public static implicit operator OperationResult<TValue>(TValue value)
public static implicit operator OperationResult<TValue>(OperationResult result)
=> new(result.ResultType, default, result.Error);

/// <summary>
/// Implicitly converts the given <paramref name="result"/>
/// to the value returned by the operation.
/// </summary>
/// <param name="result">
/// The operation result that is to be converted.
/// </param>
public static implicit operator TValue?(OperationResult<TValue> result) => result.Value;

/// <summary>
/// Creates a result that indicates a successful operation.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected override Task RemoveRelationshipAsync(DbPerson person, DbRelationship

protected override void ConfigureServices(IServiceCollection services) {
services.AddDbContext<DbContext, PersonDbContext>(builder => {
builder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTrackingWithIdentityResolution);
builder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
builder.UseSqlite(sql.Connection, sqlite => {
sqlite.UseNetTopologySuite();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public async Task AddRangeOfEntities_InvalidEmail() {
}

[Fact]
public async Task UpdateEntity() {
public virtual async Task UpdateEntity() {
var person = People.Random()!;

var copy = new Person {
Expand All @@ -152,6 +152,7 @@ public async Task UpdateEntity() {
};

var result = await Manager.UpdateAsync(copy);

Assert.False(result.IsValidationError());
Assert.True(result.IsSuccess());

Expand Down
31 changes: 31 additions & 0 deletions test/Deveel.Repository.Manager.XUnit/OperationResultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,37 @@ public static void ImplicitConvertResultValue_Success() {
Assert.Equal(value, result.Value);
}

[Fact]
public static void ImplicitConvertResultValue_ToValue() {
var success = OperationResult<int>.Success(22);

int value = success;

Assert.Equal(22, value);
}

[Fact]
public static void ImplicitConvertResultValue_FromSuccessOperationResult() {
var success = OperationResult.Success;

var result = (OperationResult<int>) success;

Assert.Equal(OperationResultType.Success, result.ResultType);
Assert.Equal(default, result.Value);
Assert.Null(result.Error);
}

[Fact]
public static void ImplicitConvertResultValue_FromFailOperationResult() {
var fail = OperationResult.Fail("ERROR_CODE", "An error has occurred");

var result = (OperationResult<int>)fail;

Assert.Equal(OperationResultType.Error, result.ResultType);
Assert.Equal(default, result.Value);
Assert.NotNull(result.Error);
}

[Fact]
public static async Task MapValueAsync() {
var result = OperationResult<string>.Success("test");
Expand Down

0 comments on commit 428aea2

Please sign in to comment.