- No Exception
- Steps to reproduce the issue:
In ABP.io 9.0, the documentation shows registering entity cache with:
context.Services.AddEntityCache<TEntity, TKey>();
In a layered solution (Domain, Application, EntityFrameworkCore, Host), which module is the recommended place to register this?
Should it be in the EntityFrameworkCore module, Application module, or Host?
Also, if I consume the entity cache from the Domain layer, should the configuration be placed in the Domain module, or does this not matter?
Thank you for your help.
2 Answer(s)
-
0
AddEntityCache<TEntity, TKey>()is just a DI registration forIEntityCache<,>, so it should be placed in the module that defines/owns the entity (the bounded context/module whereTEntitylives) and that is always included in your application.- In a typical layered solution, that means:
YourProjectDomainModule(or a dedicatedYourProjectDomainCachingModuleif you prefer to keep registrations separated). - Reason: the cache is part of the domain’s read model optimization for that entity and should be available wherever the domain is used (HTTP API host, workers, tests, etc.) without duplicating registrations.
- In a typical layered solution, that means:
Do not put it into the
EntityFrameworkCoremodule unless you have a very specific reason.- The entity cache is not an EF Core mapping/DbContext concern; it’s an application-wide service.
- Registering it in the EFCore module can also lead to “it works only when EFCore module is referenced” coupling.
Do not put it into the Host module as the primary place.
- Host is composition root; keeping the registration there makes the cache unavailable in other hosts (e.g., a background worker host) unless you remember to duplicate it.
If you consume
IEntityCache<TEntity, TKey>from the Domain layer, registering it in the Domain module is the right choice (so the domain package guarantees that the cache service exists when the domain is used). The consuming layer doesn’t need to care where it’s registered as long as the module dependency chain includes the module that registers it.
Example placement:
using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Modularity; namespace MyCompany.MyProject; public class MyProjectDomainModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { context.Services.AddEntityCache<Product, Guid>(); // context.Services.AddEntityCache<OtherEntity, int>(); } }- Sources:
- https://abp.io/docs/latest/framework/infrastructure/entity-cache
- https://abp.io/docs/10.0//framework/infrastructure/entity-cache
This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.
-
0
hi
Register and use entity cache in
Domain layer.Thanks.