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: samples/MvvmSampleUwp/MvvmSampleUwp/Assets/docs/AsyncRelayCommand.md
+3-2Lines changed: 3 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,13 +9,14 @@ dev_langs:
9
9
10
10
# AsyncRelayCommand and AsyncRelayCommand<T>
11
11
12
-
The [AsyncRelayCommand](https://docs.microsoft.com/dotnet/api/microsoft.toolkit.mvvm.input.AsyncRelayCommand) and [AsyncRelayCommand<T>](https://docs.microsoft.com/dotnet/api/microsoft.toolkit.mvvm.input.AsyncRelayCommand-1) are `ICommand` implementations that extend the functionalities offered by `RelayCommand`, with support for asynchronous operations.
12
+
The [`AsyncRelayCommand`](https://docs.microsoft.com/dotnet/api/microsoft.toolkit.mvvm.input.AsyncRelayCommand) and [`AsyncRelayCommand<T>`](https://docs.microsoft.com/dotnet/api/microsoft.toolkit.mvvm.input.AsyncRelayCommand-1) are `ICommand` implementations that extend the functionalities offered by `RelayCommand`, with support for asynchronous operations.
13
13
14
14
## How they work
15
15
16
16
`AsyncRelayCommand` and `AsyncRelayCommand<T>` have the following main features:
17
17
18
-
- They extend the functionalities of the non-asynchronous commands included in the library, with support for `Task`-returning delegates.
18
+
- They extend the functionalities of the synchronous commands included in the library, with support for `Task`-returning delegates.
19
+
- They can wrap asynchronous functions with an additional `CancellationToken` parameter to support cancelation, and they expose a `CanBeCanceled` and `IsCancellationRequested` properties, as well as a `Cancel` method.
19
20
- They expose an `ExecutionTask` property that can be used to monitor the progress of a pending operation, and an `IsRunning` that can be used to check when an operation completes. This is particularly useful to bind a command to UI elements such as loading indicators.
20
21
- They implement the `IAsyncRelayCommand` and `IAsyncRelayCommand<T>` interfaces, which means that viewmodel can easily expose commands using these to reduce the tight coupling between types. For instance, this makes it easier to replace a command with a custom implementation exposing the same public API surface, if needed.
Copy file name to clipboardExpand all lines: samples/MvvmSampleUwp/MvvmSampleUwp/Assets/docs/Introduction.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,6 +33,8 @@ using Microsoft.Toolkit.Mvvm;
33
33
ImportsMicrosoft.Toolkit.Mvvm
34
34
```
35
35
36
+
3. Code samples are available in the other docs pages for the MVVM package, and in the [unit tests](https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/master/UnitTests/UnitTests.Shared/Mvvm) for the project.
37
+
36
38
## When should I use this package?
37
39
38
40
Use this package for access to a collection of standard, self-contained, lightweight types that provide a starting implementation for building modern apps using the MVVM pattern. These types alone are usually enough for many users to build apps without needing additional external references.
@@ -65,5 +67,3 @@ The included types are:
65
67
-`ValueChangedMessage<T>`
66
68
67
69
This package aims to offer as much flexibility as possible, so developers are free to choose which components to use. All types are loosely-coupled, so that it's only necessary to include what you use. There is no requirement to go "all-in" with a specific series of all-encompassing APIs, nor is there a set of mandatory patterns that need to be followed when building apps using these helpers. Combine these building blocks in a way that best fits your needs.
68
-
69
-
Code samples are available in the other docs pages for the MVVM package, and in the [unit tests](https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/master/UnitTests/UnitTests.Shared/Mvvm) for the project.
Copy file name to clipboardExpand all lines: samples/MvvmSampleUwp/MvvmSampleUwp/Assets/docs/Ioc.md
+98-24Lines changed: 98 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,47 +1,82 @@
1
1
---
2
2
title: Ioc
3
3
author: Sergio0694
4
-
description: A type that facilitates the use of the IServiceProvider type
4
+
description: An introduction to the use of the IServiceProvider type through the Microsoft.Extensions.DependencyInjection APIs
5
5
keywords: windows 10, uwp, windows community toolkit, uwp community toolkit, uwp toolkit, mvvm, service, dependency injection, net core, net standard
6
6
dev_langs:
7
7
- csharp
8
8
---
9
9
10
10
# Ioc ([Inversion of control](https://en.wikipedia.org/wiki/Inversion_of_control))
11
11
12
-
The [Ioc](https://docs.microsoft.com/dotnet/api/microsoft.toolkit.mvvm.DependencyInjection.Ioc) class is a type that facilitates the use of the `IServiceProvider` type. It's powered by the `Microsoft.Extensions.DependencyInjection` package, which provides a fully featured and powerful DI set of APIs, and acts as an easy to setup and use `IServiceProvider`.
12
+
Inversion of Control is a common pattern that can be used to increase modularity in the codebase of an application when using the MVVM pattern. A frequently used way to enable this is to use _dependency injection_ (DI), which consists of creating a number of services that are injected into backend classes (i.e. passed as parameters to the viewmodel constructors). Doing this allows code using these services not to rely on the implementation details of these services, and it also makes it easy to swap the concrete implementations of these services. This pattern also makes it easy to make platform-specific features available to backend code, by abstracting them through a service which is then injected where needed. Since services are then isolated from where they are used, they become more testable as well.
13
+
14
+
The MVVM Toolkit doesn't provide built-in APIs to facilitate the usage of this pattern, as dedicated libraries specifically exist for this, such as the `Microsoft.Extensions.DependencyInjection` package. It provides a fully featured and powerful set of dependency injection APIs already and can be easily setup to use the [`IServiceProvider`](https://docs.microsoft.com/dotnet/api/system.iserviceprovider) interface. The following guide will refer to this library and provide a series of examples of how to integrate it into applications using the MVVM pattern with the MVVM Toolkit.
13
15
14
16
## Configure and resolve services
15
17
16
-
The main entry point is the `ConfigureServices` method, which can be used like so:
18
+
The first step is to declare an `IServiceProvider` instance, and to initialize all the necessary services, usually at startup. For instance, on UWP (but a similar setup can be used on other frameworks too):
The `Ioc.Default` property offers a thread-safe `IServiceProvider` instance that can be used anywhere in the application to resolve services. The `ConfigureService` method handles the initialization of that service. It is also possible to create different `Ioc` instances and to initialize each with different services.
58
+
Here the `Services` property is initialized at startup, and all the application services and viewmodels are registered. There is also a new `Current` property that can be used to easily access the `Services` property from other views in the application. For instance:
The key aspect here is that each service may very well be using platform-specific APIs, but since those are all abstracted away through the interface our code is using, we don't need to worry about them whenever we're just resolving an instance and using it to perform operations.
32
67
33
68
## Constructor injection
34
69
35
70
One powerful feature that is available is "constructor injection", which means that the DI service provider is able to automatically resolve indirect dependencies between registered services when creating instances of the type being requested. Consider the following service:
36
71
37
72
```csharp
38
-
publicclassConsoleLogger : ILogger
73
+
publicclassFileLogger : IFileLogger
39
74
{
40
-
privatereadonlyIFileServiceFileService;
75
+
privatereadonlyIFilesServiceFileService;
41
76
privatereadonlyIConsoleServiceConsoleService;
42
77
43
-
publicConsoleLogger(
44
-
IFileServicefileService,
78
+
publicFileLogger(
79
+
IFilesServicefileService,
45
80
IConsoleServiceconsoleService)
46
81
{
47
82
FileService=fileService;
@@ -52,22 +87,61 @@ public class ConsoleLogger : ILogger
52
87
}
53
88
```
54
89
55
-
Here we have a `ConsoleLogger`implementing the `ILogger` interface, and requiring `IFileService` and `IConsoleService` instances. Constructor injection means the DI service provider will "automagically" gather all the necessary services, like so:
90
+
Here we have a `FileLogger` type implementing the `IFileLogger` interface, and requiring `IFilesService` and `IConsoleService` instances. Constructor injection means the DI service provider will automatically gather all the necessary services, like so:
The DI service provider will automatically check whether all the necessary services are registered, then it will retrieve them and invoke the constructor for the registered `IFileLogger` concrete type, to get the instance to return.
112
+
113
+
## What about viewmodels?
114
+
115
+
A service provider has "service" in its name, but it can actually be used to resolve instances of any class, including viewmodels! The same concepts explained above still apply, including constructor injection. Imagine we had a `ContactsViewModel` type, using an `IContactsService` and an `IPhoneService` instance through its constructor. We could have a `ConfigureServices` method like this:
The DI service provider will automatically check whether all the necessary services are registered, then it will retrieve them and invoke the constructor for the registered `ILogger` concrete type, to get the instance to return - all done automatically!
136
+
And then in our `ContactsView`, we would assign the data context as follows:
0 commit comments