Original title: Dependency Injection in .NET
Posted by: Mark Siman
Release date: August 2013
At the end of the summer we have a new translation book, “
Dependency Injection in .NET ”, published by Manning Publications. We would like to give your attention to the
translation of a small announcement.
Dependency Injection in .NET is an excellent description of the dependency injection technology (DI), its associated patterns and several of the most important frameworks for such an implementation. Siman has worked many resources (books, journal articles, blog entries) and created his own comprehensive work. Undoubtedly, the author relies on a lot of independently conducted research and his own practical experience in working with dependency injection.
This book fell into my hands very handy. A couple of months ago, I started working on a WPF project using
Prism . One of my colleagues, with whom we had previously worked together, very much recommended this book to me, so I bought a copy. (The fact is that I am just a “bookworm,” it is easiest for me to learn from books. Other people study differently, but this review was written precisely from the point of view of a bookman).
')
Part 1
Part 1 is a review of dependency injection technology. Siman explains what such operations are (called “injections”) and also debunks some of the misconceptions associated with them. The author talks about the merits of dependency injection, in particular, about late binding, extensibility, parallel development, support, and code testability. The author also lays the foundation for the practical application of dependencies, demonstrating good and bad examples of implementing dependency injection and indicating how each such option affects the achievement of goals. Finally, he makes an introduction to other topics that will be covered in the book, and in particular mentions design patterns, dependency injection functionality, as well as the available containers for dependency injection.
Part 2
Part 2 discusses patterns and anti-patterns of dependency injection. Patterns include Designer Injection, Property Injection, Method Injection, and Ambient Context. Siman very intelligently describes these patterns, telling both about their advantages and disadvantages. It immediately becomes clear that the author prefers to use the Designer Injection as often as possible and refers to other patterns only if the Designer Injection cannot be performed. (I would like to note that I decided to adhere to the same principle, although I am working with the introduction of dependencies only recently).
Antipatterns include Dictator, Hybrid Injection, Limited Design, and Locator Services. The fact that the author places Service Locator as antipattern is most controversial. Most experts consider the Service Locator as a completely valid dependency injection pattern (and not an anti-pattern). Siman himself admits that for many years he defended the Locator of services, but in the end he came to the conclusion that the disadvantages of the Locator of services outweigh its advantages. The Prism project (this is a template for creating composite Microsoft applications) has a Service Locator embedded. Now I work in a team using Prism. We constantly use the Locator of services, and I know firsthand about its advantages and disadvantages. At this stage, I can say that the advantages outweigh, and I am ready to work with this pattern further, despite some shortcomings.
Part 2 also has a chapter on refactoring dependency injection. This is a very good overview of what situations involving dependency injection you may encounter in practice. For example, the treatment of cyclic and short-term dependencies is considered. These are really very important topics. Since those dependency injection containers that usually have to work with add an extra level of abstraction, we sometimes forget about potential problems (assuming that the container will deal with them itself).
Part 3
Part 3 considers some more major topics related to dependency injection: object composition, object lifetime, and interception. Object composition determines exactly how dependency injection is applied when creating objects. Siman demonstrates various examples of creating objects — from simple implementations in the console and WPF applications to fairly complex options associated with using ASP.NET and PowerShell launch commands (cmdlets).
The lifespan of objects determines how exactly dependency management proceeds. Do you always return the same instance of dependency, regardless of what element it requests (Singleton)? Or do you always return a new instance of dependency (Short-term style)? Or choose some kind of compromise approach? Siman examines various options for life expectancy, advantages and disadvantages of each of the options. As in other cases, you have to act according to the specific situation and choose the answer in the context of the problem being solved.
Finally, Siman addresses the issue of interception. This idea is to use a container for dependency injection in order to implement end-to-end functions in an application. For example, you can program each of the dependencies in the container to implement logging or error handling. This is a very interesting topic. The author also touches upon aspect-oriented programming (AOP) and compares it with the implementation of dependencies in examples with the solution of the same problems.
Part 4
Part 4 covers several containers for dependency injection. In this part I especially liked the fact that in all chapters Siman consistently uses the same examples. Thus, it focuses not on the example itself, but on the differences in the implementation of specific containers.
Conclusion
The book "The introduction of dependencies in. NET" - a great practical guide. There are not so many books on this topic on the market, which is why it is even more pleasant to study such high-quality work. I recommend this book to all developers who want to master the use of dependency injection technology perfectly.