/
eu ropean con ference Dependency injection eu ropean con ference Dependency injection

eu ropean con ference Dependency injection - PowerPoint Presentation

olivia
olivia . @olivia
Follow
66 views
Uploaded On 2023-09-22

eu ropean con ference Dependency injection - PPT Presentation

How to decouple your code modules What is dependency injection Put appropriate instances in dont let the object create them Thanks for your atten wait wait its a bit more complicated ID: 1019338

dependency container type injection container dependency injection type principle code types service factory responsibility design classes instances existing details

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "eu ropean con ference Dependency injecti..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1. europeanconference

2. Dependency injectionHow to decouple your code modules

3. What is dependency injection?“Put appropriate instances in; don’t let the object create them.”Thanks for your atten… wait, wait, its “a bit” more complicated…

4. What is SOLID?S - Single responsibility principle (SRP)O - Open/closed principle (OCP)L - Liskov substitution principle (LSP)I - Interface segregation principle (ISP)D - Dependency inversion principle (DIP)

5. Single responsibility principleA class should only have one responsibilityhaving problems finding a name is a sign of a violationcreating things is a responsibility -> factoryrelated to separation of concerns (SoC)

6. Dependency inversion principleAbstractions should not depend on details. Details should depend on abstractions"Code against interfaces (i.e. abstractions) not implementations (details)"

7. What is dependency injection"Dependency Injection is a 25-dollar term for a 5-cent concept.""Don't look for things - ask for them!" (Misko Hevery)

8. Why Dependency Injection?Decoupling of software pieces (classes)SRPflexibilitycomposabilityexchangeabilitytestability (mocking)maintainability

9. „To create or not to create“Injectables and creatables – who is who?Example for creatables:All kinds of collections to store data or inner state(lists, dictionaries, TStrings, dynamic arrays, TStream)PODOs (aka data objects) – but sometimes not directly but with a factory depending on other requirementsInjectables are those things that actually do something often called services

10. Reading suggestions

11. More materialNot only purely on dependency injection but in general useful things about software design and architecturehttps://blog.ploeh.dk (Blog of Mark Seemann)http://misko.hevery.com/code-reviewers-guide/(Guide to write testable code)https://www.youtube.com/playlist?list=PL693EFD059797C21E (Google Clean Code Talks)

12. Pure DILearn and understand how DI works, what patterns and practices are helpful before using an automated wayTry putting your dependencies together manually to get a feeling for how to design your classes and interfacesOnce you‘ve written pages and pages of constructor calls, consider using a DI container

13. Service LocatorDI is about passing things (to the constructor basically)Service Locator is about looking for things activelyCode smell and dangerous anti-patternCreates dependencies not only on the service locator but implicitly on types that need to be existing in the service locatorNot easily noticable when creating a class but only when some method gets called

14. Composition rootThe point where DI starts – ideally there is only one and it‘s at the very start of the applicationWhen trying to fit DI into an existing application you might have multiple at firstMove them closer to the start of the application and eventually they will converge

15. DI Container (finally!...)Factory pattern on steroidsAnd a repository, and some other design patterns A central place to register all your injectables and let them be created and composed automatically

16. Registering typesRegisterTypeTell the container what implementing type is available and as what type it can resolve itPossibility to delegate the creation to another place than the container itselfRegisterInstanceTell the container about an existing instance to let it participate in dependency injection

17. Registering types (advanced)RegisterFactoryRegister a type and let the container provide its implemention which then serves as factory to return instances returned by the containerRegisterDecoratorRegister a type and let the container use it as decorator for other classes when it resolves them and automatically apply them

18. Controlling lifetimesNew instance every time or use only one?Transient vs SingletonPossibility to control when new instances are being created or already created ones are returnedSingleton, SingletonPerThread, PerResolve, Pooled

19. Type providersPossibility to extend the container to give it special knowledge about specific types and how to build themExamples of built-in providers are: LazyProvider, ListProvider, DynamicArrayProvider

20. Types of injectionConstructor Injectionconstructor parametersmandatory dependenciesProperty Injectionproperties/setteroptional dependenciesNull-Object patternField InjectionUse only in exceptional cases – this is not possible with pure DI

21. Controlling the compositionTypes can be namedAll injection targets can be marked with the [Inject] attribute to control where and what is being injectedAdditional conditions can be applied to types and injection targets (planned)

22. Let‘s take a look at some example

23. Container.Free