Moonglade

Modern Markdown blogging with AI-powered localization and search.

Tag: DependencyInjection


Still memorizing the concept of dependency injection? Why not write your own dependency injection framework?

This article introduces the core implementation and testing process of KDI, a custom Dependency Injection (DI) framework built with C#. KServiceProvider registers services by iterating through KServiceCollection. For the Transient lifetime, it uses Func delegates to ensure a new instance is created every time a service is requested. For the Singleton lifetime, it leverages Lazy objects to implement lazy loading while guaranteeing global uniqueness. Through Guid service tests, the correctness of both lifetimes was verified: Singleton returns the same instance on multiple requests, whereas Transient returns a different instance each time. The article concludes with a profound critique of exam-oriented programming learning mindsets, pointing out that Scoped and Singleton behaviors have fundamental differences in console applications. Blindly applying interview techniques can lead to misunderstandings of underlying principles. The author emphasizes the importance of deeply understanding Dependency Injection mechanisms rather than rote memorization, and reminds readers to be aware of the security risks associated with the Service Locator pattern in production environments, as well as the room for improvement in the framework. Developers are encouraged to abandon rigid thinking and cultivate genuine creativity.

Views 130
.NET Dependency Injection DependencyInjection Design Patterns Service Lifecycles Software Engineering

Do You Really Need Autofac? Scrutor: A Lighter Container Companion

The article first dissects the ServiceLocator anti-pattern, pointing out that accessing services through global static instances in ASP.NET Core leads to high code coupling, difficulty in testing, and poor scalability. It recommends switching to the standard dependency injection container. Subsequently, the article introduces Scrutor, a lightweight extension library, as an effective alternative to Autofac. Scrutor supports batch service registration through assembly scanning and provides various flexible configuration methods, including registration based on name matching or implemented interfaces, restricting to specific namespaces, uniformly setting lifetimes, and precisely controlling the lifetimes of different service instances (Transient, Scoped, Singleton) via marker interfaces or custom Attributes. Additionally, Scrutor simplifies the implementation of the decorator pattern, requiring only two lines of code to complete decorator registration. In summary, if a project primarily relies on Autofac for batch registration or implementing decorators, and does not deeply utilize the ServiceLocator, Scrutor can serve as an ideal lightweight alternative with less code and higher readability, helping developers simplify dependency management while maintaining best practices.

Views 147
.NET BatchRegistration DecoratorPattern DependencyInjection LightweightLibrary ServiceLocator