Service Lifetime refers to the lifespan or scope of an object in an application. In https://t.co/X4cCxpdMfr when you register a service to the DI Container you have to specify it’s lifetime and this defines how long a service will live and govern how it is shared or accessed across various parts of the application. In https://t.co/X4cCxpdMfr we have three types of service lifetimes, each with different dependency rules:
1. Singleton - A single instance of a service is created once and shared across the application. Ideal for stateful services and services that a expensive to instatiate
2. Scoped - A single instance of a service is created once and shared per logical context usually a HTTP request. Ideal for services that need to share state per request but not across different requests.
3. Transient - A service instance is created each time a service is requested from DI Container. Ideal for lightweight stateless services.
#DotNet #CSharp #Programming #DI #ASPNETCore #SoftwareArchitecture #WebDevelopment #Coding #DevTips
Dependency injection is a crucial aspect of all modern C# applications, allowing us to manage dependencies seamlessly. There are usually a lot of times when service implementation is different depending on the environment. I recently encountered a similar scenario where it was not feasible to invoke a 3rd party API outside of production. My initial thought was to add a conditional statement in the service implementation. Mixing environmental logic and business logic felt like I was going to war with the single responsibility principle. This prompted me to explore cleaner DI patterns:
1. If/else—Clear and concise but can quickly become messy.
2. Factory pattern—Centralizes dependency-resolving logic, independently testable, but it is more verbose.
3. Keyed Services—Dynamic, flexible, and scalable in complex scenarios, but needs key management discipline
Each approach clearly has its benefits and drawbacks, but the choice of which path to take mainly depends on your scenario. Which approach do you prefer? Drop your experience in the comments! 👇
#dotnet #csharp #dependencyinjection #coding #programming #webdev #softwareengineering #cleancode
✅ Benefits
🚀 Better Performance: Only fetch what you need
🔄 Always Fresh Data: Query runs on latest source
🧩 Dynamic Queries: Build complex queries step by step
Key Linq operators that use deferred execution (not an exhaustive list):
AsEnumerable
Distinct
Except
GroupBy
GroupJoin
Intersect
Join
OfType
OrderBy
OrderByDescending
Select
SelectMany
Skip
Take
ThenBy
Union
Where
🧵 LINQ Deferred Execution: The Power of Lazy Loading!
#csharp#dotnet#linq#developers
Deferred execution in LINQ means that the evaluation of a query is delayed until it’s realized value is required.
@KarenPayneMVP@mjovanovictech I’m certainly not among those who would envy the work you tackled. Meanwhile, we’re busy building new features in WCF because that’s what drives the business.
@magonjomarlon For me it goes down to my passion/drive. I think most focus is on the what in our communities, I am you are familar with this phrase when you meet someone "What do you do ?".