Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Singletons and which scope do they use #7070


User avatar
0
antonis created

How are classes marked as singleton are initialized? Which scope do they use to be created?

Thanks in advance


7 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    I recommend you check this article https://medium.com/volosoft/asp-net-core-dependency-injection-best-practices-tips-tricks-c6e9c67f9d96

  • User Avatar
    0
    antonis created

    @mailming I understand how DI works. What I am asking how are they being initialized in abp?

  • User Avatar
    0
    maliming created
    Support Team

    They are initialized by the DI container, and abp uses [Windsor] (https://github.com/castleproject/Windsor).

    Singleton components will only produce a single instance that is bound to the container. The instance will be created the first time someone requests it, and subsequently reused every time it's needed

    Abp provides the IShouldInitialize interface. https://aspnetboilerplate.com/Pages/Documents/Dependency-Injection#ishouldinitialize-interface

    Dependency injection containers can also use factory to create components.

  • User Avatar
    0
    antonis created

    The instance will be created the first time someone requests it, and subsequently reused every time it's needed Take as example the EntityCache which is a singleton. It is using the dbcontext to get the entity from database if I am not mistaked. The first request that comes in the controller that uses an EntityCache is gonna use the dbcontext that was created for the first request. Is that right?

  • User Avatar
    0
    maliming created
    Support Team

    EntityCache not a singleton service. SettingManager is a singleton service.

    It's usually the first request, but it will be created if you use it before requesting it, then reuse it. For example, call it when the application starts.

    What problem have you encountered?

  • User Avatar
    0
    antonis created

    Where can I call it?

  • User Avatar
    0
    maliming created
    Support Team

    For example, AppTimes, it will be created when the application is launched.