generated from dailydevops/dotnet-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: Finished implementation, added tests, added description and RE…
…ADME (#4) * feat: Finished implementation, added tests, added description and README * docs: Updated exception infos * fix: Registered `IFormularProvider` * chore: Added Integration Tests * chore: Removed unused classes and activated sonarqube * chore: Hide TestForm
- Loading branch information
Showing
23 changed files
with
1,476 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,67 @@ | ||
# template-dotnet | ||
.NET template for repositories | ||
# NetEvolve.Extensions.Hosting.WinForms | ||
|
||
[![Nuget](https://img.shields.io/nuget/v/NetEvolve.Extensions.Hosting.WinForms?logo=nuget)](https://www.nuget.org/packages/NetEvolve.Extensions.Hosting.WinForms/) | ||
[![Nuget](https://img.shields.io/nuget/dt/NetEvolve.Extensions.Hosting.WinForms?logo=nuget)](https://www.nuget.org/packages/NetEvolve.Extensions.Hosting.WinForms/) | ||
|
||
The main purpose of this package is to provide a way to use the `Microsoft.Extensions.Hosting` for WinForms applications, allowing the use of dependency injection, configuration, logging, and other features provided by the `Microsoft.Extensions` libraries. | ||
|
||
:bulb: This package is available for .NET 6.0 and later. | ||
|
||
## Contribution | ||
|
||
If you have any suggestions, bug reports, or any other form of feedback, please feel free to open an issue or a pull request. Any contributions are welcome! | ||
|
||
## Why not .NET Standard? | ||
With the .NET Standard Microsoft created a specification for APIs that are intended to be available on all .NET implementations. This was a great idea, but it also has some drawbacks. The main drawback is that the .NET Standard is a specification and not an implementation. This means that the real work is done by .NET implementations, such as .NET 5.0 and later versions. Which is why we decided us against the .NET Standard and for the concrete .NET implementations. | ||
|
||
See [The future of .NET Standard](https://devblogs.microsoft.com/dotnet/the-future-of-net-standard/) for more details. | ||
|
||
## Installation | ||
To use this package, you need to add the package to your project. You can do this by using the NuGet package manager or by using the dotnet CLI. | ||
```powershell | ||
dotnet add package NetEvolve.Extensions.Hosting.WinForms | ||
``` | ||
|
||
## Usage | ||
To use the `Microsoft.Extensions.Hosting` in a WinForms application, you just need to create a new `HostBuilder` and configure it as you would do in a console application. | ||
|
||
```csharp | ||
namespace WinForms; | ||
|
||
using Microsoft.Extensions.Hosting; | ||
using NetEvolve.Extensions.Hosting.WinForms; | ||
|
||
internal static class Program | ||
{ | ||
internal static async Task Main() => | ||
await CreateHostBuilder().Build().RunAsync().ConfigureAwait(false); | ||
|
||
public static IHostBuilder CreateHostBuilder() => | ||
Host.CreateDefaultBuilder().UseWindowsForms<Form1>(); | ||
} | ||
``` | ||
|
||
Therefore, you can use for example the `Microsoft.Extensions.DependencyInjection` to register services and inject them into your forms. | ||
|
||
```csharp | ||
namespace WinForms; | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
using System.Windows.Forms; | ||
|
||
public partial class Form1 : Form | ||
{ | ||
private readonly ILogger<Form1> _logger; | ||
|
||
public Form1(ILogger<Form1> logger) | ||
{ | ||
_logger = logger; | ||
InitializeComponent(); | ||
} | ||
|
||
private void Form1_Load(object sender, EventArgs e) | ||
{ | ||
_logger.LogInformation("Form loaded."); | ||
} | ||
} | ||
``` |
54 changes: 50 additions & 4 deletions
54
src/NetEvolve.Extensions.Hosting.WinForms/IFormularProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,76 @@ | ||
namespace NetEvolve.Extensions.Hosting.WinForms; | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using System.Windows.Forms; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
/// <summary> | ||
/// Unified access to provide windows forms, which can be used to create and manage forms. | ||
/// </summary> | ||
public interface IFormularProvider | ||
{ | ||
/// <summary> | ||
/// Gets the formular of the specified type. | ||
/// </summary> | ||
/// <typeparam name="T">The specified forms.</typeparam> | ||
/// <returns>The requested form.</returns> | ||
T GetFormular<T>() | ||
where T : Form; | ||
|
||
/// <summary> | ||
/// Gets the formular of the specified type asynchronously. | ||
/// </summary> | ||
/// <typeparam name="T">The specified forms.</typeparam> | ||
/// <returns>The requested form.</returns> | ||
ValueTask<T> GetFormularAsync<T>() | ||
where T : Form; | ||
|
||
/// <summary> | ||
/// Gets the main formular. | ||
/// </summary> | ||
/// <returns>The requested form.</returns> | ||
Form GetMainFormular(); | ||
|
||
/// <summary> | ||
/// Gets the main formular asynchronously. | ||
/// </summary> | ||
/// <returns>The requested form.</returns> | ||
ValueTask<Form> GetMainFormularAsync(); | ||
|
||
ValueTask<T> GetScopedFormAsync<T>() | ||
/// <summary> | ||
/// Gets the scoped formular of the specified type. | ||
/// </summary> | ||
/// <typeparam name="T">The specified forms.</typeparam> | ||
/// <returns>The requested form.</returns> | ||
T GetScopedForm<T>() | ||
where T : Form; | ||
|
||
ValueTask<T> GetScopedFormAsync<T>(IServiceScope scope) | ||
/// <summary> | ||
/// Gets the scoped formular of the specified type. | ||
/// </summary> | ||
/// <typeparam name="T">The specified forms.</typeparam> | ||
/// <param name="scope">The scope.</param> | ||
/// <exception cref="ArgumentNullException">Throws a <see cref="ArgumentNullException"/>, if <paramref name="scope"/> is <see langword="null"/>.</exception> | ||
/// <returns>The requested form.</returns> | ||
T GetScopedForm<T>(IServiceScope scope) | ||
where T : Form; | ||
|
||
T GetScopedForm<T>() | ||
/// <summary> | ||
/// Gets the scoped formular of the specified type asynchronously. | ||
/// </summary> | ||
/// <typeparam name="T">The specified forms.</typeparam> | ||
/// <returns>The requested form.</returns> | ||
ValueTask<T> GetScopedFormAsync<T>() | ||
where T : Form; | ||
|
||
T GetScopedForm<T>(IServiceScope scope) | ||
/// <summary> | ||
/// Gets the scoped formular of the specified type asynchronously. | ||
/// </summary> | ||
/// <typeparam name="T">The specified forms.</typeparam> | ||
/// <param name="scope">The scope.</param> | ||
/// <exception cref="ArgumentNullException">Throws a <see cref="ArgumentNullException"/>, if <paramref name="scope"/> is <see langword="null"/>.</exception> | ||
/// <returns>The requested form.</returns> | ||
ValueTask<T> GetScopedFormAsync<T>(IServiceScope scope) | ||
where T : Form; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.