Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducting synchronous triggers. #161

Merged
merged 4 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ namespace EntityFrameworkCore.Triggered.Benchmarks.Triggers
{
public class SetStudentRegistrationDateTrigger : IBeforeSaveTrigger<Student>
{
public Task BeforeSave(ITriggerContext<Student> context, CancellationToken cancellationToken)
public void BeforeSave(ITriggerContext<Student> context)
{
context.Entity.RegistrationDate = DateTimeOffset.Now;
return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public SignStudentUpForMandatoryCourses(TriggeredApplicationContext applicationC
_applicationContext = applicationContext;
}

public Task BeforeSave(ITriggerContext<Student> context, CancellationToken cancellationToken)
public void BeforeSave(ITriggerContext<Student> context)
{
var mandatoryCourses = _applicationContext.Courses.Where(x => x.IsMandatory).ToList();

Expand All @@ -24,8 +24,6 @@ public Task BeforeSave(ITriggerContext<Student> context, CancellationToken cance
StudentId = context.Entity.Id
});
}

return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Threading;
using System.Threading.Tasks;

namespace EntityFrameworkCore.Triggered
{
public interface IAfterSaveAsyncTrigger<TEntity>
where TEntity : class
{
Task AfterSaveAsync(ITriggerContext<TEntity> context, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace EntityFrameworkCore.Triggered
{
public interface IAfterSaveFailedAsyncTrigger<TEntity>
where TEntity : class
{
Task AfterSaveFailedAsync(ITriggerContext<TEntity> context, Exception exception, CancellationToken cancellationToken);
}


}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace EntityFrameworkCore.Triggered
{
public interface IAfterSaveFailedTrigger<TEntity>
where TEntity : class
{
Task AfterSaveFailed(ITriggerContext<TEntity> context, Exception exception, CancellationToken cancellationToken);
void AfterSaveFailed(ITriggerContext<TEntity> context, Exception exception);
}


}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System.Threading;
using System.Threading.Tasks;

namespace EntityFrameworkCore.Triggered
namespace EntityFrameworkCore.Triggered
{
public interface IAfterSaveTrigger<TEntity>
where TEntity : class
{
Task AfterSave(ITriggerContext<TEntity> context, CancellationToken cancellationToken);
void AfterSave(ITriggerContext<TEntity> context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Threading;
using System.Threading.Tasks;

namespace EntityFrameworkCore.Triggered
{
public interface IBeforeSaveAsyncTrigger<in TEntity>
where TEntity : class
{
Task BeforeSaveAsync(ITriggerContext<TEntity> context, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System.Threading;
using System.Threading.Tasks;

namespace EntityFrameworkCore.Triggered
namespace EntityFrameworkCore.Triggered
{
public interface IBeforeSaveTrigger<in TEntity>
where TEntity : class
{
Task BeforeSave(ITriggerContext<TEntity> context, CancellationToken cancellationToken);
void BeforeSave(ITriggerContext<TEntity> context);
}
}
43 changes: 33 additions & 10 deletions src/EntityFrameworkCore.Triggered.Abstractions/ITriggerSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,55 @@ public interface ITriggerSession : IDisposable
/// Discoveres any new pending changes in the DbContext
/// </summary>
void DiscoverChanges();
Task RaiseBeforeSaveStartingTriggers(CancellationToken cancellationToken = default);
Task RaiseBeforeSaveCompletedTriggers(CancellationToken cancellationToken = default);
Task RaiseAfterSaveFailedStartingTriggers(Exception exception, CancellationToken cancellationToken = default);
Task RaiseAfterSaveFailedCompletedTriggers(Exception exception, CancellationToken cancellationToken = default);
Task RaiseAfterSaveStartingTriggers(CancellationToken cancellationToken = default);
Task RaiseAfterSaveCompletedTriggers(CancellationToken cancellationToken = default);
void RaiseBeforeSaveStartingTriggers();
void RaiseBeforeSaveCompletedTriggers();
void RaiseAfterSaveFailedStartingTriggers(Exception exception);
void RaiseAfterSaveFailedCompletedTriggers(Exception exception);
void RaiseAfterSaveStartingTriggers();
void RaiseAfterSaveCompletedTriggers();
Task RaiseBeforeSaveStartingAsyncTriggers(CancellationToken cancellationToken = default);
Task RaiseBeforeSaveCompletedAsyncTriggers(CancellationToken cancellationToken = default);
Task RaiseAfterSaveFailedStartingAsyncTriggers(Exception exception, CancellationToken cancellationToken = default);
Task RaiseAfterSaveFailedCompletedAsyncTriggers(Exception exception, CancellationToken cancellationToken = default);
Task RaiseAfterSaveStartingAsyncTriggers(CancellationToken cancellationToken = default);
Task RaiseAfterSaveCompletedAsyncTriggers(CancellationToken cancellationToken = default);
/// <summary>
/// Makes a snapshot of all changes in the DbContext and invokes BeforeSaveTriggers while detecting and cascading based on the cascade settings until all changes have been processed
/// </summary>
Task RaiseBeforeSaveTriggers(CancellationToken cancellationToken = default);
void RaiseBeforeSaveTriggers();
/// <summary>
/// Makes a snapshot of all changes in the DbContext and invokes BeforeSaveTriggers while detecting and cascading based on the cascade settings until all changes have been processed
/// </summary>
Task RaiseBeforeSaveAsyncTriggers(CancellationToken cancellationToken = default);
/// <summary>
/// Makes a snapshot of all changes in the DbContext and invokes BeforeSaveTriggers while detecting and cascading based on the cascade settings until all changes have been processed
/// </summary>
/// <param name="skipDetectedChanges">Allows BeforeSaveTriggers not to include previously detected changes. Only new changes will be detected and fired upon. This is useful in case of multiple calls to RaiseBeforeSaveTriggers</param>
void RaiseBeforeSaveTriggers(bool skipDetectedChanges);
/// <summary>
/// Makes a snapshot of all changes in the DbContext and invokes BeforeSaveTriggers while detecting and cascading based on the cascade settings until all changes have been processed
/// </summary>
/// <param name="skipDetectedChanges">Allows BeforeSaveTriggers not to include previously detected changes. Only new changes will be detected and fired upon. This is useful in case of multiple calls to RaiseBeforeSaveTriggers</param>
Task RaiseBeforeSaveTriggers(bool skipDetectedChanges, CancellationToken cancellationToken = default);
Task RaiseBeforeSaveAsyncTriggers(bool skipDetectedChanges, CancellationToken cancellationToken = default);
/// <summary>
/// Captures and locks all discovered changes
/// </summary>
void CaptureDiscoveredChanges();
/// <summary>
/// Invokes AfterSaveTriggers. Calling this method expects that either RaiseBeforeSaveTriggers() or DiscoverChanges() has been called
/// </summary>
Task RaiseAfterSaveTriggers(CancellationToken cancellationToken = default);
void RaiseAfterSaveTriggers();
/// <summary>
/// Invokes AfterSaveTriggers. Calling this method expects that either RaiseBeforeSaveTriggers() or DiscoverChanges() has been called
/// </summary>
Task RaiseAfterSaveAsyncTriggers(CancellationToken cancellationToken = default);
/// <summary>
/// Invokes AfterSaveFailedTriggers. Calling this method expects that either RaiseBeforeSaveTriggers() or DiscoverChanges() has been called
/// </summary>
void RaiseAfterSaveFailedTriggers(Exception exception);
/// <summary>
/// Invokes AfterSaveFailedTriggers. Calling this method expects that either RaiseBeforeSaveTriggers() or DiscoverChanges() has been called
/// </summary>
Task RaiseAfterSaveFailedTriggers(Exception exception, CancellationToken cancellationToken = default);
Task RaiseAfterSaveFailedAsyncTriggers(Exception exception, CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Threading;
using System.Threading.Tasks;

namespace EntityFrameworkCore.Triggered.Lifecycles
{
public interface IAfterSaveCompletedAsyncTrigger
{
Task AfterSaveCompletedAsync(CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System.Threading;
using System.Threading.Tasks;

namespace EntityFrameworkCore.Triggered.Lifecycles
namespace EntityFrameworkCore.Triggered.Lifecycles
{
public interface IAfterSaveCompletedTrigger
{
Task AfterSaveCompleted(CancellationToken cancellationToken);
void AfterSaveCompleted();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace EntityFrameworkCore.Triggered.Lifecycles
{
public interface IAfterSaveFailedCompletedAsyncTrigger
{
Task AfterSaveFailedCompletedAsync(Exception exception, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace EntityFrameworkCore.Triggered.Lifecycles
{
public interface IAfterSaveFailedCompletedTrigger
{
Task AfterSaveFailedCompleted(Exception exception, CancellationToken cancellationToken);
void AfterSaveFailedCompleted(Exception exception);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace EntityFrameworkCore.Triggered.Lifecycles
{
public interface IAfterSaveFailedStartingAsyncTrigger
{
Task AfterSaveFailedStartingAsync(Exception exception, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace EntityFrameworkCore.Triggered.Lifecycles
{
public interface IAfterSaveFailedStartingTrigger
{
Task AfterSaveFailedStarting(Exception exception, CancellationToken cancellationToken);
void AfterSaveFailedStarting(Exception exception);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Threading;
using System.Threading.Tasks;

namespace EntityFrameworkCore.Triggered.Lifecycles
{
public interface IAfterSaveStartingAsyncTrigger
{
Task AfterSaveStartingAsync(CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System.Threading;
using System.Threading.Tasks;

namespace EntityFrameworkCore.Triggered.Lifecycles
namespace EntityFrameworkCore.Triggered.Lifecycles
{
public interface IAfterSaveStartingTrigger
{
Task AfterSaveStarting(CancellationToken cancellationToken);
void AfterSaveStarting();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Threading;
using System.Threading.Tasks;

namespace EntityFrameworkCore.Triggered.Lifecycles
{
public interface IBeforeSaveCompletedAsyncTrigger
{
Task BeforeSaveCompletedAsync(CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System.Threading;
using System.Threading.Tasks;

namespace EntityFrameworkCore.Triggered.Lifecycles
namespace EntityFrameworkCore.Triggered.Lifecycles
{
public interface IBeforeSaveCompletedTrigger
{
Task BeforeSaveCompleted(CancellationToken cancellationToken);
void BeforeSaveCompleted();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Threading;
using System.Threading.Tasks;

namespace EntityFrameworkCore.Triggered.Lifecycles
{
public interface IBeforeSaveStartingAsyncTrigger
{
Task BeforeSaveStartingAsync(CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System.Threading;
using System.Threading.Tasks;

namespace EntityFrameworkCore.Triggered.Lifecycles
namespace EntityFrameworkCore.Triggered.Lifecycles
{
public interface IBeforeSaveStartingTrigger
{
Task BeforeSaveStarting(CancellationToken cancellationToken);
void BeforeSaveStarting();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,23 @@ public static class ServiceCollectionExtensions
{
static readonly Type[] _wellKnownTriggerTypes = new Type[] {
typeof(IBeforeSaveTrigger<>),
typeof(IBeforeSaveAsyncTrigger<>),
typeof(IAfterSaveTrigger<>),
typeof(IAfterSaveAsyncTrigger<>),
typeof(IAfterSaveFailedTrigger<>),
typeof(IAfterSaveFailedAsyncTrigger<>),
typeof(IBeforeSaveStartingTrigger),
typeof(IBeforeSaveStartingAsyncTrigger),
typeof(IBeforeSaveCompletedTrigger),
typeof(IBeforeSaveCompletedAsyncTrigger),
typeof(IAfterSaveFailedStartingTrigger),
typeof(IAfterSaveFailedStartingAsyncTrigger),
typeof(IAfterSaveFailedCompletedTrigger),
typeof(IAfterSaveFailedCompletedAsyncTrigger),
typeof(IAfterSaveStartingTrigger),
typeof(IAfterSaveCompletedTrigger)
typeof(IAfterSaveStartingAsyncTrigger),
typeof(IAfterSaveCompletedTrigger),
typeof(IAfterSaveCompletedAsyncTrigger)
};

static void RegisterTriggerTypes(Type triggerImplementationType, IServiceCollection services)
Expand Down
30 changes: 7 additions & 23 deletions src/EntityFrameworkCore.Triggered.Extensions/Trigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,19 @@ namespace EntityFrameworkCore.Triggered.Extensions
{
public class Trigger<TEntity> :
IBeforeSaveTrigger<TEntity>,
IBeforeSaveAsyncTrigger<TEntity>,
IAfterSaveTrigger<TEntity>,
IAfterSaveFailedTrigger<TEntity>
IAfterSaveAsyncTrigger<TEntity>,
IAfterSaveFailedTrigger<TEntity>,
IAfterSaveFailedAsyncTrigger<TEntity>

where TEntity : class
{
public virtual void BeforeSave(ITriggerContext<TEntity> context) { }
public virtual void AfterSave(ITriggerContext<TEntity> context) { }
public virtual void AfterSaveFailed(ITriggerContext<TEntity> context, Exception exception) { }

public virtual Task BeforeSave(ITriggerContext<TEntity> context, CancellationToken cancellationToken) => Task.CompletedTask;
public virtual Task AfterSave(ITriggerContext<TEntity> context, CancellationToken cancellationToken) => Task.CompletedTask;
public virtual Task AfterSaveFailed(ITriggerContext<TEntity> context, Exception exception, CancellationToken cancellationToken) => Task.CompletedTask;

Task IBeforeSaveTrigger<TEntity>.BeforeSave(ITriggerContext<TEntity> context, CancellationToken cancellationToken)
{
BeforeSave(context);
return BeforeSave(context, cancellationToken);
}

Task IAfterSaveTrigger<TEntity>.AfterSave(ITriggerContext<TEntity> context, CancellationToken cancellationToken)
{
AfterSave(context);
return AfterSave(context, cancellationToken);
}

Task IAfterSaveFailedTrigger<TEntity>.AfterSaveFailed(ITriggerContext<TEntity> context, Exception exception, CancellationToken cancellationToken)
{
AfterSaveFailed(context, exception);
return AfterSaveFailed(context, exception, cancellationToken);
}
public virtual Task BeforeSaveAsync(ITriggerContext<TEntity> context, CancellationToken cancellationToken) => Task.CompletedTask;
public virtual Task AfterSaveAsync(ITriggerContext<TEntity> context, CancellationToken cancellationToken) => Task.CompletedTask;
public virtual Task AfterSaveFailedAsync(ITriggerContext<TEntity> context, Exception exception, CancellationToken cancellationToken) => Task.CompletedTask;
}
}
Loading