M E D Y A T Ö R

.NET Core MVC Dependency Management: What is the Difference Between AddTransient, AddScoped, AddSingleton?

14

KASIM

2024


View

   In the .NET Core MVC development process, dependency management (DI “Dependency Injection”) plays a very important role in terms of flexibility, performance and maintainability of the software. Managing dependencies efficiently makes our application more readable. So, what are AddTransient, AddScoped and AddSingleton and how do they work? Understanding the differences between them will guide you on which dependency lifetime you should choose in your application.
Purpose of Dependency Injection and Dependency Management

Purpose of Dependency Injection and Dependency Management

Dependency Injection, In short, DI (Dependency Injection) means that a class imports the objects it depends on from outside. This approach provides flexibility by preventing classes from being directly dependent on each other. For example, let's assume that the "Order" class in an e-commerce application is dependent on the "Product" class. Dependency Injection allows the “Order” class to be injected from outside instead of creating the “Product” class directly. This increases the manageability of the application. Methods such as AddTransient, AddScoped and AddSingleton are used to manage dependency injection in .NET Core MVC. Each method deals with the lifespan of addiction, that is, its lifespan, with a different approach.



Purpose of Dependency Injection and Dependency Management


Differences Between AddTransient, AddScoped and AddSingleton

1. AddTransient


Differences Between AddTransient, AddScoped and AddSingleton


AddTransient creates the dependency as a new object with each request. So this lifetime provides a copy of the object every time it is used. This feature is ideal for short-lived objects and standalone tasks.
Example:

Differences Between AddTransient, AddScoped and AddSingleton

This code is executed when IEmailService is requested Creates a new EmailService object each time. For example, AddTransient would make sense if you want to start the process of sending an email from scratch every time.

2. AddScoped

Differences Between AddTransient, AddScoped and AddSingleton


AddScoped creates the object once in each HTTP request and the same object is used throughout that request. . That is, objects specified as AddScoped within the same request are created only once, but when another request comes, an object instance is created again.
Example:

3. AddSingleton


Here a single OrderService object is used for IOrderService throughout the same HTTP request. This feature is ideal for data that should only be used in one session or request, such as user transactions.

3. AddSingleton


3. AddSingleton


AddSingleton creates an object only once when the application is started and returns the same object in all requests. uses. This is the best choice for data that is long-term or shared among all users.
Example:

3. AddSingleton

Here the ILoggingService object is It is created only once in its lifetime. This approach is suitable for data such as logging, configuration settings or cache that will be used throughout the application.

Which Lifetime Should We Choose?

  1. 1. AddTransient: Ideal for short-lived objects. Use for light and standalone tasks.
  2. 2. AddScoped: Optimal for tasks that require per-user or per-request processing. For example, user-specific data processing.
  3. 3. AddSingleton: Best choice for data shared across the application or immutable data.
  4. OrderService: AddScoped is preferred for order management. can be done, because order transactions are specific to each user.
  5. UserAuthenticationService: Such transactions become more flexible with AddTransient, because user authentication is renewed for each session.
  6. Log Records (LoggingService): It makes sense to use AddSingleton since it is a service shared among all users.
  7. If AddTransient is used too frequently, it may consume a lot of memory as each request will create a new object. p>
  8. When used correctly, AddScoped improves performance by avoiding re-rendering duplicate objects within the same request.
  9. AddSingleton saves memory for long-running objects, but should only be used for secure operations because the same object must be used in each operation.

3. AddSingleton

Let's Examine It Through a Practical Example

Let's assume you are working on an e-commerce application. You need to plan how you will manage operations such as order management, user transactions and log records:

With this strategy, you can both increase performance and optimize the security and availability of data.

The Effect of Dependency Management on Performance

Memory in any application usage and processing performance are extremely important, especially in large-scale projects. Choosing the right lifetime directly affects performance:

3. AddSingleton

Dependency management with AddTransient, AddScoped, and AddSingleton in .NET Core MVC plays a key role in improving the performance, maintainability, and security of your project. By understanding the intended use of each lifetime, you can establish a more efficient structure in your application. AddTransient for short-lived tasks, AddScoped for operations requiring per-user operations, and AddSingleton for objects shared across the application are the right choices.