DoenaSoft.AbstractionLayer.WinForms
2.0.4
dotnet add package DoenaSoft.AbstractionLayer.WinForms --version 2.0.4
NuGet\Install-Package DoenaSoft.AbstractionLayer.WinForms -Version 2.0.4
<PackageReference Include="DoenaSoft.AbstractionLayer.WinForms" Version="2.0.4" />
<PackageVersion Include="DoenaSoft.AbstractionLayer.WinForms" Version="2.0.4" />
<PackageReference Include="DoenaSoft.AbstractionLayer.WinForms" />
paket add DoenaSoft.AbstractionLayer.WinForms --version 2.0.4
#r "nuget: DoenaSoft.AbstractionLayer.WinForms, 2.0.4"
#:package DoenaSoft.AbstractionLayer.WinForms@2.0.4
#addin nuget:?package=DoenaSoft.AbstractionLayer.WinForms&version=2.0.4
#tool nuget:?package=DoenaSoft.AbstractionLayer.WinForms&version=2.0.4
DoenaSoft.AbstractionLayer.WinForms
WinForms abstractions that make Windows Forms components easier to mock and unit-test. This project targets multiple frameworks to support legacy .NET Framework and modern .NET desktop applications.
Package Id: DoenaSoft.AbstractionLayer.WinForms
Targets: net472, net8.0-windows, net10.0-windows
Usage:
Install the package from NuGet and program against the provided interfaces instead of concrete WinForms types to make code testable.
License: MIT
Interfaces
This package provides WinForms-specific implementations of the interfaces defined in DoenaSoft.AbstractionLayer.UI. The key interfaces include:
UI Services
IUIServices- The primary entry point for UI operations. Provides methods to show message boxes and common file/folder dialogs:ShowMessageBox- Display message boxes with configurable text, caption, buttons, and iconShowOpenFileDialog- Show open file dialogs for single or multiple file selectionShowSaveFileDialog- Show save file dialogsShowFolderBrowserDialog- Show folder browser dialogs
Clipboard Services
IClipboardServices- Comprehensive clipboard operations:- Contains methods:
ContainsText,ContainsAudio,ContainsFileDropList,ContainsImage,ContainsData - Get methods:
GetText,GetAudioStream,GetFileDropList,GetImage,GetData,GetDataObject - Set methods:
SetText,SetAudio(byte array or stream),SetFileDropList,SetImage,SetData,SetDataObject - Clear method:
Clear- Remove all data from clipboard
- Contains methods:
Thread Synchronization
ISynchronizer- Invoke work on the UI thread:Invoke(Action)- Synchronously invoke an action on the UI threadInvoke<T>(Func<T>)- Synchronously invoke a function and return its resultBeginInvoke(Action)- Asynchronously invoke an action on the UI thread
IDispatcherOperation- Represents a posted dispatcher operation:Result- Gets the result after completionStatus- Gets the current status (Pending, Aborted, Completed, Executing)Task- Gets a Task that represents the operation
Supporting Data Types
Dialog Configuration
FileDialogOptions- Base configuration for file dialogsOpenFileDialogOptions- Configuration for open file dialogs (includes multi-select)SaveFileDialogOptions- Configuration for save file dialogsFolderBrowserDialogOptions- Configuration for folder browser dialogs
Enumerations
MessageButton- Message box button configuration (OK, YesNo, YesNoCancel)MessageIcon- Message box icon types (None, Information, Warning, Error, Question)Result- Dialog result values (OK, Cancel, Yes, No, etc.)DispatcherStatus- Dispatcher operation status (Pending, Aborted, Completed, Executing)
Adapters
This package provides concrete WinForms adapter implementations:
FormUIServices- WrapsSystem.Windows.Formsdialog types and implementsIUIServicesFormClipboardServices- WrapsSystem.Windows.Forms.Clipboardand implementsIClipboardServicesFormSynchronizer- WrapsControl.Invoke/BeginInvokeand implementsISynchronizer
Examples
Usage from application code (example view model):
public class MyViewModel
{
private readonly DoenaSoft.AbstractionLayer.UIServices.IUIServices _ui;
private readonly DoenaSoft.AbstractionLayer.UIServices.IClipboardServices _clipboard;
public MyViewModel(
DoenaSoft.AbstractionLayer.UIServices.IUIServices ui,
DoenaSoft.AbstractionLayer.UIServices.IClipboardServices clipboard)
{
_ui = ui;
_clipboard = clipboard;
}
public void SaveCommand()
{
if (_ui.ShowMessageBox("Save changes?", "Confirm", MessageButton.YesNo, MessageIcon.Question) == Result.Yes)
{
// perform save
}
}
public void CopyToClipboard(string text)
{
if (_clipboard.SetText(text))
{
_ui.ShowMessageBox("Copied to clipboard", "Success", MessageButton.OK, MessageIcon.Information);
}
}
}
Simple fake for unit tests:
class FakeUIServices : DoenaSoft.AbstractionLayer.UIServices.IUIServices
{
public Result LastMessageBoxResult { get; set; } = Result.Yes;
public Result ShowMessageBox(string text, string caption, Buttons buttons, Icon icon)
{
return LastMessageBoxResult;
}
public bool ShowOpenFileDialog(OpenFileDialogOptions options, out string fileName)
{
fileName = "test.txt";
return true;
}
public bool ShowOpenFileDialog(OpenFileDialogOptions options, out string[] fileNames)
{
fileNames = new[] { "test.txt" };
return true;
}
public bool ShowSaveFileDialog(SaveFileDialogOptions options, out string fileName)
{
fileName = "out.txt";
return true;
}
public bool ShowFolderBrowserDialog(FolderBrowserDialogOptions options, out string folder)
{
folder = "C:\\Temp";
return true;
}
}
class FakeClipboardServices : DoenaSoft.AbstractionLayer.UIServices.IClipboardServices
{
public bool ContainsText => true;
public string ClipboardText { get; set; } = string.Empty;
public bool ContainsAudio() => false;
public bool ContainsFileDropList() => false;
public bool ContainsImage() => false;
public bool ContainsData(string format) => false;
public void Clear() => ClipboardText = string.Empty;
public string GetText() => ClipboardText;
public Stream GetAudioStream() => null;
public StringCollection GetFileDropList() => null;
public object GetImage() => null;
public object GetData(string format) => null;
public object GetDataObject() => null;
public bool SetText(string text)
{
ClipboardText = text;
return true;
}
public void SetAudio(byte[] audioBytes) { }
public void SetAudio(Stream audioStream) { }
public void SetFileDropList(StringCollection filePaths) { }
public void SetImage(object image) { }
public void SetData(string format, object data) { }
public void SetDataObject(object data, bool copy, int retryTimes, int retryDelay) { }
}
Notes
- This package depends on
DoenaSoft.AbstractionLayer.UIversion 2.0.1 or later, which contains the contract interfaces and data types. - The concrete adapter implementations in this package wrap
System.Windows.Formstypes to implement the UI contracts. - In production code, use the adapters provided by this package. In unit tests, reference only the contracts package and provide test doubles for the interfaces.
DoenaSoft.AbstractionLayer.WinFormsprovides concrete adapters and implementations for those interfaces (for exampleFormUIServices,FormClipboardServices) that wrapSystem.Windows.Formscomponents. In production you reference and use this package; in unit tests you should depend on the contracts (DoenaSoft.AbstractionLayer.UI) and supply test doubles for the interfaces.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0-windows7.0 is compatible. net9.0-windows was computed. net10.0-windows was computed. net10.0-windows7.0 is compatible. |
| .NET Framework | net472 is compatible. net48 was computed. net481 was computed. |
-
.NETFramework 4.7.2
- DoenaSoft.AbstractionLayer.UI (>= 2.0.6)
-
net10.0-windows7.0
- DoenaSoft.AbstractionLayer.UI (>= 2.0.6)
-
net8.0-windows7.0
- DoenaSoft.AbstractionLayer.UI (>= 2.0.6)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on DoenaSoft.AbstractionLayer.WinForms:
| Package | Downloads |
|---|---|
|
DoenaSoft.DVDProfiler.Xml
Strongly-typed .NET library for reading, writing, and manipulating DVD Profiler XML export files from Invelos. |
GitHub repositories
This package is not used by any popular GitHub repositories.