├── .github ├── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md └── workflows │ └── ci.yml ├── .gitignore ├── LICENSE ├── PULL_REQUEST_TEMPLATE.md ├── README.md ├── Smaragd.sln ├── resources ├── diagram.png ├── icon-256x256.png ├── icon-32x32.png ├── logo-x32.png ├── smaragd.afdesign └── social_preview.png ├── src └── Smaragd │ ├── Attributes │ ├── IsDirtyIgnoredAttribute.cs │ ├── IsReadOnlyIgnoredAttribute.cs │ └── PropertySourceAttribute.cs │ ├── Commands │ ├── AsyncViewModelCommand.cs │ ├── IAsyncCommand.cs │ ├── IBindableCommand.cs │ ├── IViewModelCommand.cs │ └── ViewModelCommand.cs │ ├── Helpers │ ├── ActionDisposable.cs │ ├── Disposable.cs │ ├── INotificationCache.cs │ ├── NotificationCache.cs │ └── WeakReferenceExtensions.cs │ ├── Smaragd.csproj │ ├── Smaragd.snk │ ├── UnitTestSupport.cs │ ├── Validation │ ├── FuncValidation.cs │ ├── IValidation.cs │ └── PredicateValidation.cs │ └── ViewModels │ ├── Bindable.cs │ ├── DialogModel.cs │ ├── IBindable.cs │ ├── IDialogModel.cs │ ├── ITreeViewModel.cs │ ├── IValidatingBindable.cs │ ├── IViewModel.cs │ ├── TreeViewModel.cs │ ├── ValidatingBindable.cs │ └── ViewModel.cs └── test └── Smaragd.Tests ├── Attributes ├── IsDirtyIgnoredAttributeTests.cs ├── IsReadOnlyIgnoredAttributeTests.cs └── PropertySourceAttributeTests.cs ├── Commands ├── AsyncViewModelCommandTests.cs └── ViewModelCommandTests.cs ├── GCHelper.cs ├── Helpers ├── ActionDisposableTests.cs ├── DisposableTests.cs ├── NotificationCacheTests.cs └── WeakReferenceExtensionsTests.cs ├── Smaragd.Tests.csproj ├── Smaragd.Tests.snk ├── Validation ├── FuncValidationTests.cs └── PredicateValidationTests.cs └── ViewModels ├── BindableTests.cs ├── DialogModelTests.cs ├── TreeViewModelTests.cs ├── ValidatingBindableTests.cs └── ViewModelTests.cs /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. Windows 10 Pro 1803] 28 | - .NET version: [e.g. .NET Framework 4.5] 29 | - Platform: [x86 / x64] 30 | 31 | **Additional context** 32 | Add any other context about the problem here. 33 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: [push, pull_request] 3 | jobs: 4 | build: 5 | runs-on: windows-latest 6 | steps: 7 | 8 | - name: Install .NET Core 3.0 9 | uses: actions/setup-dotnet@v1 10 | with: 11 | dotnet-version: 3.0.100 12 | 13 | - name: Checkout 14 | uses: actions/checkout@v1 15 | 16 | - name: Restore dependencies 17 | run: dotnet restore 18 | 19 | - name: Build 20 | run: dotnet build --no-restore --no-incremental --configuration Release 21 | 22 | - name: Test 23 | run: dotnet test --no-restore --no-build --configuration Release 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Build results 17 | [Dd]ebug/ 18 | [Dd]ebugPublic/ 19 | [Rr]elease/ 20 | [Rr]eleases/ 21 | x64/ 22 | x86/ 23 | [Aa][Rr][Mm]/ 24 | [Aa][Rr][Mm]64/ 25 | bld/ 26 | [Bb]in/ 27 | [Oo]bj/ 28 | [Ll]og/ 29 | 30 | # Visual Studio 2015/2017 cache/options directory 31 | .vs/ 32 | # Uncomment if you have tasks that create the project's static files in wwwroot 33 | #wwwroot/ 34 | 35 | # Visual Studio 2017 auto generated files 36 | Generated\ Files/ 37 | 38 | # MSTest test Results 39 | [Tt]est[Rr]esult*/ 40 | [Bb]uild[Ll]og.* 41 | 42 | # NUNIT 43 | *.VisualState.xml 44 | TestResult.xml 45 | 46 | # Build Results of an ATL Project 47 | [Dd]ebugPS/ 48 | [Rr]eleasePS/ 49 | dlldata.c 50 | 51 | # Benchmark Results 52 | BenchmarkDotNet.Artifacts/ 53 | 54 | # .NET Core 55 | project.lock.json 56 | project.fragment.lock.json 57 | artifacts/ 58 | 59 | # StyleCop 60 | StyleCopReport.xml 61 | 62 | # Files built by Visual Studio 63 | *_i.c 64 | *_p.c 65 | *_h.h 66 | *.ilk 67 | *.meta 68 | *.obj 69 | *.iobj 70 | *.pch 71 | *.pdb 72 | *.ipdb 73 | *.pgc 74 | *.pgd 75 | *.rsp 76 | *.sbr 77 | *.tlb 78 | *.tli 79 | *.tlh 80 | *.tmp 81 | *.tmp_proj 82 | *_wpftmp.csproj 83 | *.log 84 | *.vspscc 85 | *.vssscc 86 | .builds 87 | *.pidb 88 | *.svclog 89 | *.scc 90 | 91 | # Chutzpah Test files 92 | _Chutzpah* 93 | 94 | # Visual C++ cache files 95 | ipch/ 96 | *.aps 97 | *.ncb 98 | *.opendb 99 | *.opensdf 100 | *.sdf 101 | *.cachefile 102 | *.VC.db 103 | *.VC.VC.opendb 104 | 105 | # Visual Studio profiler 106 | *.psess 107 | *.vsp 108 | *.vspx 109 | *.sap 110 | 111 | # Visual Studio Trace Files 112 | *.e2e 113 | 114 | # TFS 2012 Local Workspace 115 | $tf/ 116 | 117 | # Guidance Automation Toolkit 118 | *.gpState 119 | 120 | # ReSharper is a .NET coding add-in 121 | _ReSharper*/ 122 | *.[Rr]e[Ss]harper 123 | *.DotSettings.user 124 | 125 | # JustCode is a .NET coding add-in 126 | .JustCode 127 | 128 | # TeamCity is a build add-in 129 | _TeamCity* 130 | 131 | # DotCover is a Code Coverage Tool 132 | *.dotCover 133 | 134 | # AxoCover is a Code Coverage Tool 135 | .axoCover/* 136 | !.axoCover/settings.json 137 | 138 | # Visual Studio code coverage results 139 | *.coverage 140 | *.coveragexml 141 | 142 | # NCrunch 143 | _NCrunch_* 144 | .*crunch*.local.xml 145 | nCrunchTemp_* 146 | 147 | # MightyMoose 148 | *.mm.* 149 | AutoTest.Net/ 150 | 151 | # Web workbench (sass) 152 | .sass-cache/ 153 | 154 | # Installshield output folder 155 | [Ee]xpress/ 156 | 157 | # DocProject is a documentation generator add-in 158 | DocProject/buildhelp/ 159 | DocProject/Help/*.HxT 160 | DocProject/Help/*.HxC 161 | DocProject/Help/*.hhc 162 | DocProject/Help/*.hhk 163 | DocProject/Help/*.hhp 164 | DocProject/Help/Html2 165 | DocProject/Help/html 166 | 167 | # Click-Once directory 168 | publish/ 169 | 170 | # Publish Web Output 171 | *.[Pp]ublish.xml 172 | *.azurePubxml 173 | # Note: Comment the next line if you want to checkin your web deploy settings, 174 | # but database connection strings (with potential passwords) will be unencrypted 175 | *.pubxml 176 | *.publishproj 177 | 178 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 179 | # checkin your Azure Web App publish settings, but sensitive information contained 180 | # in these scripts will be unencrypted 181 | PublishScripts/ 182 | 183 | # NuGet Packages 184 | *.nupkg 185 | # The packages folder can be ignored because of Package Restore 186 | **/[Pp]ackages/* 187 | # except build/, which is used as an MSBuild target. 188 | !**/[Pp]ackages/build/ 189 | # Uncomment if necessary however generally it will be regenerated when needed 190 | #!**/[Pp]ackages/repositories.config 191 | # NuGet v3's project.json files produces more ignorable files 192 | *.nuget.props 193 | *.nuget.targets 194 | 195 | # Microsoft Azure Build Output 196 | csx/ 197 | *.build.csdef 198 | 199 | # Microsoft Azure Emulator 200 | ecf/ 201 | rcf/ 202 | 203 | # Windows Store app package directories and files 204 | AppPackages/ 205 | BundleArtifacts/ 206 | Package.StoreAssociation.xml 207 | _pkginfo.txt 208 | *.appx 209 | 210 | # Visual Studio cache files 211 | # files ending in .cache can be ignored 212 | *.[Cc]ache 213 | # but keep track of directories ending in .cache 214 | !*.[Cc]ache/ 215 | 216 | # Others 217 | ClientBin/ 218 | ~$* 219 | *~ 220 | *.dbmdl 221 | *.dbproj.schemaview 222 | *.jfm 223 | *.pfx 224 | *.publishsettings 225 | orleans.codegen.cs 226 | 227 | # Including strong name files can present a security risk 228 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 229 | #*.snk 230 | 231 | # Since there are multiple workflows, uncomment next line to ignore bower_components 232 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 233 | #bower_components/ 234 | # ASP.NET Core default setup: bower directory is configured as wwwroot/lib/ and bower restore is true 235 | **/wwwroot/lib/ 236 | 237 | # RIA/Silverlight projects 238 | Generated_Code/ 239 | 240 | # Backup & report files from converting an old project file 241 | # to a newer Visual Studio version. Backup files are not needed, 242 | # because we have git ;-) 243 | _UpgradeReport_Files/ 244 | Backup*/ 245 | UpgradeLog*.XML 246 | UpgradeLog*.htm 247 | ServiceFabricBackup/ 248 | *.rptproj.bak 249 | 250 | # SQL Server files 251 | *.mdf 252 | *.ldf 253 | *.ndf 254 | 255 | # Business Intelligence projects 256 | *.rdl.data 257 | *.bim.layout 258 | *.bim_*.settings 259 | *.rptproj.rsuser 260 | 261 | # Microsoft Fakes 262 | FakesAssemblies/ 263 | 264 | # GhostDoc plugin setting file 265 | *.GhostDoc.xml 266 | 267 | # Node.js Tools for Visual Studio 268 | .ntvs_analysis.dat 269 | node_modules/ 270 | 271 | # Visual Studio 6 build log 272 | *.plg 273 | 274 | # Visual Studio 6 workspace options file 275 | *.opt 276 | 277 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 278 | *.vbw 279 | 280 | # Visual Studio LightSwitch build output 281 | **/*.HTMLClient/GeneratedArtifacts 282 | **/*.DesktopClient/GeneratedArtifacts 283 | **/*.DesktopClient/ModelManifest.xml 284 | **/*.Server/GeneratedArtifacts 285 | **/*.Server/ModelManifest.xml 286 | _Pvt_Extensions 287 | 288 | # Paket dependency manager 289 | .paket/paket.exe 290 | paket-files/ 291 | 292 | # FAKE - F# Make 293 | .fake/ 294 | 295 | # JetBrains Rider 296 | .idea/ 297 | *.sln.iml 298 | 299 | # CodeRush personal settings 300 | .cr/personal 301 | 302 | # Python Tools for Visual Studio (PTVS) 303 | __pycache__/ 304 | *.pyc 305 | 306 | # Cake - Uncomment if you are using it 307 | # tools/** 308 | # !tools/packages.config 309 | 310 | # Tabs Studio 311 | *.tss 312 | 313 | # Telerik's JustMock configuration file 314 | *.jmconfig 315 | 316 | # BizTalk build output 317 | *.btp.cs 318 | *.btm.cs 319 | *.odx.cs 320 | *.xsd.cs 321 | 322 | # OpenCover UI analysis results 323 | OpenCover/ 324 | 325 | # Azure Stream Analytics local run output 326 | ASALocalRun/ 327 | 328 | # MSBuild Binary and Structured Log 329 | *.binlog 330 | 331 | # NVidia Nsight GPU debugger configuration file 332 | *.nvuser 333 | 334 | # MFractors (Xamarin productivity tool) working folder 335 | .mfractor/ 336 | 337 | # Local History for Visual Studio 338 | .localhistory/ 339 | 340 | # Visual Studio Code Settings 341 | .vscode/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Niclas Kristek 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ## Summary 2 | 3 | 4 | ## Motivation and Context 5 | 6 | 7 | 8 | ## Details 9 | 10 | 11 | ## Types of changes 12 | 13 | - [ ] Bug fix (non-breaking change which fixes an issue) 14 | - [ ] New feature (non-breaking change which adds functionality) 15 | - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) 16 | 17 | ## Checklist: 18 | 19 | 20 | - [ ] My code follows the code style of this project. 21 | - [ ] My change requires a change to the documentation. 22 | - [ ] I have updated the documentation accordingly. 23 | - [ ] I have written/altered unit tests for the changes. 24 | - [ ] Only necessary files have been added/altered. 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ![Smaragd](./resources/logo-x32.png) 2 | 3 | [![CI Status](https://github.com/nkristek/Smaragd/workflows/CI/badge.svg)](https://github.com/nkristek/Smaragd/actions) 4 | [![NuGet version](https://img.shields.io/nuget/v/NKristek.Smaragd.svg)](https://www.nuget.org/packages/NKristek.Smaragd/) 5 | 6 | This is a very lightweight library containing base classes for implementing .NET applications using the MVVM architecture. 7 | It is fully unit tested and platform independent. 8 | 9 | ## Features 10 | 11 | Smaragd offers base implementations of key .NET interfaces for building WPF / MVVM applications. 12 | 13 | ![Core class diagram](./resources/diagram.png) 14 | 15 | In addition, it enables developers to: 16 | 17 | - Build dialog and tree structures via [`DialogModel`](https://github.com/nkristek/Smaragd/wiki/DialogModel) and [`TreeViewModel`](https://github.com/nkristek/Smaragd/wiki/TreeViewModel) 18 | - Execute commands synchronously and asynchronously via [`ViewModelCommand`](https://github.com/nkristek/Smaragd/wiki/Commands) and [`AsyncViewModelCommand`](https://github.com/nkristek/Smaragd/wiki/Commands) 19 | - Perform validation via [`FuncValidation`](https://github.com/nkristek/Smaragd/blob/master/src/Smaragd/Validation/FuncValidation.cs) and [`PredicateValidation`](https://github.com/nkristek/Smaragd/blob/master/src/Smaragd/Validation/PredicateValidation.cs) 20 | - Manage state updates for interdependent properties via [`PropertySourceAttribute`](https://github.com/nkristek/Smaragd/wiki/ViewModel#propertysourceattribute) 21 | 22 | For more information, please visit the [documentation](https://github.com/nkristek/Smaragd/wiki). 23 | 24 | ## Installation 25 | 26 | The recommended way to use this library is via [NuGet](https://www.nuget.org/packages/NKristek.Smaragd/). 27 | 28 | Currently supported frameworks: 29 | - .NET Standard 2.0 or higher 30 | - .NET Framework 4.5 or higher 31 | 32 | ## Quick Start 33 | 34 | The following is a simple demonstration of some core features of Smaragd. 35 | 36 | 1. Choose a base class for your **ViewModel**. 37 | 38 | - Inherit from [`ViewModel`](https://github.com/nkristek/Smaragd/wiki/ViewModel) if you want to use the fill feature set (recommended) 39 | - Inherit from [`Bindable`](https://github.com/nkristek/Smaragd/wiki/Bindable) if you only want an implementation of `INotifyPropertyChanged` and `INotifyPropertyChanging` 40 | 41 | ```csharp 42 | class AppViewModel : ViewModel 43 | { 44 | // ... 45 | } 46 | ``` 47 | 48 | 2. Add a property with a backing field that invokes `PropertyChanged` when set. 49 | 50 | ```csharp 51 | class AppViewModel : ViewModel 52 | { 53 | private string _name; 54 | public string Name 55 | { 56 | get => _name; 57 | set => SetProperty(ref _name, value); 58 | } 59 | } 60 | ``` 61 | 62 | 3. Make the property dependent on the `ViewModel`'s `IsDirty` property. `IsDirty` indicates whether property values have changed. The `Name` property then automatically updates observing views when `IsDirty` changes. 63 | 64 | ```csharp 65 | class AppViewModel : ViewModel 66 | { 67 | private string _name; 68 | 69 | [PropertySource(nameof(IsDirty))] 70 | public string Name 71 | { 72 | get => IsDirty ? $"{_name} (unsaved changes)" : _name; 73 | set => SetProperty(ref _name, value); 74 | } 75 | } 76 | ``` 77 | 78 | 4. Add an async command to reset the `IsDirty` flag. 79 | 80 | ```csharp 81 | class AppViewModel : ViewModel 82 | { 83 | private string _name; 84 | 85 | [PropertySource(nameof(IsDirty))] 86 | public string Name 87 | { 88 | get => IsDirty ? $"{_name} (unsaved changes)" : _name; 89 | set => SetProperty(ref _name, value); 90 | } 91 | 92 | 93 | private IViewModelCommand _saveCommand; 94 | 95 | [IsDirtyIgnored] 96 | [IsReadOnlyIgnored] 97 | public IViewModelCommand SaveCommand => _saveCommand ??= new SaveCommand(this) 98 | } 99 | 100 | 101 | class SaveCommand : AsyncViewModelCommand 102 | { 103 | public SaveCommand(AppViewModel context) 104 | { 105 | Context = context; 106 | } 107 | 108 | protected override async Task ExecuteAsync(AppViewModel viewModel, object parameter) 109 | { 110 | // SaveChanges(viewModel); 111 | viewModel.IsDirty = false; 112 | } 113 | } 114 | ``` 115 | 116 | 5. Create a view in XAML for your `ViewModel` and enjoy working with bindings. 117 | 118 | ```xml 119 | 120 |