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.
Example: Cached, Store-Scoped Business Service
Source: src/Business/Grand.Business.Catalog/Services/Tax/TaxCategoryService.cs
This is the canonical shape of a GrandNode business service. Almost every service in Grand.Business.* is a variation on it. Read it alongside .ai/knowledge/caching.md, .ai/knowledge/scoping.md, and .ai/knowledge/domain-events.md.
Dependencies
public class TaxCategoryService : ITaxCategoryService
{
private readonly IRepository<TaxCategory> _taxCategoryRepository;
private readonly IMediator _mediator;
private readonly ICacheBase _cacheBase;
public TaxCategoryService(ICacheBase cacheBase,
IRepository<TaxCategory> taxCategoryRepository,
IMediator mediator)
{
_cacheBase = cacheBase;
_taxCategoryRepository = taxCategoryRepository;
_mediator = mediator;
}
}
Three dependencies, and they are the three a service of this kind almost always has: repository abstraction, cache, mediator. No IMongoDatabase, no IServiceProvider, no IWorkContext — the store id arrives as a parameter.
The class is registered AddScoped in the owning project's IStartupApplication.
Read: cache wraps the query, store id is in the key
public virtual async Task<IList<TaxCategory>> GetAllTaxCategories(string storeId = "")
{
var key = string.Format(CacheKey.TAXCATEGORIES_ALL_KEY, storeId);
return await _cacheBase.GetAsync(key, async () =>
{
var query = _taxCategoryRepository.Table.AsQueryable();
if (!string.IsNullOrEmpty(storeId))
query = query.Where(tc => tc.StoreId == storeId || string.IsNullOrEmpty(tc.StoreId));
return await Task.FromResult(query.OrderBy(tc => tc.DisplayOrder).ToList());
});
}
Four things at once:
- The key constant carries the parameter.
TAXCATEGORIES_ALL_KEYis"Grand.taxcategory.all-{0}", documented inCommonCacheKey.cswith{0} : store ID (empty = all stores). - The store id is part of the key. Omit it and store A serves store B's tax categories. This is a data leak, not a cache miss.
GetAsyncis read-through — the delegate only runs on a miss. There is no separateSetAsync.- Scoping is applied in the query, not after materialization: records with an empty
StoreIdare global and visible to every store; the rest match the current store.
The by-id read has the same shape with a different parameter:
public virtual Task<TaxCategory> GetTaxCategoryById(string taxCategoryId)
{
var key = string.Format(CacheKey.TAXCATEGORIES_BY_ID_KEY, taxCategoryId);
return _cacheBase.GetAsync(key, () => _taxCategoryRepository.GetByIdAsync(taxCategoryId));
}
Note it returns Task<T> directly without async/await — a pure pass-through, per .ai/standards/csharp-style.md.
Write: guard, write, invalidate, publish — in that order
public virtual async Task InsertTaxCategory(TaxCategory taxCategory)
{
ArgumentNullException.ThrowIfNull(taxCategory);
await _taxCategoryRepository.InsertAsync(taxCategory);
await _cacheBase.RemoveByPrefix(CacheKey.TAXCATEGORIES_PATTERN_KEY);
//event notification
await _mediator.EntityInserted(taxCategory);
}
The order is the contract:
ArgumentNullException.ThrowIfNull— the built-in helper, not a hand-written null check.- Repository write.
RemoveByPrefixwith the*_PATTERN_KEYconstant, which clears every key in the family regardless of which store ids happen to be cached.EntityInserted— published after the write succeeded, through theIMediatorextension rather than by constructing the notification.
UpdateTaxCategory and DeleteTaxCategory are identical in structure with EntityUpdated / EntityDeleted. All three invalidate. A service that caches on read but forgets to invalidate on delete serves deleted records until the entry expires.
Cross-family invalidation
Delete does one extra thing:
await _taxCategoryRepository.DeleteAsync(taxCategory);
//clear tax categories cache
await _cacheBase.RemoveByPrefix(CacheKey.TAXCATEGORIES_PATTERN_KEY);
//clear product cache
await _cacheBase.RemoveByPrefix(CacheKey.PRODUCTS_PATTERN_KEY);
//event notification
await _mediator.EntityDeleted(taxCategory);
Products embed a tax category, so cached product projections are stale the moment a tax category disappears. When you add a cached family, ask what else embeds this data in a cached projection, and clear those prefixes too. This is the step most often missed in review.
virtual methods
Every public method is virtual. That is deliberate — plugins replace core services by registering their own implementation, and derived implementations override individual methods. Keep new service methods virtual for consistency with the surrounding code.
What is deliberately absent
| Not here | Where it belongs |
|---|---|
| View models | Grand.Web/Models + a mediator handler |
IWorkContext |
the caller — the service takes storeId explicitly |
| MongoDB filters and collections | inside IRepository<T> |
| Authorization checks | the controller's [PermissionAuthorize] attribute |
| Try/catch around the write | nothing here is an expected failure |
Checklist when writing a service like this
- Constructor takes
IRepository<T>,ICacheBase,IMediator— nothing infrastructural. - Every cached read uses a
CacheKeyconstant with every result-changing parameter formatted in. - Store scope is applied inside the query.
- Every write invalidates with
RemoveByPrefixand the*_PATTERN_KEYconstant. - Every write publishes the matching entity event, after the write.
- Cross-family caches that embed this entity are invalidated too.
- Guard clauses use
ArgumentNullException.ThrowIfNull. - Methods are
virtual.