
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.

Differences Between AddTransient, AddScoped and AddSingleton
1. AddTransient
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:

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


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:

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

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:

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.

