Skip to content

Commit fbeafa5

Browse files
author
Ben White
committed
Use a CollectionBuilder to register FeedGenerators. Updated Backend implementation to obtain IProductFeedGeneratorService instances via this collection.
1 parent 0ab63d2 commit fbeafa5

File tree

15 files changed

+77
-41
lines changed

15 files changed

+77
-41
lines changed

src/Umbraco.Commerce.ProductFeeds.Core/Features/FeedGenerators/Application/IProductFeedGeneratorFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ namespace Umbraco.Commerce.ProductFeeds.Core.Features.FeedGenerators.Application
44
{
55
public interface IProductFeedGeneratorFactory
66
{
7-
IProductFeedGeneratorService GetGenerator(ProductFeedType feedType);
7+
IProductFeedGeneratorService GetGenerator(string feedGeneratorId);
88
}
99
}

src/Umbraco.Commerce.ProductFeeds.Core/Features/FeedGenerators/Application/IProductFeedGeneratorService.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ namespace Umbraco.Commerce.ProductFeeds.Core.Features.FeedGenerators.Application
55
{
66
public interface IProductFeedGeneratorService
77
{
8+
/// <summary>
9+
/// Returns the value extractor id. Must be unique.
10+
/// </summary>
11+
public string Id { get; }
12+
13+
/// <summary>
14+
/// Returns a user friendly name of the value extractor.
15+
/// </summary>
16+
public string DisplayName { get; }
817
Task<XmlDocument> GenerateFeedAsync(ProductFeedSettingReadModel feedSetting);
918
}
1019
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Umbraco.Cms.Core.Composing;
2+
using Umbraco.Commerce.ProductFeeds.Core.Features.FeedGenerators.Application;
3+
4+
namespace Umbraco.Commerce.ProductFeeds.Core.Features.FeedGenerators.Implementations
5+
{
6+
public class FeedGeneratorCollection : BuilderCollectionBase<IProductFeedGeneratorService>
7+
{
8+
public FeedGeneratorCollection(Func<IEnumerable<IProductFeedGeneratorService>> items) : base(items)
9+
{
10+
}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Umbraco.Cms.Core.Composing;
8+
using Umbraco.Commerce.ProductFeeds.Core.Features.FeedGenerators.Application;
9+
10+
namespace Umbraco.Commerce.ProductFeeds.Core.Features.FeedGenerators.Implementations
11+
{
12+
public class FeedGeneratorCollectionBuilder : OrderedCollectionBuilderBase<FeedGeneratorCollectionBuilder, FeedGeneratorCollection, IProductFeedGeneratorService>
13+
{
14+
protected override FeedGeneratorCollectionBuilder This => this;
15+
protected override ServiceLifetime CollectionLifetime => ServiceLifetime.Scoped;
16+
}
17+
}

src/Umbraco.Commerce.ProductFeeds.Core/Features/FeedGenerators/Implementations/GoogleMerchantCenterFeedService.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public class GoogleMerchantCenterFeedService : IProductFeedGeneratorService
3636
private readonly ISingleValuePropertyExtractorFactory _singleValuePropertyExtractorFactory;
3737
private readonly IMultipleValuePropertyExtractorFactory _multipleValuePropertyExtractorFactory;
3838

39+
public string Id => "GoogleMerchantCenter";
40+
41+
public string DisplayName => "Google Merchant Center Feed";
42+
3943
public GoogleMerchantCenterFeedService(
4044
ILogger<GoogleMerchantCenterFeedService> logger,
4145
ICurrencyService currencyService,
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
11
using Microsoft.Extensions.DependencyInjection;
22
using Umbraco.Commerce.ProductFeeds.Core.Features.FeedGenerators.Application;
3-
using Umbraco.Commerce.ProductFeeds.Core.Features.FeedSettings.Application;
43

54
namespace Umbraco.Commerce.ProductFeeds.Core.Features.FeedGenerators.Implementations
65
{
76
public class ProductFeedGeneratorFactory : IProductFeedGeneratorFactory
87
{
9-
private readonly IServiceProvider _serviceProvider;
8+
private readonly FeedGeneratorCollection _feedGenerators;
109

11-
public ProductFeedGeneratorFactory(IServiceProvider serviceProvider)
10+
public ProductFeedGeneratorFactory(FeedGeneratorCollection feedGenerators)
1211
{
13-
_serviceProvider = serviceProvider;
12+
_feedGenerators = feedGenerators;
1413
}
1514

16-
public IProductFeedGeneratorService GetGenerator(ProductFeedType feedType)
15+
public IProductFeedGeneratorService GetGenerator(string feedGeneratorId)
1716
{
18-
switch (feedType)
19-
{
20-
case ProductFeedType.GoogleMerchantCenter:
21-
return _serviceProvider.GetRequiredService<GoogleMerchantCenterFeedService>();
17+
IProductFeedGeneratorService? feedGenerator = _feedGenerators.FirstOrDefault(p => p.Id == feedGeneratorId);
2218

23-
default:
24-
throw new InvalidOperationException($"Invalid feed type: {feedType}");
19+
if (feedGenerator == null)
20+
{
21+
throw new InvalidOperationException($"Invalid feed id: {feedGeneratorId}");
2522
}
26-
23+
return feedGenerator;
2724
}
2825
}
2926
}

src/Umbraco.Commerce.ProductFeeds.Core/Features/FeedSettings/Application/ProductFeedSettingReadModel.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ public class ProductFeedSettingReadModel
77
{
88
public Guid Id { get; set; }
99

10-
public ProductFeedType FeedType { get; set; }
11-
12-
public string FeedTypeName => FeedType.GetDescription();
10+
public required string FeedGeneratorId { get; set; }
1311

1412
public required string FeedName { get; set; }
1513

src/Umbraco.Commerce.ProductFeeds.Core/Features/FeedSettings/Application/ProductFeedSettingWriteModel.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Runtime.Serialization;
2+
13
namespace Umbraco.Commerce.ProductFeeds.Core.Features.FeedSettings.Application
24
{
35
public class ProductFeedSettingWriteModel
@@ -6,7 +8,7 @@ public class ProductFeedSettingWriteModel
68

79
public required string FeedRelativePath { get; set; }
810

9-
public ProductFeedType? FeedType { get; set; }
11+
public required string FeedGeneratorId { get; set; }
1012

1113
public required string FeedName { get; set; }
1214

src/Umbraco.Commerce.ProductFeeds.Core/Features/FeedSettings/Application/ProductFeedSettingWriteModelValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public ProductFeedSettingWriteModelValidator()
1111
RuleFor(x => x.FeedName).NotEmpty().WithName("Feed Name");
1212
RuleFor(x => x.FeedRelativePath).NotEmpty().WithName("Feed Relative Path");
1313
RuleFor(x => x.FeedDescription).MaximumLength(MaximumStringLength).WithName("Feed Description");
14-
RuleFor(x => x.FeedType).NotEmpty().WithName("Feed Type");
14+
RuleFor(x => x.FeedGeneratorId).NotEmpty().WithName("Feed Generator Id");
1515
RuleFor(x => x.StoreId).NotEmpty().WithName("Umbraco Commerce Store");
1616
RuleFor(x => x.ProductRootId).NotEmpty().WithName("Product Root");
1717
RuleFor(x => x.ProductDocumentTypeIds).NotEmpty().WithName("Product Document Types");

src/Umbraco.Commerce.ProductFeeds.Core/Features/FeedSettings/Application/ProductFeedType.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)