Skip to content

Commit 1f28f10

Browse files
authored
Avoid async await Task.FromResult, plus some other minor tweaks (#19597)
1 parent e78eee5 commit 1f28f10

File tree

8 files changed

+50
-50
lines changed

8 files changed

+50
-50
lines changed

src/Umbraco.Cms.Api.Management/Controllers/PublishedCache/RebuildPublishedCacheController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class RebuildPublishedCacheController : PublishedCacheControllerBase
1515
[HttpPost("rebuild")]
1616
[MapToApiVersion("1.0")]
1717
[ProducesResponseType(StatusCodes.Status200OK)]
18-
public async Task<IActionResult> Rebuild(CancellationToken cancellationToken)
18+
public Task<IActionResult> Rebuild(CancellationToken cancellationToken)
1919
{
2020
if (_databaseCacheRebuilder.IsRebuilding())
2121
{
@@ -27,10 +27,10 @@ public async Task<IActionResult> Rebuild(CancellationToken cancellationToken)
2727
Type = "Error",
2828
};
2929

30-
return await Task.FromResult(Conflict(problemDetails));
30+
return Task.FromResult<IActionResult>(Conflict(problemDetails));
3131
}
3232

3333
_databaseCacheRebuilder.Rebuild(true);
34-
return await Task.FromResult(Ok());
34+
return Task.FromResult<IActionResult>(Ok());
3535
}
3636
}

src/Umbraco.Core/Services/ContentTypeEditing/ContentTypeEditingServiceBase.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected async Task<IEnumerable<ContentTypeAvailableCompositionsResult>> FindAv
5050

5151
var currentCompositionAliases = currentCompositeKeys.Any()
5252
? allContentTypes.Where(ct => currentCompositeKeys.Contains(ct.Key)).Select(ct => ct.Alias).ToArray()
53-
: Array.Empty<string>();
53+
: [];
5454

5555
ContentTypeAvailableCompositionsResults availableCompositions = _contentTypeService.GetAvailableCompositeContentTypes(
5656
contentType,
@@ -142,9 +142,9 @@ protected async Task<IEnumerable<ContentTypeAvailableCompositionsResult>> FindAv
142142
return Attempt.SucceedWithStatus<TContentType?, ContentTypeOperationStatus>(ContentTypeOperationStatus.Success, contentType);
143143
}
144144

