Open Closed

Where Should AddEntityCache Be Registered #10389


User avatar
0
ademaygun created
  • 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)
  • User Avatar
    0
    AI-Bot created
    Support Team ABP Agent
    • AddEntityCache<TEntity, TKey>() is just a DI registration for IEntityCache<,>, so it should be placed in the module that defines/owns the entity (the bounded context/module where TEntity lives) and that is always included in your application.

      • In a typical layered solution, that means: YourProjectDomainModule (or a dedicated YourProjectDomainCachingModule if 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.
    • Do not put it into the EntityFrameworkCore module 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.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Register and use entity cache in Domain layer.

    Thanks.

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.2.0-preview. Updated on February 02, 2026, 07:17
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.