Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
#nullable enable
using Grand.Domain.Catalog;
using Grand.Web.AdminShared.Interfaces;
using Microsoft.AspNetCore.Http;
namespace Grand.Web.AdminShared.Services;
/// <summary>
/// Resolves the correct per-host <see cref="IAdminDataScope{Product}" /> implementation at
/// request time, based on the current request's "area" route value, instead of relying on
/// DI registration order.
///
/// Why this exists: Grand.Web (the combined host, run as "grand-web" in Aspire) references
/// Grand.Web.Admin, Grand.Web.Store, and Grand.Web.Vendor together in one process/one DI
/// container. Each host's own StartupApplication used to register
/// AddScoped<IAdminDataScope<Product>, X>() directly - plain AddScoped doesn't replace an
/// earlier registration, it appends one, so whichever host's StartupApplication ran last (by
/// IStartupApplication.Priority - Vendor's is highest) silently won for every area in that
/// process. That surfaced as a NullReferenceException in VendorProductDataScope.DefaultVendorId
/// when an Admin user opened the product list under the combined host, because Vendor's scope
/// assumes WorkContext.CurrentVendor is set.
///
/// Fix: register the three concrete scopes as themselves (not as IAdminDataScope<Product>)
/// and register this resolver as the single IAdminDataScope<Product> - see
/// Grand.Web.AdminShared/Startup/StartupApplication.cs. Each host's own StartupApplication no
/// longer registers IAdminDataScope<Product> at all.
/// </summary>
public class RoutedProductDataScope(
IHttpContextAccessor httpContextAccessor,
GlobalAdminDataScope<Product> globalScope,
StoreAdminDataScope<Product> storeScope,
VendorProductDataScope vendorScope) : IAdminDataScope<Product>
{
private IAdminDataScope<Product> Resolved
{
get
{
var area = httpContextAccessor.HttpContext?.Request.RouteValues["area"] as string;
return area switch {
"Admin" => globalScope,
"Store" => storeScope,
"Vendor" => vendorScope,
//fail closed: this object fronts vendor/store tenant isolation, so an unrecognized
//or missing area must never silently resolve to the unscoped global scope
_ => throw new InvalidOperationException(
$"RoutedProductDataScope: unrecognized or missing area '{area}'.")
};
}
}
public Task<bool> HasAccess(Product entity) => Resolved.HasAccess(entity);
public Task<bool> CanView(Product entity) => Resolved.CanView(entity);
public string? DefaultStoreId => Resolved.DefaultStoreId;
public string ResourceKeyPrefix => Resolved.ResourceKeyPrefix;
public bool ShowStoreSelector => Resolved.ShowStoreSelector;
public string? DefaultVendorId => Resolved.DefaultVendorId;
public bool CanFeatureOnHomepage => Resolved.CanFeatureOnHomepage;
}