public async Task <Result> EditAsync(IEnumerable <TModel> models, CancellationToken cancellationToken = default) { var modelList = models.ToList(); var ids = modelList.Select(m => m.Id).ToList(); var entityList = await FindEntityListAsync(e => ids.Contains(e.Id), cancellationToken); var modifiedList = modelList.ToModifiedList <TEntity, TModel, TKey>(entityList, MapToModel); var result = await BeforeEditAsync(modifiedList, entityList, cancellationToken); if (result.Failed) { return(result); } MappingExtensions.Map(modelList, entityList, MapToEntity); await AfterMappingAsync(modelList, entityList, cancellationToken); result = await EventBus.TriggerEditingEventAsync <TModel, TKey>(modifiedList, cancellationToken); if (result.Failed) { return(result); } await UpdateEntityListAsync(entityList, cancellationToken); MappingExtensions.Map(entityList, modelList, MapToModel); result = await AfterEditAsync(modifiedList, entityList, cancellationToken); if (result.Failed) { return(result); } result = await EventBus.TriggerEditedEventAsync <TModel, TKey>(modifiedList, cancellationToken); return(result); }
public async Task <Result> CreateAsync(IEnumerable <TModel> models, CancellationToken cancellationToken = default) { var modelList = models.ToList(); var result = await BeforeCreateAsync(modelList, cancellationToken); if (result.Failed) { return(result); } var entityList = modelList.MapReadOnlyList <TModel, TEntity>(MapToEntity); await AfterMappingAsync(modelList, entityList, cancellationToken); result = await EventBus.TriggerCreatingEventAsync <TModel, TKey>(modelList, cancellationToken); if (result.Failed) { return(result); } await CreateEntityListAsync(entityList, cancellationToken); MappingExtensions.Map(entityList, modelList, MapToModel); result = await AfterCreateAsync(modelList, cancellationToken); if (result.Failed) { return(result); } result = await EventBus.TriggerCreatedEventAsync <TModel, TKey>(modelList, cancellationToken); return(result); }