Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
adding all services present in the program in addTransient will create instance for these services . This instance creation allocate memory for all service. If there are 200 services then Is this good to allocate memory for all these services at startup ?

public static class ServiceExtensions
    {
        public static IServiceCollection RegisterServices(
            this IServiceCollection services)
        {
            services.AddTransient<ITopicAreaService, TopicAreaService>();
            // Add say 200 services here will allocate memory for all  services at startup 
            //is this good to do so
            return services;
        }
    }


What I have tried:

public static class ServiceExtensions
    {
        public static IServiceCollection RegisterServices(
            this IServiceCollection services)
        {
            services.AddTransient<ITopicAreaService, TopicAreaService>();
            // Add say 200 services here will allocate memory for all  services at startup 
            //is this good to do so
            return services;
        }
    }
Posted
Updated 27-Dec-18 6:06am
v2

1 solution

It is not actually creating the services at this point. Transient services are created as each it resolved from the DI container. All that is allocated is an object that describes the construction of the service and its lifetime.
The techniques that are used to create the instances if very fast and efficient resulting in one of the fastest DI Containers out there.
 
Share this answer
 
Comments
[no name] 27-Dec-18 21:18pm    
In my view allocating memory for all the concrete type present in all say 200 addTransient method is not good at startup.Though you said DI Container maintain these
It's Okay.
My next question is How DI Container knows which service to inject to controller at runtime? Do DI Container inject all Service present in startup.cs to respective controller constructor ?OR does it inject on the basis of Request url ?
Matthew Dennis 28-Dec-18 2:45am    
You need to do some research on how DI Containers and DI work. Try using your friendly Google to find some articles, or use the search on this site to let people k for some articles.
[no name] 28-Dec-18 3:24am    
ok thank you sir.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900