You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guides/implementation-guides/how-to-implement-dependency-injection.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ This guide will show you step by step how to use Dependency Injection (DI) with
11
11
12
12
## Step 0: Context and Initial Code
13
13
14
-
Let's assume that you have an app with a MainViewModel, a BusinessService and a Repository. MainViewModel has a dependency on IBusinessService and BusinessService on IRepository. A simple implementation would look like this:
14
+
Let's assume that you have an app with a `MainViewModel`, a `BusinessService` and a `Repository`. `MainViewModel` has a dependency on `IBusinessService` and `BusinessService` on `IRepository`. A simple implementation would look like this:
15
15
16
16
```csharp
17
17
publicpartialclassMainViewModel
@@ -43,7 +43,7 @@ public class Repository : IRepository
43
43
}
44
44
```
45
45
46
-
Typically you would directly instantiate `Repository` and pass it into `BusinessService` then pass it into `MainViewModel`, like this:
46
+
Traditionally, you would directly instantiate `Repository` and pass it into `BusinessService` then pass it into `MainViewModel`, like this:
47
47
48
48
```csharp
49
49
varwindow=newMainWindow
@@ -52,12 +52,12 @@ var window = new MainWindow
52
52
}
53
53
```
54
54
55
-
Thisworksgreatforsimpleconstructorsthatarenotusedveryoftenanddon't change. But this technique does not scale very well because:
Dependencyinjectionsolvestheseproblembyabstractingawaythecreationofobjectsandtheirdependencies. Thisallowsfor well encapsulated services to be used that will be automatically passed into any other service that is registered to use them.
60
+
Dependencyinjectionsolvestheseproblembyabstractingawaythecreationofobjectsandtheirdependencies. Thisallowsfor well encapsulated services that will be automatically passed into any other service that is registered to use them.
61
61
62
62
## Step 1: Install the NuGet package for DI
63
63
There are many dependency injection (DI) containerprovidersavailable ([DryIoC](https://github.com/dadhi/DryIoc), [Autofac](https://github.com/autofac/Autofac), [Pure.DI](https://github.com/DevTeam/Pure.DI)) but this guide will only focus on `Microsoft.Extensions.DependencyInjection` which is a lightweight, extensible dependency injection container. It provides an easy-to-use and convention-based way to add DI to .NET applications, including Avalonia-based desktop applications.
0 commit comments