Dependency Injection (DI)¶
Dependency Injection is a technique that helps us write flexible, maintainable, and testable code by providing objects (dependencies) from outside a class instead of creating them inside the class
. This reduces tight coupling
between components, making it easier to change implementations without modifying too much code.
Not to be confused with WPF's dependency properties
.
Instead of a class creating its own dependencies, they are "injected" from the outside. This makes it easy to replace components when needed, for example, using a different database provider or mocking a service for testing.
Benefits: - Loose Coupling – Classes depend on abstractions (interfaces) instead of concrete implementations. - Better Testability – Mock dependencies for unit tests instead of relying on actual implementations. - Easier Maintenance – Change implementations without modifying dependent classes. - Reusability – Components can be used in different applications without modification.
Constructor Injection¶
Dependencies are provided through the class constructor.
Property Injection¶
Dependencies are set via public properties instead of the constructor.
Method Injection¶
Dependencies are passed as method parameters.
Using an IoC Container¶
Manually injecting dependencies works, but in real projects, we use IoC (Inversion of Control)
containers like the built-in .NET DI container.
WPF Dependency Properties (DP)¶
A Dependency Property is an advanced property system used in Windows Presentation Foundation (WPF) that allows properties to be automatically updated
based on styles, data bindings, animations, and inheritance. It is different from regular C# properties because it stores values in a property store
rather than in a private field.
Dependency properties are useful because they: - Enable data binding (e.g., binding UI elements to data) - Support styles and triggers (change properties dynamically) - Work with animations (WPF animations can modify dependency properties) - Allow default values and value inheritance (values can be inherited from parent controls)
A normal property does not support these features, but a dependency property does.
Creating a Dependency Property¶
A dependency property must be registered in a class that inherits from DependencyObject
.
- The property is registered using DependencyProperty.Register.
- We store the property in a static DependencyProperty field.
- We wrap it in a normal C# property (MyText) that uses GetValue and SetValue.
Example:
Attached Dependency Properties¶
An Attached Property
is a special kind of dependency property that is declared in one class but used by other controls.
A common example is the Grid.Column property:
The Grid.Column property does not belong to Button, but the Grid class allows Button to use it.
Creating an Attached Dependency Property¶
With this, other elements can use Grid.Column, even though it's defined in the Grid class.