145-
protected virtual async Task<ContentTypeOperationStatus> AdditionalCreateValidationAsync(
145+
protected virtual Task<ContentTypeOperationStatus> AdditionalCreateValidationAsync(
146146
ContentTypeEditingModelBase<TPropertyTypeModel, TPropertyTypeContainer> model)
147-
=> await Task.FromResult(ContentTypeOperationStatus.Success);
147+
=> Task.FromResult(ContentTypeOperationStatus.Success);
148148

149149
#region Sanitization
150150

@@ -346,7 +346,7 @@ private ContentTypeOperationStatus ValidateCompositions(TContentType? contentTyp
346346
return ContentTypeOperationStatus.Success;
347347
}
348348

349-
private ContentTypeOperationStatus ValidateProperties(ContentTypeEditingModelBase<TPropertyTypeModel, TPropertyTypeContainer> model, IContentTypeComposition[] allContentTypeCompositions)
349+
private static ContentTypeOperationStatus ValidateProperties(ContentTypeEditingModelBase<TPropertyTypeModel, TPropertyTypeContainer> model, IContentTypeComposition[] allContentTypeCompositions)
350350
{
351351
// grab all content types used for composition and/or inheritance
352352
Guid[] allCompositionKeys = KeysForCompositionTypes(model, CompositionType.Composition, CompositionType.Inheritance);
@@ -365,7 +365,7 @@ private ContentTypeOperationStatus ValidateProperties(ContentTypeEditingModelBas
365365
return ContentTypeOperationStatus.Success;
366366
}
367367

368-
private ContentTypeOperationStatus ValidateContainers(ContentTypeEditingModelBase<TPropertyTypeModel, TPropertyTypeContainer> model, IContentTypeComposition[] allContentTypeCompositions)
368+
private static ContentTypeOperationStatus ValidateContainers(ContentTypeEditingModelBase<TPropertyTypeModel, TPropertyTypeContainer> model, IContentTypeComposition[] allContentTypeCompositions)
369369
{
370370
if (model.Containers.Any(container => Enum.TryParse<PropertyGroupType>(container.Type, out _) is false))
371371
{
@@ -420,7 +420,7 @@ private bool ContentTypeAliasCanBeUsedFor(string alias, Guid key)
420420
return ContentTypeAliasIsInUse(alias) is false;
421421
}
422422

423-
private bool IsReservedContentTypeAlias(string alias)
423+
private static bool IsReservedContentTypeAlias(string alias)
424424
{
425425
var reservedAliases = new[] { "system" };
426426
return reservedAliases.InvariantContains(alias);
@@ -474,7 +474,7 @@ private async Task<TContentType> UpdateAsync(
474474
return contentType;
475475
}
476476

477-
private void UpdateAllowedContentTypes(
477+
private static void UpdateAllowedContentTypes(
478478
TContentType contentType,
479479
ContentTypeEditingModelBase<TPropertyTypeModel, TPropertyTypeContainer> model,
480480
IContentTypeComposition[] allContentTypeCompositions)
@@ -573,7 +573,7 @@ private async Task UpdatePropertiesAsync(
573573
}
574574
}
575575

576-
private string PropertyGroupAlias(string? containerName)
576+
private static string PropertyGroupAlias(string? containerName)
577577
{
578578
if (containerName.IsNullOrWhiteSpace())
579579
{
@@ -625,7 +625,7 @@ private IPropertyType MapProperty(
625625
return propertyType;
626626
}
627627

628-
private void UpdateCompositions(
628+
private static void UpdateCompositions(
629629
TContentType contentType,
630630
ContentTypeEditingModelBase<TPropertyTypeModel, TPropertyTypeContainer> model,
631631
IContentTypeComposition[] allContentTypeCompositions)
@@ -658,7 +658,7 @@ private void UpdateCompositions(
658658
}
659659
}
660660

661-
private void UpdateParentContentType(
661+
private static void UpdateParentContentType(
662662
TContentType contentType,
663663
ContentTypeEditingModelBase<TPropertyTypeModel, TPropertyTypeContainer> model,
664664
IContentTypeComposition[] allContentTypeCompositions)
@@ -677,15 +677,15 @@ private void UpdateParentContentType(
677677

678678
#region Shared between model validation and model update
679679

680-
private Guid[] GetDataTypeKeys(ContentTypeEditingModelBase<TPropertyTypeModel, TPropertyTypeContainer> model)
680+
private static Guid[] GetDataTypeKeys(ContentTypeEditingModelBase<TPropertyTypeModel, TPropertyTypeContainer> model)
681681
=> model.Properties.Select(property => property.DataTypeKey).Distinct().ToArray();
682682

683683
private async Task<IDataType[]> GetDataTypesAsync(ContentTypeEditingModelBase<TPropertyTypeModel, TPropertyTypeContainer> model)
684684
{
685685
Guid[] dataTypeKeys = GetDataTypeKeys(model);
686686
return dataTypeKeys.Any()
687687
? (await _dataTypeService.GetAllAsync(GetDataTypeKeys(model))).ToArray()
688-
: Array.Empty<IDataType>();
688+
: [];
689689
}
690690

691691
private int? GetParentId(ContentTypeEditingModelBase<TPropertyTypeModel, TPropertyTypeContainer> model, Guid? containerKey)
@@ -711,7 +711,7 @@ private async Task<IDataType[]> GetDataTypesAsync(ContentTypeEditingModelBase<TP
711711
return Constants.System.Root;
712712
}
713713

714-
private Guid[] KeysForCompositionTypes(ContentTypeEditingModelBase<TPropertyTypeModel, TPropertyTypeContainer> model, params CompositionType[] compositionTypes)
714+
private static Guid[] KeysForCompositionTypes(ContentTypeEditingModelBase<TPropertyTypeModel, TPropertyTypeContainer> model, params CompositionType[] compositionTypes)
715715
=> model.Compositions
716716
.Where(c => compositionTypes.Contains(c.CompositionType))
717717
.Select(c => c.Key)

src/Umbraco.Core/Services/ContentTypeEditing/ElementSwitchValidator.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,27 @@ public ElementSwitchValidator(
2020
_dataTypeService = dataTypeService;
2121
}
2222

23-
public async Task<bool> AncestorsAreAlignedAsync(IContentType contentType)
23+
public Task<bool> AncestorsAreAlignedAsync(IContentType contentType)
2424
{
2525
// this call does not return the system roots
2626
var ancestorIds = contentType.AncestorIds();
2727
if (ancestorIds.Length == 0)
2828
{
2929
// if there are no ancestors, validation passes
30-
return true;
30+
return Task.FromResult(true);
3131
}
3232

3333
// if there are any ancestors where IsElement is different from the contentType, the validation fails
34-
return await Task.FromResult(_contentTypeService.GetMany(ancestorIds)
34+
return Task.FromResult(_contentTypeService.GetMany(ancestorIds)
3535
.Any(ancestor => ancestor.IsElement != contentType.IsElement) is false);
3636
}
3737

38-
public async Task<bool> DescendantsAreAlignedAsync(IContentType contentType)
38+
public Task<bool> DescendantsAreAlignedAsync(IContentType contentType)
3939
{
4040
IEnumerable<IContentType> descendants = _contentTypeService.GetDescendants(contentType.Id, false);
4141

4242
// if there are any descendants where IsElement is different from the contentType, the validation fails
43-
return await Task.FromResult(descendants.Any(descendant => descendant.IsElement != contentType.IsElement) is false);
43+
return Task.FromResult(descendants.Any(descendant => descendant.IsElement != contentType.IsElement) is false);
4444
}
4545

4646
public async Task<bool> ElementToDocumentNotUsedInBlockStructuresAsync(IContentTypeBase contentType)
@@ -59,8 +59,8 @@ public async Task<bool> ElementToDocumentNotUsedInBlockStructuresAsync(IContentT
5959
.ConfiguredElementTypeKeys().Contains(contentType.Key)) is false;
6060
}
6161

62-
public async Task<bool> DocumentToElementHasNoContentAsync(IContentTypeBase contentType) =>
62+
public Task<bool> DocumentToElementHasNoContentAsync(IContentTypeBase contentType) =>
6363

6464
// if any content for the content type exists, the validation fails.
65-
await Task.FromResult(_contentTypeService.HasContentNodes(contentType.Id) is false);
65+
Task.FromResult(_contentTypeService.HasContentNodes(contentType.Id) is false);
6666
}

src/Umbraco.Core/Services/DataTypeService.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,13 @@ public Task<IEnumerable<IDataType>> GetByEditorAliasAsync(string propertyEditorA
355355
}
356356

357357
/// <inheritdoc />
358-
public async Task<IEnumerable<IDataType>> GetByEditorAliasAsync(string[] propertyEditorAlias)
358+
public Task<IEnumerable<IDataType>> GetByEditorAliasAsync(string[] propertyEditorAlias)
359359
{
360360
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
361361
IQuery<IDataType> query = Query<IDataType>().Where(x => propertyEditorAlias.Contains(x.EditorAlias));
362362
IEnumerable<IDataType> dataTypes = _dataTypeRepository.Get(query).ToArray();
363363
ConvertMissingEditorsOfDataTypesToLabels(dataTypes);
364-
return await Task.FromResult(dataTypes);
364+
return Task.FromResult(dataTypes);
365365
}
366366

367367
/// <inheritdoc />
@@ -396,7 +396,7 @@ private void ConvertMissingEditorOfDataTypeToLabel(IDataType? dataType)
396396
return;
397397
}
398398

399-
ConvertMissingEditorsOfDataTypesToLabels(new[] { dataType });
399+
ConvertMissingEditorsOfDataTypesToLabels([dataType]);
400400
}
401401

402402
private void ConvertMissingEditorsOfDataTypesToLabels(IEnumerable<IDataType> dataTypes)
@@ -763,7 +763,7 @@ public Task<PagedModel<RelationItemModel>> GetPagedRelationsAsync(Guid key, int
763763
var totalItems = combinedUsages.Count;
764764

765765
// Create the page of items.
766-
IList<(string PropertyAlias, Udi Udi)> pagedUsages = combinedUsages
766+
List<(string PropertyAlias, Udi Udi)> pagedUsages = combinedUsages
767767
.OrderBy(x => x.Udi.EntityType) // Document types first, then media types, then member types.
768768
.ThenBy(x => x.PropertyAlias)
769769
.Skip(skip)
@@ -772,7 +772,7 @@ public Task<PagedModel<RelationItemModel>> GetPagedRelationsAsync(Guid key, int
772772

773773
// Get the content types for the UDIs referenced in the page of items to construct the response from.
774774
// They could be document, media or member types.
775-
IList<IContentTypeComposition> contentTypes = GetReferencedContentTypes(pagedUsages);
775+
List<IContentTypeComposition> contentTypes = GetReferencedContentTypes(pagedUsages);
776776

777777
IEnumerable<RelationItemModel> relations = pagedUsages
778778
.Select(x =>
@@ -807,7 +807,7 @@ public Task<PagedModel<RelationItemModel>> GetPagedRelationsAsync(Guid key, int
807807
return Task.FromResult(pagedModel);
808808
}
809809

810-
private IList<IContentTypeComposition> GetReferencedContentTypes(IList<(string PropertyAlias, Udi Udi)> pagedUsages)
810+
private List<IContentTypeComposition> GetReferencedContentTypes(List<(string PropertyAlias, Udi Udi)> pagedUsages)
811811
{
812812
IEnumerable<IContentTypeComposition> documentTypes = GetContentTypes(
813813
pagedUsages,
@@ -845,10 +845,10 @@ public IEnumerable<ValidationResult> ValidateConfigurationData(IDataType dataTyp
845845
{
846846
IConfigurationEditor? configurationEditor = dataType.Editor?.GetConfigurationEditor();
847847
return configurationEditor == null
848-
? new[]
849-
{
848+
?
849+
[
850850
new ValidationResult($"Data type with editor alias {dataType.EditorAlias} does not have a configuration editor")
851-
}
851+
]
852852
: configurationEditor.Validate(dataType.ConfigurationData);
853853
}
854854

src/Umbraco.Core/Services/EntityTypeContainerService.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@ protected EntityTypeContainerService(
5252

5353

5454
/// <inheritdoc />
55-
public async Task<IEnumerable<EntityContainer>> GetAsync(string name, int level)
55+
public Task<IEnumerable<EntityContainer>> GetAsync(string name, int level)
5656
{
5757
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
5858
ReadLock(scope);
59-
return await Task.FromResult(_entityContainerRepository.Get(name, level));
59+
return Task.FromResult(_entityContainerRepository.Get(name, level));
6060
}
6161
/// <inheritdoc />
62-
public async Task<IEnumerable<EntityContainer>> GetAllAsync()
62+
public Task<IEnumerable<EntityContainer>> GetAllAsync()
6363
{
6464
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
6565
ReadLock(scope);
66-
return await Task.FromResult(_entityContainerRepository.GetMany());
66+
return Task.FromResult(_entityContainerRepository.GetMany());
6767
}
6868

6969
/// <inheritdoc />

src/Umbraco.Core/Services/Querying/ContentQueryService.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@ public ContentQueryService(
2323
_coreScopeProvider = coreScopeProvider;
2424
}
2525

26-
public async Task<Attempt<ContentScheduleQueryResult?, ContentQueryOperationStatus>> GetWithSchedulesAsync(Guid id)
26+
public Task<Attempt<ContentScheduleQueryResult?, ContentQueryOperationStatus>> GetWithSchedulesAsync(Guid id)
2727
{
2828
using ICoreScope scope = _coreScopeProvider.CreateCoreScope(autoComplete: true);
2929

30-
IContent? content = await Task.FromResult(_contentService.GetById(id));
30+
IContent? content = _contentService.GetById(id);
3131

3232
if (content == null)
3333
{
34-
return Attempt<ContentScheduleQueryResult, ContentQueryOperationStatus>.Fail(ContentQueryOperationStatus
35-
.ContentNotFound);
34+
return Task.FromResult(Attempt<ContentScheduleQueryResult, ContentQueryOperationStatus>.Fail(ContentQueryOperationStatus
35+
.ContentNotFound));
3636
}
3737

3838
ContentScheduleCollection schedules = _contentService.GetContentScheduleByContentId(id);
3939

40-
return Attempt<ContentScheduleQueryResult?, ContentQueryOperationStatus>
40+
return Task.FromResult(Attempt<ContentScheduleQueryResult?, ContentQueryOperationStatus>
4141
.Succeed(
4242
ContentQueryOperationStatus.Success,
43-
new ContentScheduleQueryResult(content, schedules));
43+
new ContentScheduleQueryResult(content, schedules)));
4444
}
4545
}

src/Umbraco.Core/Services/RelationService.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public IEnumerable<IRelation> GetByRelationTypeName(string relationTypeName)
220220
}
221221

222222
return relationTypeIds.Count == 0
223-
? Enumerable.Empty<IRelation>()
223+
? []
224224
: GetRelationsByListOfTypeIds(relationTypeIds);
225225
}
226226

@@ -230,8 +230,8 @@ public IEnumerable<IRelation> GetByRelationTypeAlias(string relationTypeAlias)
230230
IRelationType? relationType = GetRelationType(relationTypeAlias);
231231

232232
return relationType == null
233-
? Enumerable.Empty<IRelation>()
234-
: GetRelationsByListOfTypeIds(new[] { relationType.Id });
233+
? []
234+
: GetRelationsByListOfTypeIds([relationType.Id]);
235235
}
236236

237237
/// <inheritdoc />
@@ -594,7 +594,7 @@ private async Task<Attempt<IRelationType, RelationTypeOperationStatus>> SaveAsyn
594594

595595
EventMessages eventMessages = EventMessagesFactory.Get();
596596
var savingNotification = new RelationTypeSavingNotification(relationType, eventMessages);
597-
if (scope.Notifications.PublishCancelable(savingNotification))
597+
if (await scope.Notifications.PublishCancelableAsync(savingNotification))
598598
{
599599
scope.Complete();
600600
return Attempt.FailWithStatus(RelationTypeOperationStatus.CancelledByNotification, relationType);
@@ -659,7 +659,7 @@ public void Delete(IRelationType relationType)
659659

660660
EventMessages eventMessages = EventMessagesFactory.Get();
661661
var deletingNotification = new RelationTypeDeletingNotification(relationType, eventMessages);
662-
if (scope.Notifications.PublishCancelable(deletingNotification))
662+
if (await scope.Notifications.PublishCancelableAsync(deletingNotification))
663663
{
664664
scope.Complete();
665665
return Attempt.FailWithStatus<IRelationType?, RelationTypeOperationStatus>(RelationTypeOperationStatus.CancelledByNotification, null);
@@ -670,7 +670,7 @@ public void Delete(IRelationType relationType)
670670
Audit(AuditType.Delete, currentUser, relationType.Id, "Deleted relation type");
671671
scope.Notifications.Publish(new RelationTypeDeletedNotification(relationType, eventMessages).WithStateFrom(deletingNotification));
672672
scope.Complete();
673-
return await Task.FromResult(Attempt.SucceedWithStatus<IRelationType?, RelationTypeOperationStatus>(RelationTypeOperationStatus.Success, relationType));
673+
return Attempt.SucceedWithStatus<IRelationType?, RelationTypeOperationStatus>(RelationTypeOperationStatus.Success, relationType);
674674
}
675675

676676
/// <inheritdoc />
@@ -726,7 +726,7 @@ public IEnumerable<UmbracoObjectTypes> GetAllowedObjectTypes() =>
726726
return _relationTypeRepository.Get(query).FirstOrDefault();
727727
}
728728

729-
private IEnumerable<IRelation> GetRelationsByListOfTypeIds(IEnumerable<int> relationTypeIds)
729+
private List<IRelation> GetRelationsByListOfTypeIds(IEnumerable<int> relationTypeIds)
730730
{
731731
var relations = new List<IRelation>();
732732
using (ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true))

src/Umbraco.Core/Strings/Diff.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Umbraco.Cms.Core.Strings;
1313
/// Copyright (c) by Matthias Hertel, http://www.mathertel.de
1414
/// This work is licensed under a BSD style license. See http://www.mathertel.de/License.aspx
1515
/// </summary>
16-
internal class Diff
16+
internal sealed class Diff
1717
{
1818
/// <summary>
1919
/// Find the difference in 2 texts, comparing by text lines.

0 commit comments

Comments
 (0)