Skip to content

Document when to use Grand.Mediator; inject IGroupService into CustomerService - #1177

Merged
grandnode-dev merged 3 commits into
developfrom
refactor/mediator-usage-rules
Dec 3, 2026
Merged

grandnode-dev merged 3 commits into
developfrom
refactor/mediator-usage-rules

Conversation

@grandnode-dev

Copy link
Copy Markdown
Member

Type: bugfix

Issue

The .ai/ guidance for agents said every write goes through a mediator command, every read through a query, and controllers only call _mediator.Send. The code does not work that way:

  • business services (I<Noun>Service) are the business API, and most writes go through them;
  • the admin panels (Admin, Store, Vendor, AdminShared) define no request handlers and build screens through I<Thing>ViewModelService;
  • the storefront builds view models through queries in Grand.Web/Features, and some actions call a service directly.

An agent following the old rule would add handlers where none belong, and a second way to reach operations that already have a service method.

In the code, CustomerService reached GroupService through GetGroupBySystemNameQuery, a handler that only called IGroupService. The two services have no dependency cycle, so the mediator hop only hid a dependency.

Solution

  • .ai/knowledge/architecture.md: the section "Grand.Mediator — when to use it" replaces the old rule. It has a table of where each host uses a service, a view-model service, a query, a command or a notification, plus short rules: one entry point per operation, queries have no side effects, no service-to-service Send when constructor injection works, and requests are not an extension point. Existing exceptions are listed so they are not copied.
  • request-lifecycle.md, domain-events.md, constraints.md, naming.md and prompts/implement-feature.md are aligned with that section.
  • CustomerService injects IGroupService. GetGroupBySystemNameQuery, its handler and its handler test are removed (no other caller).

Breaking changes

  • GetGroupBySystemNameQuery (Grand.Business.Core.Queries.Customers) is removed. A plugin that sent it should call IGroupService.GetCustomerGroupBySystemName instead.
  • The CustomerService constructor takes IGroupService. This matters only to code that constructs it by hand; DI resolves it automatically.

Testing

  1. dotnet test src/Tests/Grand.Business.Customers.Tests — 157 passed.
  2. dotnet test GrandNode.slnx — every project passes except Grand.Module.Api.Tests (4) and Grand.Business.Catalog.Tests (4). Those are inventory tests that share the Mongo test database during a parallel run. Run alone, both projects pass (144/144, 1232/1232).
  3. Start the storefront as a new visitor: a guest customer is created in the Guests group (InsertGuestCustomer).

support added 2 commits December 3, 2026 13:34
CustomerService reached GroupService through GetGroupBySystemNameQuery, whose
handler only called IGroupService. There is no dependency cycle between the
two services, so the mediator hop only hid a dependency. The query and its
handler had no other caller and are removed.
The guidance said every write goes through a command, every read through a
query, and controllers only call the mediator. The code does not work that
way: business services are the business API, the admin panels use view-model
services and define no request handlers, and the storefront uses queries for
view models. Replace the rule with a table of where each host uses the
mediator, and rules against a second entry point to the same operation.
Copilot AI balanced review requested due to automatic review settings December 3, 2026 12:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

{
private MemoryCacheBase _cacheBase;
private CustomerService _customerService;
private Mock<IGroupService> _groupServiceMock;
List the reads named as commands, MaxOrderNumberCommand and the existing direct sends as exceptions; add rows for business queries, admin use-case commands and dedicated API queries; replace the examples with CancelOrderCommand, GetBlogPost and the current GetSuggestedProductsQuery; align constraints, fix-bug and explore-repository; comment the plugin boundary in DiscountService.
@grandnode-dev
grandnode-dev merged commit 8acf5d4 into develop Dec 3, 2026
9 checks passed
@grandnode-dev
grandnode-dev deleted the refactor/mediator-usage-rules branch December 3, 2026 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants