Sean Thorne
12/04/2024, 2:31 PMcs
public static class UmbracoCommerceUmbracoBuilderExtensions
{
public static IUmbracoCommerceBuilder AddMyServices(IUmbracoCommerceBuilder builder)
{
// Replacing the default Product Adapter implementation
builder.Services.AddUnique<ProductAdapterBase, MyCustomProductAdapter>();
// Return the builder to continue the chain
return builder;
}
}
Which is using the ProductAdaptorBase
as the base class instead of IProductAdaptor
(.. as is obsolete). But when inheriting from the base and trying to replace it doesn't actually allow me to register it, using the IProductAdaptor
interface instead works as I expect it to.
Switching up my CustomProductAdaptor to inherit base and implement interface then also allows it to work like so:
cs
builder.CreateUmbracoBuilder()
.AddBackOffice()
.AddWebsite()
.AddUmbracoCommerce(commerceBuilder =>
{
// works just fine
commerceBuilder.Services.AddUnique<IProductAdapter, ProductAdaptor>();
// does not work
// commerceBuilder.Services.AddUnique<ProductAdaptorBase, ProductAdaptor>();
})
So my final class impl looks like this:
cs
public class ProductAdaptor(IProductService productService, ICommerceApi commerceApi) : ProductAdapterBase, IProductAdapter
{
public override IProductSnapshot GetProductSnapshot(Guid storeId, string productReference, string languageIsoCode)
// rest removed for brevity
}
I think the intent is to actually inherit from the ABC and replace that
cs
public abstract class ProductAdapterBase : IProductAdapter
Have I wildly missed something? I'm a little miffed. The obsolete warnings are trigerring.
https://cdn.discordapp.com/attachments/1313875209203486771/1313875209828171836/image.png?ex=6751b924&is=675067a4&hm=c8327ccde8747128ed6cb98c671373708dc67923fbff054a2861e9ee1b46e415&Ian Houghton
12/05/2024, 8:02 AM// Umbraco builder setup
builder.Services.AddUnique<IProductAdapter, CustomProductAdapter>();
// Custom Product Adapter
public class CustomProductAdapter : ProductAdapterBase
{
private readonly ILogger<CustomProductAdapter> _logger;
private readonly ICommerceService _commerceService;
private readonly IPriceService _priceService;
public CustomProductAdapter(
ICommerceService commerceService,
ILogger<CustomProductAdapter> logger,
IPriceService priceService)
{
_commerceService = commerceService;
_logger = logger;
_priceService = priceService;
}
public override IProductSnapshot? GetProductSnapshot(Guid storeId, string productReference, string productVariantReference, string languageIsoCode)
{
try
{
CustomProductSnapshot snapshot = null;
var sku = _commerceService.GetSku(productReference, productVariantReference);
// Product Variant - shouldn't have a productVariantReference
if (string.IsNullOrEmpty(productVariantReference))
{
snapshot = new CustomProductSnapshot(storeId, sku, productReference, null, languageIsoCode, _priceService, _logger);
return snapshot;
}
snapshot = new CustomProductSnapshot(storeId, sku, productReference, productVariantReference, languageIsoCode, _priceService, _logger);
return snapshot;
}
catch (Exception e)
{
_logger.LogError(e, e.Message);
}
return null;
}
public override bool TryGetProductReference(Guid storeId, string sku, out string productReference, out string productVariantReference)
{
throw new NotImplementedException();
}
}
But your close, you just need to insure that when adding the adapter in your startup code, that you use IProductAdapter and NOT ProductAdapterBase. The actual adapter class needs to inherit from ProductAdapterBase without IProductAdapter !!Sean Thorne
12/05/2024, 11:40 AMSean Thorne
12/05/2024, 11:40 AMProductAdaptor(IProductService productService, ICommerceApi commerceApi) : ProductAdapterBase, IProductAdapter
This is the only flavour in which it loads in