Skip to content

Commit

Permalink
Improved WPF example
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Sep 17, 2024
1 parent 1f379a5 commit 59a048f
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 42 deletions.
26 changes: 13 additions & 13 deletions readme/Avalonia.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,20 @@ A single instance of the _Composition_ class is defined as a static resource in
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AvaloniaApp.App"
xmlns:local="using:AvaloniaApp"
xmlns:app="using:AvaloniaApp"
RequestedThemeVariant="Default">

<!-- "Default" ThemeVariant follows system theme variant.
"Dark" or "Light" are other available options. -->
<Application.Styles>
<FluentTheme />
</Application.Styles>
<!--Creates a shared resource of type `Composition` and with key _‘Composition’_,
which will be further used as a data context in the views.-->
<Application.Resources>
<local:Composition x:Key="Composition" />
</Application.Resources>
<!-- "Default" ThemeVariant follows system theme variant.
"Dark" or "Light" are other available options. -->
<Application.Styles>
<FluentTheme />
</Application.Styles>

<!--Creates a shared resource of type `Composition` and with key _‘Composition’_,
which will be further used as a data context in the views.-->
<Application.Resources>
<app:Composition x:Key="Composition" />
</Application.Resources>

</Application>
```
Expand All @@ -69,7 +69,7 @@ This markup fragment

```xml
<Application.Resources>
<local:Composition x:Key="Composition" />
<app:Composition x:Key="Composition" />
</Application.Resources>
```

Expand Down
26 changes: 13 additions & 13 deletions readme/AvaloniaPageTemplate.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,20 @@ A single instance of the _Composition_ class is defined as a static resource in
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AvaloniaApp.App"
xmlns:local="using:AvaloniaApp"
xmlns:app="using:AvaloniaApp"
RequestedThemeVariant="Default">

<!-- "Default" ThemeVariant follows system theme variant.
"Dark" or "Light" are other available options. -->
<Application.Styles>
<FluentTheme />
</Application.Styles>
<!--Creates a shared resource of type `Composition` and with key _‘Composition’_,
which will be further used as a data context in the views.-->
<Application.Resources>
<local:Composition x:Key="Composition" />
</Application.Resources>
<!-- "Default" ThemeVariant follows system theme variant.
"Dark" or "Light" are other available options. -->
<Application.Styles>
<FluentTheme />
</Application.Styles>

<!--Creates a shared resource of type `Composition` and with key _‘Composition’_,
which will be further used as a data context in the views.-->
<Application.Resources>
<app:Composition x:Key="Composition" />
</Application.Resources>

</Application>
```
Expand All @@ -69,7 +69,7 @@ This markup fragment

```xml
<Application.Resources>
<local:Composition x:Key="Composition" />
<app:Composition x:Key="Composition" />
</Application.Resources>
```

Expand Down
49 changes: 45 additions & 4 deletions readme/Wpf.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

This example demonstrates the creation of a WPF application in the pure DI paradigm using the _Pure.DI_ code generator.

The definition of the composition is in [Composition.cs](/samples/WpfAppNetCore/Composition.cs). You must not forget to define any necessary composition roots, for example, these can be view models such as _ClockViewModel_:
The definition of the composition is in [Composition.cs](/samples/WpfAppNetCore/Composition.cs). This class setups how the composition of objects will be created for the application. You must not forget to define any necessary composition roots, for example, these can be view models such as _ClockViewModel_:

```csharp
using Pure.DI;
Expand All @@ -30,22 +30,47 @@ internal partial class Composition
}
```

Advantages over classical DI container libraries:
- No performance impact or side effects when creating composition of objects.
- All logic for analyzing the graph of objects, constructors and methods takes place at compile time. Pure.DI notifies the developer at compile time of missing or cyclic dependencies, cases when some dependencies are not suitable for injection, etc.
- Does not add dependencies to any additional assembly.
- Since the generated code uses primitive language constructs to create object compositions and does not use any libraries, you can easily debug the object composition code as regular code in your application.

A single instance of the _Composition_ class is defined as a static resource in [App.xaml](/samples/WpfAppNetCore/App.xaml) for later use within the _xaml_ markup everywhere:

