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
Refactor: Separate IWorkContext into read (IWorkContext) and write (IWorkContextSetter) interfaces for clearer separation of concerns
supportad765e32fc15
diff --git a/src/Business/Grand.Business.Checkout/Commands/Handlers/Orders/PlaceOrderCommandHandler.cs b/src/Business/Grand.Business.Checkout/Commands/Handlers/Orders/PlaceOrderCommandHandler.csindex 71b520856..30a1ed183 100644--- a/src/Business/Grand.Business.Checkout/Commands/Handlers/Orders/PlaceOrderCommandHandler.cs+++ b/src/Business/Grand.Business.Checkout/Commands/Handlers/Orders/PlaceOrderCommandHandler.cs@@ -1029,7 +1029,7 @@ namespace Grand.Business.Checkout.Commands.Handlers.Orders{using var scope = scopeFactory.CreateScope();- var workContext = scope.ServiceProvider.GetService<IWorkContext>();+ var workContext = scope.ServiceProvider.GetService<IWorkContextSetter>();await workContext.SetCurrentCustomer(customer);await workContext.SetWorkingLanguage(customer);await workContext.SetWorkingCurrency(customer);diff --git a/src/Core/Grand.Infrastructure/IWorkContext.cs b/src/Core/Grand.Infrastructure/IWorkContext.csindex d95472a68..89c542c69 100644--- a/src/Core/Grand.Infrastructure/IWorkContext.cs+++ b/src/Core/Grand.Infrastructure/IWorkContext.cs@@ -17,18 +17,6 @@ namespace Grand.Infrastructure/// </summary>Customer CurrentCustomer { get; }- /// <summary>- /// Set the current customer by Middleware- /// </summary>- /// <returns></returns>- Task<Customer> SetCurrentCustomer();-- /// <summary>- /// Set the current customer- /// </summary>- /// <returns></returns>- Task<Customer> SetCurrentCustomer(Customer customer);-/// <summary>/// Gets or sets the original customer (in case the current one is impersonated)/// </summary>@@ -45,30 +33,56 @@ namespace Grand.InfrastructureDomainHost CurrentHost { get; }/// <summary>- /// Set the current vendor (logged-in manager)+ /// Get or set current user working language/// </summary>- Task<Vendor> SetCurrentVendor(Customer customer);+ Language WorkingLanguage { get; }/// <summary>- /// Get or set current user working language+ /// Get or set current user working currency/// </summary>- Language WorkingLanguage { get; }+ Currency WorkingCurrency { get; }/// <summary>- /// Set current user working language by Middleware+ /// Get current tax display type/// </summary>- Task<Language> SetWorkingLanguage(Customer customer);+ TaxDisplayType TaxDisplayType { get; }++ /// <summary>+ /// Gets or sets the current store+ /// </summary>+ Store CurrentStore { get; }++ }+ public interface IWorkContextSetter+ {/// <summary>- /// Set current user working language+ /// Set the current customer by Middleware/// </summary>- Task<Language> SetWorkingLanguage(Language language);+ /// <returns></returns>+ Task<Customer> SetCurrentCustomer();/// <summary>- /// Get or set current user working currency+ /// Set the current customer/// </summary>- Currency WorkingCurrency { get; }+ /// <returns></returns>+ Task<Customer> SetCurrentCustomer(Customer customer);++ /// <summary>+ /// Set the current vendor (logged-in manager)+ /// </summary>+ Task<Vendor> SetCurrentVendor(Customer customer);++ /// <summary>+ /// Set current user working language by Middleware+ /// </summary>+ Task<Language> SetWorkingLanguage(Customer customer);+ /// <summary>+ /// Set current user working language+ /// </summary>+ Task<Language> SetWorkingLanguage(Language language);+/// <summary>/// Set current user working currency by Middleware/// </summary>@@ -78,11 +92,6 @@ namespace Grand.Infrastructure/// Set user working currency/// </summary>Task<Currency> SetWorkingCurrency(Currency currency);-- /// <summary>- /// Get current tax display type- /// </summary>- TaxDisplayType TaxDisplayType { get; }/// <summary>/// Set current tax display type by Middleware@@ -92,12 +101,6 @@ namespace Grand.Infrastructure/// <summary>/// Set tax display type/// </summary>- Task<TaxDisplayType> SetTaxDisplayType(TaxDisplayType taxDisplayType);-- /// <summary>- /// Gets or sets the current store- /// </summary>- Store CurrentStore { get; }-+ Task<TaxDisplayType> SetTaxDisplayType(TaxDisplayType taxDisplayType);}}diff --git a/src/Web/Grand.Web.Admin/Controllers/HomeController.cs b/src/Web/Grand.Web.Admin/Controllers/HomeController.csindex b96672b27..c7b9b87ad 100644--- a/src/Web/Grand.Web.Admin/Controllers/HomeController.cs+++ b/src/Web/Grand.Web.Admin/Controllers/HomeController.cs@@ -109,12 +109,12 @@ namespace Grand.Web.Admin.Controllersreturn PartialView(model);}- public async Task<IActionResult> SetLanguage(string langid, [FromServices] ILanguageService languageService, string returnUrl = "")+ public async Task<IActionResult> SetLanguage(string langid, [FromServices] IWorkContextSetter workContextSetter, [FromServices] ILanguageService languageService, string returnUrl = ""){var language = await languageService.GetLanguageById(langid);if (language != null){- await _workContext.SetWorkingLanguage(language);+ await workContextSetter.SetWorkingLanguage(language);}//home pagediff --git a/src/Web/Grand.Web.Common/Infrastructure/BackgroundServiceTask.cs b/src/Web/Grand.Web.Common/Infrastructure/BackgroundServiceTask.csindex 38f4d1bf2..c2fb4f8ce 100644--- a/src/Web/Grand.Web.Common/Infrastructure/BackgroundServiceTask.cs+++ b/src/Web/Grand.Web.Common/Infrastructure/BackgroundServiceTask.cs@@ -113,7 +113,7 @@ namespace Grand.Web.Common.Infrastructureprivate async Task WorkContext(IServiceProvider serviceProvider, ScheduleTask scheduleTask){- var workContext = serviceProvider.GetRequiredService<IWorkContext>();+ var workContext = serviceProvider.GetRequiredService<IWorkContextSetter>();var storeHelper = serviceProvider.GetRequiredService<IStoreHelper>();await workContext.SetCurrentCustomer();await storeHelper.SetCurrentStore(scheduleTask.StoreId);diff --git a/src/Web/Grand.Web.Common/Middleware/WorkContextMiddleware.cs b/src/Web/Grand.Web.Common/Middleware/WorkContextMiddleware.csindex 16c15f690..7feb669c1 100644--- a/src/Web/Grand.Web.Common/Middleware/WorkContextMiddleware.cs+++ b/src/Web/Grand.Web.Common/Middleware/WorkContextMiddleware.cs@@ -32,7 +32,7 @@ namespace Grand.Web.Common.Middleware/// <param name="context">HTTP context</param>/// <param name="workContext">workContext</param>/// <returns>Task</returns>- public async Task InvokeAsync(HttpContext context, IWorkContext workContext)+ public async Task InvokeAsync(HttpContext context, IWorkContextSetter workContext){if (context?.Request == null){diff --git a/src/Web/Grand.Web.Common/Startup/StartupApplication.cs b/src/Web/Grand.Web.Common/Startup/StartupApplication.csindex 3944d2ebe..e7e984784 100644--- a/src/Web/Grand.Web.Common/Startup/StartupApplication.cs+++ b/src/Web/Grand.Web.Common/Startup/StartupApplication.cs@@ -79,7 +79,8 @@ namespace Grand.Web.Common.Startup{//work contextserviceCollection.AddScoped<IWorkContext, WorkContext>();-+ serviceCollection.AddScoped<IWorkContextSetter>(provider => provider.GetService<IWorkContext>() as WorkContext);+//helper for SettingsserviceCollection.AddScoped<IStoreHelper, StoreHelper>();diff --git a/src/Web/Grand.Web.Common/WorkContext.cs b/src/Web/Grand.Web.Common/WorkContext.csindex 80d29ff24..6f10c597f 100644--- a/src/Web/Grand.Web.Common/WorkContext.cs+++ b/src/Web/Grand.Web.Common/WorkContext.cs@@ -22,7 +22,7 @@ namespace Grand.Web.Common/// <summary>/// Represents work context for web application / current customer / store / language / currency/// </summary>- public class WorkContext : IWorkContext+ public class WorkContext : IWorkContext, IWorkContextSetter{#region Fieldsdiff --git a/src/Web/Grand.Web/Controllers/CommonController.cs b/src/Web/Grand.Web/Controllers/CommonController.csindex a0d733e23..dc9b19357 100644--- a/src/Web/Grand.Web/Controllers/CommonController.cs+++ b/src/Web/Grand.Web/Controllers/CommonController.cs@@ -144,6 +144,7 @@ namespace Grand.Web.Controllers[DenySystemAccount]public virtual async Task<IActionResult> SetLanguage([FromServices] AppConfig config,+ [FromServices] IWorkContextSetter workContextSetter,string langCode, string returnUrl = default){var language = await _languageService.GetLanguageByCode(langCode);@@ -167,7 +168,7 @@ namespace Grand.Web.ControllersreturnUrl = AddLanguageSeo(returnUrl, language);}- await _workContext.SetWorkingLanguage(language);+ await workContextSetter.SetWorkingLanguage(language);//notificationawait _mediator.Publish(new ChangeLanguageEvent(_workContext.CurrentCustomer, language));@@ -212,12 +213,13 @@ namespace Grand.Web.Controllers[HttpGet]public virtual async Task<IActionResult> SetCurrency([FromServices] ICurrencyService currencyService,+ [FromServices] IWorkContextSetter workContextSetter,[FromServices] IUserFieldService userFieldService,string currencyCode, string returnUrl = ""){var currency = await currencyService.GetCurrencyByCode(currencyCode);if (currency != null)- await _workContext.SetWorkingCurrency(currency);+ await workContextSetter.SetWorkingCurrency(currency);//clear coupon codeawait userFieldService.SaveField(_workContext.CurrentCustomer, SystemCustomerFieldNames.DiscountCoupons,@@ -288,10 +290,12 @@ namespace Grand.Web.Controllers//available even when navigation is not allowed[PublicStore(true)][HttpGet]- public virtual async Task<IActionResult> SetTaxType(int customerTaxType, string returnUrl = default)+ public virtual async Task<IActionResult> SetTaxType(+ [FromServices] IWorkContextSetter workContextSetter,+ int customerTaxType, string returnUrl = default){var taxDisplayType = (TaxDisplayType)Enum.ToObject(typeof(TaxDisplayType), customerTaxType);- await _workContext.SetTaxDisplayType(taxDisplayType);+ await workContextSetter.SetTaxDisplayType(taxDisplayType);//notificationawait _mediator.Publish(new ChangeTaxTypeEvent(_workContext.CurrentCustomer, taxDisplayType));