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

Convert various sync calls to async #7287

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions src/Libraries/Nop.Data/Extensions/AsyncIQueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -667,4 +667,15 @@ public static async Task<IPagedList<T>> ToPagedListAsync<T>(this IQueryable<T> s

return new PagedList<T>(data, pageIndex, pageSize, count);
}

/// <summary>
/// Returns an <see cref="IAsyncEnumerable{T}"/> that can be enumerated asynchronously.
/// </summary>
/// <typeparam name="T">Source sequence element type.</typeparam>
/// <param name="source">Source sequence.</param>
/// <returns>A query that can be enumerated asynchronously.</returns>
public static IAsyncEnumerable<T> ToAsyncEnumerable<T>(this IQueryable<T> source)
{
return AsyncExtensions.AsAsyncEnumerable(source);
}
Comment on lines +677 to +680
Copy link
Contributor Author

@Shane32 Shane32 Aug 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing calls to ToAsyncEnumerable(IEnumerable) were simply wrapping an IEnumerable into an IAsyncEnumerable wrapper and were not asynchronous. LinqToDb provides the AsAsyncEnumerable method to properly convert a IQueryable to IAsyncEnumerable. This new method has a tighter scope (accepting a IQueryable) and receives precedence from the compiler.

}
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ public virtual async Task<Customer> GetUserByExternalAuthenticationParametersAsy
{
ArgumentNullException.ThrowIfNull(parameters);

var associationRecord = _externalAuthenticationRecordRepository.Table.FirstOrDefault(record =>
var associationRecord = await _externalAuthenticationRecordRepository.Table.FirstOrDefaultAsync(record =>
record.ExternalIdentifier.Equals(parameters.ExternalIdentifier) && record.ProviderSystemName.Equals(parameters.ProviderSystemName));
if (associationRecord == null)
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Libraries/Nop.Services/Catalog/CategoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public virtual async Task ClearDiscountCategoryMappingAsync(Discount discount)

var mappings = _discountCategoryMappingRepository.Table.Where(dcm => dcm.DiscountId == discount.Id);

await _discountCategoryMappingRepository.DeleteAsync(mappings.ToList());
await _discountCategoryMappingRepository.DeleteAsync(await mappings.ToListAsync());
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Libraries/Nop.Services/Catalog/ManufacturerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public virtual async Task ClearDiscountManufacturerMappingAsync(Discount discoun

var mappings = _discountManufacturerMappingRepository.Table.Where(dcm => dcm.DiscountId == discount.Id);

await _discountManufacturerMappingRepository.DeleteAsync(mappings.ToList());
await _discountManufacturerMappingRepository.DeleteAsync(await mappings.ToListAsync());
}

/// <summary>
Expand Down Expand Up @@ -488,7 +488,7 @@ public virtual async Task<string[]> GetNotExistingManufacturersAsync(string[] ma
var query = _manufacturerRepository.Table;//.Where(m => !m.Deleted);
var queryFilter = manufacturerIdsNames.Distinct().ToArray();
//filtering by name
var filter = query.Select(m => m.Name).Where(m => queryFilter.Contains(m)).ToList();
var filter = await query.Select(m => m.Name).Where(m => queryFilter.Contains(m)).ToListAsync();
queryFilter = queryFilter.Except(filter).ToArray();

//if some names not found
Expand Down
10 changes: 5 additions & 5 deletions src/Libraries/Nop.Services/Catalog/ProductService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ where p.Published && !p.Deleted && p.VisibleIndividually &&
//apply ACL constraints
query = await _aclService.ApplyAcl(query, customerRoleIds);

featuredProducts = query.ToList();
featuredProducts = await query.ToListAsync();

return featuredProducts.Select(p => p.Id).ToList();
});
Expand Down Expand Up @@ -709,7 +709,7 @@ where p.Published && !p.Deleted && p.VisibleIndividually &&
//apply ACL constraints
query = await _aclService.ApplyAcl(query, customerRoleIds);

return query.Select(p => p.Id).ToList();
return await query.Select(p => p.Id).ToListAsync();
});

if (!featuredProducts.Any() && featuredProductIds.Any())
Expand Down Expand Up @@ -786,7 +786,7 @@ where categoryIds.Contains(pc.CategoryId)
.PrepareKeyForDefaultCache(NopCatalogDefaults.CategoryProductsNumberCacheKey, customerRoleIds, storeId, categoryIds);

//only distinct products
return await _staticCacheManager.GetAsync(cacheKey, () => query.Select(p => p.Id).Count());
return await _staticCacheManager.GetAsync(cacheKey, () => query.Select(p => p.Id).CountAsync());
}

/// <summary>
Expand Down Expand Up @@ -2545,8 +2545,8 @@ public virtual async Task SetProductReviewHelpfulnessAsync(ProductReview product
ArgumentNullException.ThrowIfNull(productReview);

var customer = await _workContext.GetCurrentCustomerAsync();
var prh = _productReviewHelpfulnessRepository.Table
.SingleOrDefault(h => h.ProductReviewId == productReview.Id && h.CustomerId == customer.Id);
var prh = await _productReviewHelpfulnessRepository.Table
.SingleOrDefaultAsync(h => h.ProductReviewId == productReview.Id && h.CustomerId == customer.Id);

if (prh is null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Libraries/Nop.Services/Catalog/ProductTagService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ group ptm by ptm.ProductTagId into ptmGrouped
ProductCount = ptmGrouped.Count()
};

return pTagCount.ToDictionary(item => item.ProductTagId, item => item.ProductCount);
return await pTagCount.ToDictionaryAsync(item => item.ProductTagId, item => item.ProductCount);
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/Libraries/Nop.Services/Discounts/DiscountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public virtual async Task<IList<Discount>> GetAllDiscountsAsync(DiscountType? di
discounts = discounts.Where(discount =>
!discount.EndDateUtc.HasValue || discount.EndDateUtc <= endDateUtc.Value);

return discounts.ToList();
return await discounts.ToListAsync();
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Libraries/Nop.Services/Forums/ForumService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,14 @@ public virtual async Task DeleteForumAsync(Forum forum)
where queryTopicIds.Contains(fs.TopicId)
select fs;

await _forumSubscriptionRepository.DeleteAsync(queryFs1.ToList());
await _forumSubscriptionRepository.DeleteAsync(await queryFs1.ToListAsync());

//delete forum subscriptions (forum)
var queryFs2 = from fs in _forumSubscriptionRepository.Table
where fs.ForumId == forum.Id
select fs;

await _forumSubscriptionRepository.DeleteAsync(queryFs2.ToList());
await _forumSubscriptionRepository.DeleteAsync(await queryFs2.ToListAsync());

//delete forum
await _forumRepository.DeleteAsync(forum);
Expand Down Expand Up @@ -415,7 +415,7 @@ public virtual async Task DeleteTopicAsync(ForumTopic forumTopic)
var queryFs = from ft in _forumSubscriptionRepository.Table
where ft.TopicId == forumTopic.Id
select ft;
var forumSubscriptions = queryFs.ToList();
var forumSubscriptions = await queryFs.ToListAsync();

await _forumSubscriptionRepository.DeleteAsync(forumSubscriptions);

Expand Down
Loading