```xaml
<Application x:Class="WpfAppNetCore.App" x:ClassModifier="internal"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfAppNetCore="clr-namespace:WpfAppNetCore"
xmlns:app="clr-namespace:WpfAppNetCore"
StartupUri="/Views/MainWindow.xaml"
Exit="OnExit">

<!--Creates a shared resource of type `Composition` and with key _‘Composition’_,
which will be further used as a data context in the views.-->
<Application.Resources>
<wpfAppNetCore:Composition x:Key="Composition"/>
<app:Composition x:Key="Composition" />
</Application.Resources>

</Application>
```

All previously defined composition roots are now accessible from [markup](/samples/WpfAppNetCore/Views/MainWindow.xaml) without any effort, such as _ClockViewModel_:
This markup fragment

```xml
<Application.Resources>
<app:Composition x:Key="Composition" />
</Application.Resources>
```

creates a shared resource of type `Composition` and with key _‘Composition’_, which will be further used as a data context in the views.

Advantages over classical DI container libraries:
- No explicit initialisation of data contexts is required. Data contexts are configured directly in `.axaml` files according to the MVVM approach.
- The code is simpler, more compact, and requires less maintenance effort.
- The main window is created in a pure DI paradigm, and it can be easily supplied with all necessary dependencies via DI as regular types.

You can now use bindings to model views without even editing the views `.cs` code files. All previously defined composition roots are now accessible from [markup](/samples/WpfAppNetCore/Views/MainWindow.xaml) without any effort, such as _ClockViewModel_:

```xaml
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Expand All @@ -59,6 +84,22 @@ All previously defined composition roots are now accessible from [markup](/sampl
</Window>
```

To use bindings in views:

- You can set a shared resource as a data context

`DataContext="{StaticResource Composition}"`

- Use the bindings as usual:

`Title="{Binding ClockViewModel.Time}"`

Advantages over classical DI container libraries:
- The code-behind `.cs` files for views are free of any logic.
- This approach works just as well during design time.
- You can easily use different view models in a single view.
- Bindings depend on properties through abstractions, which additionally ensures weak coupling of types in application. This is in line with the basic principles of DI.

The [project file](/samples/WpfAppNetCore/WpfAppNetCore.csproj) looks like this:

```xml
Expand Down
49 changes: 45 additions & 4 deletions readme/WpfPageTemplate.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

This example demonstrates the creation of a WPF application in the pure DI paradigm using the _Pure.DI_ code generator.

The definition of the composition is in [Composition.cs](/samples/WpfAppNetCore/Composition.cs). You must not forget to define any necessary composition roots, for example, these can be view models such as _ClockViewModel_:
The definition of the composition is in [Composition.cs](/samples/WpfAppNetCore/Composition.cs). This class setups how the composition of objects will be created for the application. You must not forget to define any necessary composition roots, for example, these can be view models such as _ClockViewModel_:

```csharp
using Pure.DI;
Expand All @@ -30,22 +30,47 @@ internal partial class Composition
}
```

Advantages over classical DI container libraries:
- No performance impact or side effects when creating composition of objects.
- All logic for analyzing the graph of objects, constructors and methods takes place at compile time. Pure.DI notifies the developer at compile time of missing or cyclic dependencies, cases when some dependencies are not suitable for injection, etc.
- Does not add dependencies to any additional assembly.
- Since the generated code uses primitive language constructs to create object compositions and does not use any libraries, you can easily debug the object composition code as regular code in your application.

A single instance of the _Composition_ class is defined as a static resource in [App.xaml](/samples/WpfAppNetCore/App.xaml) for later use within the _xaml_ markup everywhere:

```xaml
<Application x:Class="WpfAppNetCore.App" x:ClassModifier="internal"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfAppNetCore="clr-namespace:WpfAppNetCore"
xmlns:app="clr-namespace:WpfAppNetCore"
StartupUri="/Views/MainWindow.xaml"
Exit="OnExit">

<!--Creates a shared resource of type `Composition` and with key _‘Composition’_,
which will be further used as a data context in the views.-->
<Application.Resources>
<wpfAppNetCore:Composition x:Key="Composition"/>
<app:Composition x:Key="Composition" />
</Application.Resources>

