Skip to content

Commit 391d3ec

Browse files
VCST-2585: Migrate Inventory to Generic CRUD and Search (#155)
1 parent f4cf653 commit 391d3ec

21 files changed

+863
-899
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=15b5b1f1_002D457c_002D4ca6_002Db278_002D5615aedc07d3/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Static" AccessRightKinds="Private" Description="Static readonly fields (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="READONLY_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="_" Suffix="" Style="aaBb" /&gt;&lt;/Policy&gt;</s:String>
3+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=236f7aa5_002D7b06_002D43ca_002Dbf2a_002D9b31bfcff09a/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Any" AccessRightKinds="Private" Description="Constant fields (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="CONSTANT_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="_" Suffix="" Style="aaBb" /&gt;&lt;/Policy&gt;</s:String>
24
<s:Boolean x:Key="/Default/UserDictionary/Words/=postgre/@EntryIndexedValue">True</s:Boolean>
35
<s:Boolean x:Key="/Default/UserDictionary/Words/=virto/@EntryIndexedValue">True</s:Boolean>
46
</wpf:ResourceDictionary>

src/VirtoCommerce.InventoryModule.Core/Model/Search/ProductInventorySearchCriteria.cs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,23 @@
22
using System.Linq;
33
using VirtoCommerce.Platform.Core.Common;
44

5-
namespace VirtoCommerce.InventoryModule.Core.Model.Search
5+
namespace VirtoCommerce.InventoryModule.Core.Model.Search;
6+
7+
public class ProductInventorySearchCriteria : SearchCriteriaBase
68
{
7-
public class ProductInventorySearchCriteria : SearchCriteriaBase
9+
public string ProductId
810
{
9-
public string ProductId
11+
get
1012
{
11-
get
12-
{
13-
if (ProductIds.IsNullOrEmpty())
14-
{
15-
return null;
16-
}
17-
18-
return ProductIds.First();
19-
}
20-
set
21-
{
22-
if (ProductIds == null)
23-
{
24-
ProductIds = new List<string>();
25-
}
26-
27-
if (!ProductIds.Contains(value))
28-
{
29-
ProductIds.Insert(0, value);
30-
}
31-
}
13+
return ProductIds?.FirstOrDefault();
3214
}
15+
set
16+
{
17+
ProductIds = !value.IsNullOrEmpty() ? [value] : null;
18+
}
19+
}
3320

34-
public IList<string> ProductIds { get; set; }
21+
public IList<string> ProductIds { get; set; }
3522

36-
public bool WithInventoryOnly { get; set; }
37-
}
23+
public bool WithInventoryOnly { get; set; }
3824
}
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
using System;
12
using System.Threading.Tasks;
3+
using VirtoCommerce.InventoryModule.Core.Model;
24
using VirtoCommerce.InventoryModule.Core.Model.Search;
5+
using VirtoCommerce.Platform.Core.GenericCrud;
36

4-
namespace VirtoCommerce.InventoryModule.Core.Services
7+
namespace VirtoCommerce.InventoryModule.Core.Services;
8+
9+
public interface IInventorySearchService : ISearchService<InventorySearchCriteria, InventoryInfoSearchResult, InventoryInfo>
510
{
6-
public interface IInventorySearchService
7-
{
8-
Task<InventoryInfoSearchResult> SearchInventoriesAsync(InventorySearchCriteria criteria);
9-
}
11+
[Obsolete("Use SearchAsync()", DiagnosticId = "VC0011", UrlFormat = "https://docs.virtocommerce.org/products/products-virto3-versions")]
12+
Task<InventoryInfoSearchResult> SearchInventoriesAsync(InventorySearchCriteria criteria);
1013
}
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Threading.Tasks;
34
using VirtoCommerce.InventoryModule.Core.Model;
5+
using VirtoCommerce.Platform.Core.GenericCrud;
46

5-
namespace VirtoCommerce.InventoryModule.Core.Services
7+
namespace VirtoCommerce.InventoryModule.Core.Services;
8+
9+
public interface IInventoryService : ICrudService<InventoryInfo>
610
{
7-
public interface IInventoryService
8-
{
9-
Task<IEnumerable<InventoryInfo>> GetProductsInventoryInfosAsync(IEnumerable<string> productIds, string responseGroup = null);
10-
Task SaveChangesAsync(IEnumerable<InventoryInfo> inventoryInfos);
11-
Task<IEnumerable<InventoryInfo>> GetByIdsAsync(string[] ids, string responseGroup = null);
12-
}
11+
[Obsolete("Use IInventorySearchService.SearchAsync()", DiagnosticId = "VC0011", UrlFormat = "https://docs.virtocommerce.org/products/products-virto3-versions")]
12+
Task<IEnumerable<InventoryInfo>> GetProductsInventoryInfosAsync(IEnumerable<string> productIds, string responseGroup = null);
13+
14+
[Obsolete("Use GetAsync()", DiagnosticId = "VC0011", UrlFormat = "https://docs.virtocommerce.org/products/products-virto3-versions")]
15+
Task<IEnumerable<InventoryInfo>> GetByIdsAsync(string[] ids, string responseGroup = null);
1316
}
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
using System;
12
using System.Threading.Tasks;
3+
using VirtoCommerce.InventoryModule.Core.Model;
24
using VirtoCommerce.InventoryModule.Core.Model.Search;
5+
using VirtoCommerce.Platform.Core.GenericCrud;
36

4-
namespace VirtoCommerce.InventoryModule.Core.Services
7+
namespace VirtoCommerce.InventoryModule.Core.Services;
8+
9+
/// <summary>
10+
/// This interface is intended to search the product inventories within all fulfillment centers, do a LeftJoin search with fulfillment centers and return InventoryInfo
11+
/// object for each Fulfillment center registered in the system even if it won't have an inventory record for a given product
12+
/// </summary>
13+
public interface IProductInventorySearchService : ISearchService<ProductInventorySearchCriteria, InventoryInfoSearchResult, InventoryInfo>
514
{
6-
/// <summary>
7-
/// This interface is intended to search the product inventories within all fulfillment centers, do a LeftJoin search with fulfillment centers and return InventoryInfo
8-
/// object for each Fulfillment center registered in the system even if it won't have an inventory record for a given product
9-
/// </summary>
10-
public interface IProductInventorySearchService
11-
{
12-
Task<InventoryInfoSearchResult> SearchProductInventoriesAsync(ProductInventorySearchCriteria criteria);
13-
}
15+
[Obsolete("Use SearchAsync()", DiagnosticId = "VC0011", UrlFormat = "https://docs.virtocommerce.org/products/products-virto3-versions")]
16+
Task<InventoryInfoSearchResult> SearchProductInventoriesAsync(ProductInventorySearchCriteria criteria);
1417
}
Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
using System;
12
using System.Collections.Generic;
2-
using System.Linq;
33
using System.Threading.Tasks;
44
using VirtoCommerce.InventoryModule.Core.Model;
55
using VirtoCommerce.InventoryModule.Core.Model.Search;
@@ -9,38 +9,9 @@ namespace VirtoCommerce.InventoryModule.Core.Services;
99

1010
public static class ProductInventorySearchServiceExtensions
1111
{
12-
public static async Task<IList<InventoryInfo>> SearchAllProductInventoriesNoCloneAsync(this IProductInventorySearchService searchService, ProductInventorySearchCriteria searchCriteria)
12+
[Obsolete("Use SearchAllNoCloneAsync()", DiagnosticId = "VC0011", UrlFormat = "https://docs.virtocommerce.org/products/products-virto3-versions")]
13+
public static Task<IList<InventoryInfo>> SearchAllProductInventoriesNoCloneAsync(this IProductInventorySearchService searchService, ProductInventorySearchCriteria searchCriteria)
1314
{
14-
var result = new List<InventoryInfo>();
15-
16-
await foreach (var item in searchService.SearchProductInventorBatchedAsync(searchCriteria))
17-
{
18-
result.AddRange(item.Results);
19-
}
20-
21-
return result;
22-
}
23-
24-
private static async IAsyncEnumerable<InventoryInfoSearchResult> SearchProductInventorBatchedAsync(this IProductInventorySearchService searchService, ProductInventorySearchCriteria searchCriteria)
25-
{
26-
searchCriteria = searchCriteria.CloneTyped();
27-
int totalCount;
28-
do
29-
{
30-
var searchResult = await searchService.SearchProductInventoriesAsync(searchCriteria);
31-
if (searchCriteria.Take == 0 || searchResult.Results.Any())
32-
{
33-
yield return searchResult;
34-
}
35-
36-
if (searchCriteria.Take == 0)
37-
{
38-
break;
39-
}
40-
41-
totalCount = searchResult.TotalCount;
42-
searchCriteria.Skip += searchCriteria.Take;
43-
}
44-
while (searchCriteria.Skip < totalCount);
15+
return searchService.SearchAllNoCloneAsync(searchCriteria);
4516
}
4617
}

src/VirtoCommerce.InventoryModule.Data/Caching/InventoryCacheRegion.cs

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,45 @@
55
using VirtoCommerce.InventoryModule.Core.Model;
66
using VirtoCommerce.Platform.Core.Caching;
77

8-
namespace VirtoCommerce.InventoryModule.Data.Caching
8+
namespace VirtoCommerce.InventoryModule.Data.Caching;
9+
10+
public class InventoryCacheRegion : CancellableCacheRegion<InventoryCacheRegion>
911
{
10-
public class InventoryCacheRegion : CancellableCacheRegion<InventoryCacheRegion>
12+
public static IChangeToken CreateChangeToken(List<InventoryInfo> inventoryInfos)
1113
{
14+
ArgumentNullException.ThrowIfNull(inventoryInfos);
1215

13-
public static IChangeToken CreateChangeToken(List<InventoryInfo> inventoryInfos)
14-
{
15-
if (inventoryInfos == null)
16-
{
17-
throw new ArgumentNullException(nameof(inventoryInfos));
18-
}
19-
//generate the cancellation tokens for inventory.productId as well to be able evict from the cache the all inventories that are reference to the product id
20-
var keys = inventoryInfos.Select(x => x.Id).Concat(inventoryInfos.Select(x => x.ProductId))
21-
.Where(x => !string.IsNullOrEmpty(x))
22-
.Distinct()
23-
.ToArray();
24-
return CreateChangeToken(keys);
25-
}
16+
// Generate the cancellation tokens for inventory.productId as well to be able to evict from the cache all inventories that have this product id
17+
var keys = inventoryInfos
18+
.Select(x => x.Id)
19+
.Concat(inventoryInfos.Select(x => x.ProductId))
20+
.Where(x => !string.IsNullOrEmpty(x))
21+
.Distinct()
22+
.ToArray();
2623

27-
public static IChangeToken CreateChangeToken(string[] inventoryIds)
24+
return CreateChangeToken(keys);
25+
}
26+
27+
public static IChangeToken CreateChangeToken(ICollection<string> ids)
28+
{
29+
ArgumentNullException.ThrowIfNull(ids);
30+
31+
var changeTokens = new List<IChangeToken> { CreateChangeToken() };
32+
33+
foreach (var id in ids)
2834
{
29-
if (inventoryIds == null)
30-
{
31-
throw new ArgumentNullException(nameof(inventoryIds));
32-
}
33-
var changeTokens = new List<IChangeToken>() { CreateChangeToken() };
34-
foreach (var inventoryId in inventoryIds)
35-
{
36-
changeTokens.Add(CreateChangeTokenForKey(inventoryId));
37-
}
38-
return new CompositeChangeToken(changeTokens);
35+
changeTokens.Add(CreateChangeTokenForKey(id));
3936
}
4037

41-
public static void ExpireInventory(InventoryInfo inventory)
38+
return new CompositeChangeToken(changeTokens);
39+
}
40+
41+
public static void ExpireInventory(InventoryInfo inventory)
42+
{
43+
if (inventory != null)
4244
{
43-
if (inventory != null)
44-
{
45-
ExpireTokenForKey(inventory.Id);
46-
ExpireTokenForKey(inventory.ProductId);
47-
}
45+
ExpireTokenForKey(inventory.Id);
46+
ExpireTokenForKey(inventory.ProductId);
4847
}
4948
}
5049
}
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using VirtoCommerce.Platform.Core.Caching;
22

3-
namespace VirtoCommerce.InventoryModule.Data.Caching
4-
{
5-
public class InventorySearchCacheRegion : CancellableCacheRegion<InventorySearchCacheRegion>
6-
{
7-
}
8-
}
3+
namespace VirtoCommerce.InventoryModule.Data.Caching;
4+
5+
public class InventorySearchCacheRegion : CancellableCacheRegion<InventorySearchCacheRegion>;

0 commit comments

Comments
 (0)