Update EntityChanged sample

This commit is contained in:
Sipke Schoorstra 2021-01-08 11:38:29 +01:00
parent 502850889d
commit e53ce3478a
3 changed files with 45 additions and 5 deletions

View file

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Elsa.Services;
using Elsa.Services.Models;
namespace Elsa.Samples.EntityChanged
{
public class ContextProvider : ILoadWorkflowContext
{
private readonly SomeRepository _repository;
public ContextProvider(SomeRepository repository)
{
_repository = repository;
}
public IEnumerable<Type> SupportedTypes => new[] { typeof(Entity) };
public async ValueTask<object?> LoadAsync(LoadWorkflowContext context, CancellationToken cancellationToken = default)
{
return await _repository.GetAsync(context.ContextId);
}
}
}

View file

@ -4,6 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>

View file

@ -1,4 +1,6 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Activities.Entity;
using Elsa.Activities.Entity.Extensions;
using Elsa.Services;
@ -8,10 +10,22 @@ namespace Elsa.Samples.EntityChanged
public class SomeRepository
{
private readonly IWorkflowRunner _workflowRunner;
private ICollection<Entity> _collection = new List<Entity>();
public SomeRepository(IWorkflowRunner workflowRunner) => _workflowRunner = workflowRunner;
public Task AddAsync(Entity entity) => TriggerWorkflowsAsync(entity, EntityChangedAction.Added);
public Task DeleteAsync(Entity entity) => TriggerWorkflowsAsync(entity, EntityChangedAction.Deleted);
public Task AddAsync(Entity entity)
{
_collection.Add(entity);
return TriggerWorkflowsAsync(entity, EntityChangedAction.Added);
}
public Task DeleteAsync(Entity entity)
{
_collection.Remove(entity);
return TriggerWorkflowsAsync(entity, EntityChangedAction.Deleted);
}
public Task<Entity?> GetAsync(string id) => Task.FromResult(_collection.FirstOrDefault(x => x.Id == id));
private async Task TriggerWorkflowsAsync(Entity entity, EntityChangedAction changedAction) =>
await _workflowRunner.TriggerEntityChangedWorkflowsAsync(
@ -19,7 +33,6 @@ namespace Elsa.Samples.EntityChanged
entity.GetType().GetEntityName(),
changedAction,
entity.Id,
entity.Id,
entity);
entity.Id);
}
}