</Application>
```

All previously defined composition roots are now accessible from [markup](/samples/WpfAppNetCore/Views/MainWindow.xaml) without any effort, such as _ClockViewModel_:
This markup fragment

```xml
<Application.Resources>
<app:Composition x:Key="Composition" />
</Application.Resources>
```

creates a shared resource of type `Composition` and with key _‘Composition’_, which will be further used as a data context in the views.

Advantages over classical DI container libraries:
- No explicit initialisation of data contexts is required. Data contexts are configured directly in `.axaml` files according to the MVVM approach.
- The code is simpler, more compact, and requires less maintenance effort.
- The main window is created in a pure DI paradigm, and it can be easily supplied with all necessary dependencies via DI as regular types.

You can now use bindings to model views without even editing the views `.cs` code files. All previously defined composition roots are now accessible from [markup](/samples/WpfAppNetCore/Views/MainWindow.xaml) without any effort, such as _ClockViewModel_:

```xaml
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Expand All @@ -59,6 +84,22 @@ All previously defined composition roots are now accessible from [markup](/sampl
</Window>
```

To use bindings in views:

- You can set a shared resource as a data context

`DataContext="{StaticResource Composition}"`

- Use the bindings as usual:

`Title="{Binding ClockViewModel.Time}"`

Advantages over classical DI container libraries:
- The code-behind `.cs` files for views are free of any logic.
- This approach works just as well during design time.
- You can easily use different view models in a single view.
- Bindings depend on properties through abstractions, which additionally ensures weak coupling of types in application. This is in line with the basic principles of DI.

The [project file](/samples/WpfAppNetCore/WpfAppNetCore.csproj) looks like this:

```xml
Expand Down
4 changes: 2 additions & 2 deletions samples/AvaloniaApp/App.axaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AvaloniaApp.App"
xmlns:local="using:AvaloniaApp"
xmlns:app="using:AvaloniaApp"
RequestedThemeVariant="Default">

<!-- "Default" ThemeVariant follows system theme variant.
Expand All @@ -13,7 +13,7 @@
<!--Creates a shared resource of type `Composition` and with key _‘Composition’_,
which will be further used as a data context in the views.-->
<Application.Resources>
<local:Composition x:Key="Composition" />
<app:Composition x:Key="Composition" />
</Application.Resources>

</Application>
4 changes: 2 additions & 2 deletions samples/AvaloniaSimpleApp/App.axaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:avaloniaSimpleApp="clr-namespace:AvaloniaSimpleApp"
xmlns:app="clr-namespace:AvaloniaSimpleApp"
x:Class="AvaloniaSimpleApp.App"
RequestedThemeVariant="Default">

Expand All @@ -11,7 +11,7 @@
</Application.Styles>

<Application.Resources>
<avaloniaSimpleApp:Composition x:Key="Composition" />
<app:Composition x:Key="Composition" />
</Application.Resources>

</Application>
4 changes: 2 additions & 2 deletions samples/SingleRootAvaloniaApp/App.axaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AvaloniaApp.App"
xmlns:local="using:AvaloniaApp"
xmlns:app="using:AvaloniaApp"
RequestedThemeVariant="Default">

<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->
Expand All @@ -12,7 +12,7 @@
<!--Creates a shared resource of type `Composition` and with key _‘Composition’_,
which will be further used as a data context in the views.-->
<Application.Resources>
<local:Composition x:Key="Composition" />
<app:Composition x:Key="Composition" />
</Application.Resources>

</Application>
8 changes: 6 additions & 2 deletions samples/WpfAppNetCore/App.xaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<Application x:Class="WpfAppNetCore.App" x:ClassModifier="internal"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfAppNetCore="clr-namespace:WpfAppNetCore"
xmlns:app="clr-namespace:WpfAppNetCore"
StartupUri="/Views/MainWindow.xaml"
Exit="OnExit">

<!--Creates a shared resource of type `Composition` and with key _‘Composition’_,
which will be further used as a data context in the views.-->
<Application.Resources>
<wpfAppNetCore:Composition x:Key="Composition" />
<app:Composition x:Key="Composition" />
</Application.Resources>

</Application>

0 comments on commit 59a048f

Please sign in to comment.