Skip to content

Commit 9579e47

Browse files
committed
Enabled Nullable reference type feature of C# 8.
1 parent bc5ca59 commit 9579e47

20 files changed

+100
-72
lines changed

src/ApplicationCore/ApplicationCore.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<TargetFrameworks>netstandard2.0;netcoreapp3.1</TargetFrameworks>
55
<LangVersion>latest</LangVersion>
6+
<Nullable>enable</Nullable>
67
</PropertyGroup>
78

89
<ItemGroup>

src/ApplicationCore/Entities/Store.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class Store
88
{
99
public int Id { get; set; }
1010

11-
public string Name { get; set; }
11+
public string Name { get; set; } = string.Empty;
1212

1313
public int OrdinalNumber { get; set; }
1414

src/FashionStoreWinForms/FashionStoreWinForms.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<FileAlignment>512</FileAlignment>
1313
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
1414
<Deterministic>true</Deterministic>
15+
<LangVersion>latest</LangVersion>
1516
</PropertyGroup>
1617
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1718
<PlatformTarget>AnyCPU</PlatformTarget>

src/FashionStoreWinForms/Forms/FRM_Main.cs

+24-22
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace FashionStoreWinForms.Forms
2222
{
2323
public partial class FRM_Main : Form, ILegacyWorkspaceContext, ILocalizationService
2424
{
25+
#nullable enable
2526
readonly WorkspaceViewModel workspaceViewModel;
2627
readonly IStoreManagementService storeManagementService;
2728

@@ -43,6 +44,7 @@ public void SetCurrentStore(Store store)
4344
#region ILocalizationService
4445
public string AskProceedAndAbandonFormData => Resources.ASK_PROCEED_AND_ABANDON_FORM_DATA;
4546
#endregion
47+
#nullable restore
4648

4749
bool AskForSaveIfNeeded()
4850
{
@@ -85,32 +87,32 @@ async void FRM_Main_Load(object sender, EventArgs e)
8587
}
8688
void MI_Card_PointOfSale_Click(object sender, EventArgs e)
8789
{
88-
using (FRM_Card_PointOfSale frm = new FRM_Card_PointOfSale())
89-
frm.ShowDialog(this);
90+
using FRM_Card_PointOfSale frm = new FRM_Card_PointOfSale();
91+
frm.ShowDialog(this);
9092
}
9193
void MI_DressMatrix_Click(object sender, EventArgs e)
9294
{
93-
using (FRM_DressMatrix frm = new FRM_DressMatrix())
94-
frm.ShowDialog(this);
95+
using FRM_DressMatrix frm = new FRM_DressMatrix();
96+
frm.ShowDialog(this);
9597
}
9698
void MI_Sql_Click(object sender, EventArgs e)
9799
{
98-
using (FRM_Sql frm = new FRM_Sql())
99-
frm.ShowDialog(this);
100+
using FRM_Sql frm = new FRM_Sql();
101+
frm.ShowDialog(this);
100102
}
101103
void MI_UserSettings_Click(object sender, EventArgs e)
102104
{
103-
using (FRM_UserSettings frm = new FRM_UserSettings())
104-
frm.ShowDialog(this);
105+
using FRM_UserSettings frm = new FRM_UserSettings();
106+
frm.ShowDialog(this);
105107
}
106108
void MI_AddSku_Click(object sender, EventArgs e) { AddSku(); }
107109
void MI_SearchSku_Click(object sender, EventArgs e) { SearchSku(); }
108110
void MI_SalesJournal_Click(object sender, EventArgs e)
109111
{
110112
PAN_Workplace.Controls.Clear();
111113

112-
using (FRM_SalesJournal frmSalesJournal = new FRM_SalesJournal())
113-
frmSalesJournal.ShowDialog(this);
114+
using FRM_SalesJournal frmSalesJournal = new FRM_SalesJournal();
115+
frmSalesJournal.ShowDialog(this);
114116
}
115117
void MI_Backup_Click(object sender, EventArgs e)
116118
{
@@ -159,6 +161,7 @@ void MI_Backup_Click(object sender, EventArgs e)
159161
MessageBox.Show(this, Resources.BACKUP_COMPLETED, Resources.MESSAGE, MessageBoxButtons.OK, MessageBoxIcon.Information);
160162
}
161163

164+
#nullable enable
162165
#region Temporary mock StoreService
163166
class MockStoreService: IStoreManagementService
164167
{
@@ -195,13 +198,14 @@ public FRM_Main()
195198
if (a.PropertyName == nameof(StoreSelectorViewModel.SelectedStore))
196199
{
197200
var storeVM = (StoreSelectorViewModel)s;
198-
var store = await storeManagementService.GetStore(storeVM.SelectedStore.StoreId);
201+
var store = await storeManagementService.GetStore(storeVM.SelectedStore!.StoreId);
199202
SetCurrentStore(store);
200203
}
201204
};
202205

203206
warehouseSelector1.DataContext = workspaceViewModel.StoreSelector;
204207
}
208+
#nullable restore
205209

206210
public void AddSku()
207211
{
@@ -250,17 +254,15 @@ void MakeReport(PointOfSale in_pos = null)
250254

251255
if (in_pos != null)
252256
{
253-
using (FRM_ReportParams frmParams = new FRM_ReportParams())
254-
{
255-
frmParams.ShowDialog(this);
256-
if (!frmParams.MakeReport)
257-
return;
258-
articlePrefix = frmParams.ArticlePrefix != string.Empty ? frmParams.ArticlePrefix : null;
259-
showPriceOfPurchase = Settings.Default.LastRepShowPriceOfPurchase;
260-
showPriceOfSale = Settings.Default.LastRepShowPriceOfSale;
261-
showPriceOfStock = Settings.Default.LastRepShowPriceOfStock;
262-
showSizes = Settings.Default.LastRepShowSizes;
263-
}
257+
using FRM_ReportParams frmParams = new FRM_ReportParams();
258+
frmParams.ShowDialog(this);
259+
if (!frmParams.MakeReport)
260+
return;
261+
articlePrefix = frmParams.ArticlePrefix != string.Empty ? frmParams.ArticlePrefix : null;
262+
showPriceOfPurchase = Settings.Default.LastRepShowPriceOfPurchase;
263+
showPriceOfSale = Settings.Default.LastRepShowPriceOfSale;
264+
showPriceOfStock = Settings.Default.LastRepShowPriceOfStock;
265+
showSizes = Settings.Default.LastRepShowSizes;
264266
}
265267

266268
List<string> textLines = new List<string>();

src/MvvmInfrastructure/MvvmInfrastructure.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<TargetFrameworks>netstandard2.0;netcoreapp3.1</TargetFrameworks>
55
<LangVersion>latest</LangVersion>
6+
<Nullable>enable</Nullable>
67
</PropertyGroup>
78

89
<ItemGroup>

src/MvvmInfrastructure/PropertyChangeNotifier.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace MvvmInfrastructure
55
{
66
public class PropertyChangeNotifier : INotifyPropertyChanged
77
{
8-
readonly PropertyChangeNotifier parent;
8+
readonly PropertyChangeNotifier? parent;
99

1010
bool busy;
1111

@@ -26,8 +26,8 @@ public virtual bool Busy
2626
}
2727
}
2828

29-
public event PropertyChangedEventHandler PropertyChanged;
29+
public event PropertyChangedEventHandler? PropertyChanged;
3030

31-
protected void OnPropertyChanged([CallerMemberName] string propertyName = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
31+
protected void OnPropertyChanged([CallerMemberName] string? propertyName = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
3232
}
3333
}

src/Utilities/DictionaryUtilities.cs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public static class DictionaryUtilities
1414
/// <param name="dictionary">Dictionary</param>
1515
/// <param name="key">Key</param>
1616
public static void Inc<T>(this Dictionary<T, int> dictionary, T key)
17+
where T : notnull
1718
{
1819
Guard.Against.Null(dictionary, nameof(dictionary));
1920

src/Utilities/EnumUtilities.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
using System;
1+
using Ardalis.GuardClauses;
2+
using System;
23
using System.Linq;
34

45
namespace Utilities
56
{
67
public static class EnumUtilities
78
{
89
public static TMapTo MapTo<T, TMapTo>(this T thisValue)
10+
where T : notnull
11+
where TMapTo : notnull
912
{
10-
var optionsName = Enum.GetName(typeof(T), thisValue).ToUpperInvariant();
11-
var button = ((TMapTo[])Enum.GetValues(typeof(TMapTo)))
12-
.Where(v => Enum.GetName(typeof(TMapTo), v).ToUpperInvariant() == optionsName)
13+
Guard.Against.Null(thisValue, nameof(thisValue));
14+
15+
var optionsName = Enum.GetName(typeof(T), thisValue)!.ToUpperInvariant();
16+
var button = ((TMapTo[])Enum
17+
.GetValues(typeof(TMapTo)))
18+
.Where(val => Enum.GetName(typeof(TMapTo), val!)!.ToUpperInvariant() == optionsName)
1319
.Single();
1420
return button;
1521
}

src/Utilities/Utilities.csproj

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<TargetFrameworks>netstandard2.0;netcoreapp3.1</TargetFrameworks>
55
<LangVersion>latest</LangVersion>
6+
<Nullable>enable</Nullable>
67
</PropertyGroup>
78

89
<ItemGroup>

src/ViewModels/StoreButtonViewModel.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ public StoreButtonViewModel(Store store)
4545
{
4646
Guard.Against.Null(store, nameof(store));
4747

48-
StoreName = store.Name;
48+
storeName = store.Name;
49+
OnPropertyChanged(nameof(StoreName));
50+
4951
StoreId = store.Id;
5052
}
5153
}

src/ViewModels/StoreSelectorViewModel.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ public class StoreSelectorViewModel : PropertyChangeNotifier
1313
readonly IStoreManagementService storeManagementService;
1414

1515
#region Private underlying fields
16-
ObservableCollection<StoreButtonViewModel> selectorButtons;
17-
StoreButtonViewModel selectedStore;
16+
StoreButtonViewModel? selectedStore;
1817
#endregion
1918

2019
#region Public properties and actions
21-
public ObservableCollection<StoreButtonViewModel> SelectorButtons => selectorButtons;
20+
public ObservableCollection<StoreButtonViewModel>? SelectorButtons { get; private set; }
2221

23-
public StoreButtonViewModel SelectedStore
22+
public StoreButtonViewModel? SelectedStore
2423
{
2524
get => selectedStore;
2625

@@ -51,7 +50,7 @@ public async Task Initialize()
5150
var storeButtons = stores
5251
.Select(store => new StoreButtonViewModel(store));
5352

54-
selectorButtons = new ObservableCollection<StoreButtonViewModel>(storeButtons);
53+
SelectorButtons = new ObservableCollection<StoreButtonViewModel>(storeButtons);
5554
OnPropertyChanged(nameof(SelectorButtons));
5655

5756
SelectedStore = SelectorButtons.FirstOrDefault();

src/ViewModels/ViewModels.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<TargetFrameworks>netstandard2.0;netcoreapp3.1</TargetFrameworks>
55
<LangVersion>latest</LangVersion>
6+
<Nullable>enable</Nullable>
67
</PropertyGroup>
78

89
<ItemGroup>

src/ViewModels/WorkspaceViewModel.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ ILegacyWorkspaceContext legacyWorkspaceContext
7070
this.storeManagementService = storeManagementService;
7171
this.legacyWorkspaceContext = legacyWorkspaceContext;
7272

73-
StoreSelector = new StoreSelectorViewModel(this, this.storeManagementService);
73+
storeSelector = new StoreSelectorViewModel(this, this.storeManagementService);
74+
OnPropertyChanged(nameof(StoreSelector));
7475
}
7576

7677
public async Task Initialize()

src/WinFormsInfrastructure/DialogService.cs

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using MvvmInfrastructure;
1+
#nullable enable
2+
3+
using Ardalis.GuardClauses;
4+
using MvvmInfrastructure;
25
using System;
36
using System.Collections.Generic;
47
using System.Linq;
@@ -13,9 +16,10 @@ public class DialogService : IDialogService
1316
{
1417
public DialogResultEnum PresentDialog(string question, DialogOptionsEnum options)
1518
{
19+
Guard.Against.NullOrEmpty(question, nameof(question));
20+
1621
var button = options.MapTo<DialogOptionsEnum, MessageBoxButtons>();
17-
var nonNullQuestion = question ?? string.Empty;
18-
var dlgResult = MessageBox.Show(nonNullQuestion, nonNullQuestion.Length > 30 ? (question.Substring(0, 28) + "...") : nonNullQuestion, button);
22+
var dlgResult = MessageBox.Show(question, question.Length > 30 ? (question.Substring(0, 28) + "...") : question, button);
1923
return dlgResult.MapTo<DialogResult, DialogResultEnum>();
2024
}
2125

@@ -36,3 +40,5 @@ public string SelectDirectoryPath()
3640
}
3741
}
3842
}
43+
44+
#nullable restore

src/WinFormsInfrastructure/WinFormsInfrastructure.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
1414
<Deterministic>true</Deterministic>
15+
<LangVersion>latest</LangVersion>
1516
</PropertyGroup>
1617
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1718
<DebugSymbols>true</DebugSymbols>
@@ -56,6 +57,9 @@
5657
</ProjectReference>
5758
</ItemGroup>
5859
<ItemGroup>
60+
<PackageReference Include="Ardalis.GuardClauses">
61+
<Version>1.4.2</Version>
62+
</PackageReference>
5963
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers">
6064
<Version>2.9.8</Version>
6165
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

tests/tests/ViewModelsTests/StoreButtonViewModelUT.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ public class StoreButtonViewModelUT
1313
public void WillThrowExceptionIfConstructorPassNullStore()
1414
{
1515
// Arrange
16-
Store nullStore = null;
16+
Store? nullStore = null;
1717

1818
// Act
19-
var ex = Assert.Throws<ArgumentNullException>(() => new StoreButtonViewModel(nullStore));
19+
var ex = Assert.Throws<ArgumentNullException>(() => new StoreButtonViewModel(nullStore!));
2020

2121
// Assert
2222
Assert.NotNull(ex);

tests/tests/ViewModelsTests/StoreSelectorViewModel/SelectedStoreAndNotificationsUT.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ namespace ViewModelsTests.StoreSelectorViewModel
1515
public class SelectedStoreAndNotificationsUT
1616
{
1717
(Mock<IStoreManagementService>, vm.WorkspaceViewModel) CreateBaseMocks(
18-
Mock<vm.ILegacyWorkspaceContext> mockOverrideLegacyWorkspaceContext = null,
19-
Mock<IDialogService> mockOverrideDialogService = null
18+
Mock<vm.ILegacyWorkspaceContext>? mockOverrideLegacyWorkspaceContext = null,
19+
Mock<IDialogService>? mockOverrideDialogService = null
2020
)
2121
{
2222
var mockDialogService = mockOverrideDialogService ?? new Mock<IDialogService>();
@@ -72,7 +72,7 @@ public async void DifferentSelectionConditions(bool selectDifferentStore, bool n
7272

7373
const int nextStoreIndex = 1;
7474
var oldStore = workspace.StoreSelector.SelectedStore;
75-
var nextStore = workspace.StoreSelector.SelectorButtons[nextStoreIndex];
75+
var nextStore = workspace.StoreSelector.SelectorButtons![nextStoreIndex];
7676
Assert.NotEqual(oldStore, nextStore);
7777

7878
// Act

0 commit comments

Comments
 (0)