Code Monkey home page Code Monkey logo

dilifetime's Introduction

Dependency Injection (DI)

Dependency Injection (DI) in .NET Core is a design pattern and a built-in feature that allows the separation of dependencies from the classes that use them.

In DI, instead of directly creating and managing dependencies within a class, the dependencies are provided (injected) from built in DI container,.

The DI container keeps track of the registered services and their lifetimes, and it automatically resolves dependencies when requested.

  • Transient Lifetime
  • Scoped Lifetime
  • Singleton Lifetime

Advantage of DI

It enables loose coupling between classes, as they depend on abstractions (interfaces) rather than concrete implementations. This makes it easier to replace or modify dependencies without impacting the consuming classes.

Service Lifetime

service lifetime refers to the lifespan or duration of a registered service within the DI container. It determines how long an instance of a service will be maintained.

Transient

  • A new instance of the service is created every time it is requested..
  • Transient services are suitable for lightweight and stateless components that are not thread-safe or have a short lifespan.

Let's consider a scenario where you have an application that needs to generate unique random codes for each customer request

Scoped

  • A single instance of the service is created and shared within the scope of an HTTP request or a specified scope.
  • Each new HTTP request or scope will receive a new instance of the service.
  • Scoped services are commonly used for components that require per-request or per-scope state.

Let's consider a scenario where you have a shopping cart in your web application that needs to persist across multiple requests within the scope of a user session.

Singleton

  • One instance for the entire lifetime of the application.A single instance of the service is created and shared throughout the lifetime of the application.
  • The same instance is used for all requests or scopes.
  • Singleton services are suitable for stateless or thread-safe components that can be safely shared across multiple requests or scopes.

Let's consider a scenario where you have a logging service in your application that needs to be shared across all components and maintain a consistent state

How to run

Run dotnet watch command inside the DILifeTime directory

Screens

DILifeTime.png

dilifetime's People

Contributors

rahman3593 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.