Oakrey.Applications.ViewModels
2.0.3
dotnet add package Oakrey.Applications.ViewModels --version 2.0.3
NuGet\Install-Package Oakrey.Applications.ViewModels -Version 2.0.3
<PackageReference Include="Oakrey.Applications.ViewModels" Version="2.0.3" />
<PackageVersion Include="Oakrey.Applications.ViewModels" Version="2.0.3" />
<PackageReference Include="Oakrey.Applications.ViewModels" />
paket add Oakrey.Applications.ViewModels --version 2.0.3
#r "nuget: Oakrey.Applications.ViewModels, 2.0.3"
#:package Oakrey.Applications.ViewModels@2.0.3
#addin nuget:?package=Oakrey.Applications.ViewModels&version=2.0.3
#tool nuget:?package=Oakrey.Applications.ViewModels&version=2.0.3
Oakrey.Applications.ViewModels
A .NET library that removes manual DI wiring for ViewModels in MVVM applications.
Mark a ViewModel class with the appropriate lifetime interface and call a single
IServiceCollection extension method � the library discovers and registers all
eligible types automatically via reflection.
Features
- Reflection-based discovery of all
IAutoConfigurableimplementors at startup. - Four marker interfaces that control DI lifetime (see table below).
- Single
IServiceCollectionextension method:RegisterAutoConfigurables. - No code generation, no attributes, no XML configuration.
Public API
// Marker interfaces � implement one to opt a ViewModel into auto-registration
IAutoConfigurable // transient (default)
ITransientAutoConfigurable // transient (explicit)
IScopedAutoConfigurable // scoped
ISingletonAutoConfigurable // singleton
// IServiceCollection extension
services.RegisterAutoConfigurables();
// Discovery helper (optional, for diagnostics)
List<Type> types = ViewModelsConfiguration.GetAllAutoConfigurables;
Lifetime mapping
| Interface | DI lifetime |
|---|---|
IAutoConfigurable |
Transient |
ITransientAutoConfigurable |
Transient |
IScopedAutoConfigurable |
Scoped |
ISingletonAutoConfigurable |
Singleton |
Architecture
classDiagram
IAutoConfigurable <|-- ITransientAutoConfigurable
IAutoConfigurable <|-- IScopedAutoConfigurable
IAutoConfigurable <|-- ISingletonAutoConfigurable
ViewModelsConfiguration ..> IAutoConfigurable : discovers
ViewModelsConfiguration ..> IServiceCollection : registers into
Requirements
- .NET 10 or higher
Microsoft.Extensions.DependencyInjection.Abstractions(10.0.x)Oakrey.Reflection(2.0.x) � used internally for type discovery
Installation
.NET CLI
dotnet add package Oakrey.Applications.ViewModels
Package Manager Console
Install-Package Oakrey.Applications.ViewModels
NuGet Package Manager UI
Search for Oakrey.Applications.ViewModels in Visual Studio under
Tools > NuGet Package Manager > Manage NuGet Packages for Solution.
Usage
1. Mark your ViewModels
// Singleton � one instance for the application lifetime
internal sealed class MainViewModel : ObservableObject, ISingletonAutoConfigurable
{
public MainViewModel(IMyService service) { }
}
// Transient � new instance every time it is resolved
internal class PromptViewModel : IAutoConfigurable
{
public PromptViewModel(IUserPrompter prompter) { }
}
2. Register at startup
using Oakrey.Applications.ViewModels;
IServiceCollection services = new ServiceCollection();
services.RegisterAutoConfigurables(); // returns IServiceCollection � fluent chaining is supported
ServiceProvider provider = services.BuildServiceProvider();
3. Resolve normally
MainViewModel vm = provider.GetRequiredService<MainViewModel>();
4. Bind in XAML using ViewModelProvider
ViewModelProvider is a WPF MarkupExtension (shipped in Oakrey.Applications) that resolves a ViewModel from the DI container directly in XAML, without any code-behind assignment.
<Window
xmlns:app="clr-namespace:Oakrey.Applications;assembly=Oakrey.Applications"
DataContext="{app:ViewModelProvider viewModelType={x:Type local:MainViewModel}}">
For scoped ViewModels the provider creates a new IServiceScope tied to the FrameworkElement lifetime and disposes it when the element is unloaded.
Development notes
- Type discovery uses
Oakrey.Reflectionand scans all loaded assemblies at the timeRegisterAutoConfigurablesis called. Ensure all relevant assemblies are loaded before calling the extension method. - The library is framework-agnostic at its core. WPF is not required; the package
can be used in any .NET host that supports
Microsoft.Extensions.DependencyInjection.ViewModelProvideris the only WPF-specific piece and lives in a separateOakrey.Applicationsassembly. GetAllAutoConfigurablesreturns aList<Type>and can be used at startup to log or audit which ViewModels were discovered.RegisterAutoConfigurablesreturnsIServiceCollection, so it can be chained fluently with other registration calls.
License
MIT. Copyright (c) Oakrey 2016-present.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.8)
- Oakrey.Reflection (>= 2.0.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Oakrey.Applications.ViewModels:
| Package | Downloads |
|---|---|
|
Oakrey.Applications.Settings
WPF-only (.NET 10 Windows) library providing ISettingsService with persistent and volatile typed key-value settings, SettingsBase for bindable ViewModelBase-derived settings classes, CallerMemberName key inference, built-in window bounds restore via RestoreBounds, and a one-call ConfigureSettingService DI extension. |
|
|
Oakrey.Applications.About
A WPF/.NET library providing an About dialog with application name and version display, loaded assembly/module version listing, RTF EULA rendering, and a Report Issue form with log folder access. |
GitHub repositories
This package is not used by any popular GitHub repositories.