Skip to content

Commit 41fe5a7

Browse files
committed
InfoMan: simplify code by using new C# features
1 parent 1238874 commit 41fe5a7

File tree

36 files changed

+98
-159
lines changed

36 files changed

+98
-159
lines changed

src/System.Waf/Samples/InformationManager/AddressBook.Modules.Applications.Test/ViewModels/ContactListViewModelTest.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ public class ContactListViewModelTest : AddressBookTest
1313
public void PropertiesTest()
1414
{
1515
var viewModel = Get<ContactListViewModel>();
16-
var contacts = new List<Contact>()
17-
{
18-
new(),
19-
new()
20-
};
16+
var contacts = new List<Contact>() { new(), new() };
2117

2218
Assert.IsNull(viewModel.Contacts);
2319
viewModel.Contacts = contacts;

src/System.Waf/Samples/InformationManager/AddressBook.Modules.Applications/Controllers/ContactController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public ContactController(IShellService shellService, ContactLayoutViewModel cont
2525
ContactListViewModel = contactListViewModel;
2626
ContactViewModel = contactViewModel;
2727
NewContactCommand = new DelegateCommand(NewContact);
28-
deleteContactCommand = new DelegateCommand(DeleteContact, CanDeleteContact);
28+
deleteContactCommand = new(DeleteContact, CanDeleteContact);
2929
}
3030

3131
public AddressBookRoot Root { get; set; } = null!;
@@ -40,7 +40,7 @@ public ContactController(IShellService shellService, ContactLayoutViewModel cont
4040

4141
public void Initialize()
4242
{
43-
contactsView = new ObservableListView<Contact>(Root!.Contacts, null, ContactListViewModel.Filter, null);
43+
contactsView = new(Root!.Contacts, null, ContactListViewModel.Filter, null);
4444
ContactListViewModel.Contacts = contactsView;
4545
ContactListViewModel.DeleteContactCommand = DeleteContactCommand;
4646
WeakEvent.PropertyChanged.Add(ContactListViewModel, ContactListViewModelPropertyChanged);
@@ -60,7 +60,7 @@ public void Shutdown()
6060

6161
private void NewContact()
6262
{
63-
Contact newContact = Root.AddNewContact();
63+
var newContact = Root.AddNewContact();
6464
ContactListViewModel.SelectedContact = newContact;
6565
ContactListViewModel.FocusItem();
6666
}

src/System.Waf/Samples/InformationManager/AddressBook.Modules.Applications/Controllers/ModuleController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public ModuleController(IShellService shellService, IDocumentService documentSer
3333
this.navigationService = navigationService;
3434
this.contactControllerFactory = contactControllerFactory;
3535
this.selectContactControllerFactory = selectContactControllerFactory;
36-
serializer = new Lazy<DataContractSerializer>(CreateDataContractSerializer);
36+
serializer = new(CreateDataContractSerializer);
3737
}
3838

3939
internal AddressBookRoot Root { get; private set; } = null!;
@@ -44,7 +44,7 @@ public void Initialize()
4444
{
4545
if (stream.Length == 0)
4646
{
47-
Root = new AddressBookRoot();
47+
Root = new();
4848
foreach (var x in SampleDataProvider.CreateContacts()) Root.AddContact(x);
4949
}
5050
else Root = (AddressBookRoot)serializer.Value.ReadObject(stream)!;
@@ -80,7 +80,7 @@ private void ShowAddressBook()
8080

8181
var uiNewContactCommand = new ToolBarCommand(activeContactController.NewContactCommand, "_New contact", "Creates a new contact.");
8282
var uiDeleteCommand = new ToolBarCommand(activeContactController.DeleteContactCommand, "_Delete", "Deletes the selected contact.");
83-
shellService.AddToolBarCommands(new[] { uiNewContactCommand, uiDeleteCommand });
83+
shellService.AddToolBarCommands([ uiNewContactCommand, uiDeleteCommand ]);
8484
}
8585

8686
private void CloseAddressBook()

src/System.Waf/Samples/InformationManager/AddressBook.Modules.Applications/Controllers/SelectContactController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public SelectContactController(SelectContactViewModel selectContactViewModel, Co
1919
{
2020
this.selectContactViewModel = selectContactViewModel;
2121
ContactListViewModel = contactListViewModel;
22-
selectContactCommand = new DelegateCommand(SelectContact, CanSelectContact);
22+
selectContactCommand = new(SelectContact, CanSelectContact);
2323
}
2424

2525
public object OwnerView { get; set; } = null!;
@@ -32,7 +32,7 @@ public SelectContactController(SelectContactViewModel selectContactViewModel, Co
3232

3333
public void Initialize()
3434
{
35-
contactsView = new ObservableListView<Contact>(Root.Contacts, null, ContactListViewModel.Filter, null);
35+
contactsView = new(Root.Contacts, null, ContactListViewModel.Filter, null);
3636
ContactListViewModel.Contacts = contactsView;
3737
ContactListViewModel.SelectedContact = Root.Contacts.FirstOrDefault();
3838
selectContactViewModel.ContactListView = ContactListViewModel.View;

src/System.Waf/Samples/InformationManager/AddressBook.Modules.Applications/DtoFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ namespace Waf.InformationManager.AddressBook.Modules.Applications;
55

66
internal static class DtoFactory
77
{
8-
[return: NotNullIfNotNull("contact")]
9-
public static ContactDto? ToDto(this Contact? contact) => contact != null ? new ContactDto(contact.Firstname, contact.Lastname, contact.Email) : null;
8+
[return: NotNullIfNotNull(nameof(contact))]
9+
public static ContactDto? ToDto(this Contact? contact) => contact != null ? new(contact.Firstname, contact.Lastname, contact.Email) : null;
1010
}

src/System.Waf/Samples/InformationManager/AddressBook.Modules.Applications/SampleData/SampleDataProvider.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,13 @@ namespace Waf.InformationManager.AddressBook.Modules.Applications.SampleData;
66
[GeneratedCode("ToSuppressCodeAnalysis", "1.0.0.0")]
77
public static class SampleDataProvider
88
{
9-
public static IReadOnlyList<Contact> CreateContacts()
10-
{
11-
var contacts = new List<Contact>()
12-
{
13-
CreateContact("Jesper", "Aaberg", "jesper.aaberg@example.com", "(111) 555-0100", "A. Datum Corporation", "Main St. 4567", "Buffalo", "New York", "98052", "United States"),
14-
CreateContact("Lori", "Penor", "lori.penor@fabrikam.com", "(111) 555-0104", "Baldwin Museum of Science", "Front St. 3598", "Seattle", "Washington", "12345", "United States"),
15-
CreateContact("Michael", "Pfeiffer", "michael.pfeiffer@fabrikam.com", "(222) 555-0105", "Blue Yonder Airlines", "Front St. 1234", "Seattle", "Washington", "12345", "United States"),
16-
CreateContact("Terry", "Adams", "terry.adams@adventure-works.com", "(333) 555-0102", "Adventure Works", "Main St. 789", "Buffalo", "New York", "98052", "United States"),
17-
CreateContact("Miles", "Reid", "miles.reid@adventure-works.com", "(444) 555-0123", "Adventure Works", "22nd St NE 349", "Miami", "Florida", "98052", "United States")
18-
};
19-
return contacts;
20-
}
9+
public static IReadOnlyList<Contact> CreateContacts() => [
10+
CreateContact("Jesper", "Aaberg", "jesper.aaberg@example.com", "(111) 555-0100", "A. Datum Corporation", "Main St. 4567", "Buffalo", "New York", "98052", "United States"),
11+
CreateContact("Lori", "Penor", "lori.penor@fabrikam.com", "(111) 555-0104", "Baldwin Museum of Science", "Front St. 3598", "Seattle", "Washington", "12345", "United States"),
12+
CreateContact("Michael", "Pfeiffer", "michael.pfeiffer@fabrikam.com", "(222) 555-0105", "Blue Yonder Airlines", "Front St. 1234", "Seattle", "Washington", "12345", "United States"),
13+
CreateContact("Terry", "Adams", "terry.adams@adventure-works.com", "(333) 555-0102", "Adventure Works", "Main St. 789", "Buffalo", "New York", "98052", "United States"),
14+
CreateContact("Miles", "Reid", "miles.reid@adventure-works.com", "(444) 555-0123", "Adventure Works", "22nd St NE 349", "Miami", "Florida", "98052", "United States")
15+
];
2116

2217
private static Contact CreateContact(string firstname, string lastname, string email, string phone, string company, string street, string city, string state, string postalCode, string country)
2318
{

src/System.Waf/Samples/InformationManager/AddressBook.Modules.Domain.Test/AddressBookRootTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ public void AddAndRemoveContacts()
1616
Assert.IsFalse(root.Contacts.Any());
1717

1818
var contact1 = root.AddNewContact();
19-
AssertHelper.SequenceEqual(new[] { contact1 }, root.Contacts);
19+
AssertHelper.SequenceEqual([ contact1 ], root.Contacts);
2020

2121
var contact2 = new Contact();
2222
root.AddContact(contact2);
23-
AssertHelper.SequenceEqual(new[] { contact1, contact2 }, root.Contacts);
23+
AssertHelper.SequenceEqual([ contact1, contact2 ], root.Contacts);
2424

2525
root.RemoveContact(contact1);
26-
AssertHelper.SequenceEqual(new[] { contact2 }, root.Contacts);
26+
AssertHelper.SequenceEqual([ contact2 ], root.Contacts);
2727
}
2828
}

src/System.Waf/Samples/InformationManager/AddressBook.Modules.Domain/AddressBookRoot.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@ namespace Waf.InformationManager.AddressBook.Modules.Domain;
77
public class AddressBookRoot : ValidatableModel
88
{
99
[DataMember]
10-
private readonly ObservableList<Contact> contacts;
11-
12-
public AddressBookRoot()
13-
{
14-
contacts = new();
15-
}
10+
private readonly ObservableList<Contact> contacts = [];
1611

1712
public IReadOnlyList<Contact> Contacts => contacts;
1813

src/System.Waf/Samples/InformationManager/AddressBook.Modules.Presentation/Views/ContactListView.xaml.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public partial class ContactListView : IContactListView
1616
public ContactListView()
1717
{
1818
InitializeComponent();
19-
viewModel = new Lazy<ContactListViewModel>(() => ViewHelper.GetViewModel<ContactListViewModel>(this)!);
19+
viewModel = new(() => ViewHelper.GetViewModel<ContactListViewModel>(this)!);
2020
Loaded += LoadedHandler;
2121
}
2222

@@ -30,13 +30,13 @@ public void FocusItem()
3030
return;
3131
}
3232

33-
Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)(() =>
33+
Dispatcher.BeginInvoke(DispatcherPriority.Background, () =>
3434
{
3535
// It is necessary to delay this code because data binding updates the values asynchronously.
3636
contactsBox.ScrollIntoView(ViewModel.SelectedContact);
3737
var selectedListBoxItem = (ListBoxItem)contactsBox.ItemContainerGenerator.ContainerFromItem(ViewModel.SelectedContact);
3838
selectedListBoxItem?.Focus();
39-
}));
39+
});
4040
}
4141

4242
private void LoadedHandler(object sender, RoutedEventArgs e)

src/System.Waf/Samples/InformationManager/AddressBook.Modules.Presentation/Views/SelectContactWindow.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public partial class SelectContactWindow : ISelectContactView
1616
public SelectContactWindow()
1717
{
1818
InitializeComponent();
19-
viewModel = new Lazy<SelectContactViewModel>(() => ViewHelper.GetViewModel<SelectContactViewModel>(this)!);
19+
viewModel = new(() => ViewHelper.GetViewModel<SelectContactViewModel>(this)!);
2020
}
2121

2222
public SelectContactViewModel ViewModel => viewModel.Value;

src/System.Waf/Samples/InformationManager/Assembler/App.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private static readonly (string loggerNamePattern, LogLevel minLevel)[] logSetti
3434

3535
private AggregateCatalog? catalog;
3636
private CompositionContainer? container;
37-
private IEnumerable<IModuleController> moduleControllers = Array.Empty<IModuleController>();
37+
private IEnumerable<IModuleController> moduleControllers = [];
3838

3939
public App()
4040
{
@@ -81,7 +81,7 @@ protected override void OnStartup(StartupEventArgs e)
8181
// Load module assemblies as well. See App.config file.
8282
foreach (var x in Settings.Default.ModuleAssemblies) catalog.Catalogs.Add(new AssemblyCatalog(x));
8383

84-
container = new CompositionContainer(catalog, CompositionOptions.DisableSilentRejection);
84+
container = new(catalog, CompositionOptions.DisableSilentRejection);
8585
var batch = new CompositionBatch();
8686
batch.AddExportedValue(container);
8787
container.Compose(batch);

src/System.Waf/Samples/InformationManager/Common.Applications.Test/ApplicationsTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected override void OnInitialize()
2121

2222
OnCatalogInitialize(catalog);
2323

24-
Container = new CompositionContainer(catalog, CompositionOptions.DisableSilentRejection);
24+
Container = new(catalog, CompositionOptions.DisableSilentRejection);
2525
var batch = new CompositionBatch();
2626
batch.AddExportedValue(Container);
2727
Container.Compose(batch);

src/System.Waf/Samples/InformationManager/EmailClient.Modules.Applications.Test/ViewModels/EmailListViewModelTest.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ public class EmailListViewModelTest : EmailClientTest
1313
public void PropertiesTest()
1414
{
1515
var viewModel = Get<EmailListViewModel>();
16-
var emails = new List<Email>()
17-
{
18-
new(),
19-
new(),
20-
};
16+
var emails = new List<Email>() { new(), new() };
2117

2218
Assert.IsNull(viewModel.SelectedEmail);
2319
AssertHelper.PropertyChangedEvent(viewModel, x => x.SelectedEmail, () => viewModel.SelectedEmail = emails[0]);

src/System.Waf/Samples/InformationManager/EmailClient.Modules.Applications.Test/ViewModels/NewEmailViewModelTest.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ public void PropertiesTest()
1616

1717
// Email accounts tests
1818

19-
var emailAccounts = new List<EmailAccount>()
20-
{
21-
new(),
22-
new()
23-
};
19+
var emailAccounts = new List<EmailAccount>() { new(), new() };
2420

2521
AssertHelper.PropertyChangedEvent(viewModel, x => x.SelectedEmailAccount, () => viewModel.SelectedEmailAccount = emailAccounts[0]);
2622
Assert.AreEqual(emailAccounts[0], viewModel.SelectedEmailAccount);
@@ -41,24 +37,24 @@ public void PropertiesTest()
4137
string cc = cc1 + ", " + cc2;
4238
AssertHelper.PropertyChangedEvent(viewModel, x => x.CC, () => viewModel.CC = cc);
4339
Assert.AreEqual(cc1 + "; " + cc2, viewModel.CC);
44-
AssertHelper.SequenceEqual(new[] { cc1, cc2 }, email.CC);
40+
AssertHelper.SequenceEqual([ cc1, cc2 ], email.CC);
4541

4642
string bcc1 = "user@adventure-works.com";
4743
string bcc2 = "harry@example.com";
4844
string bcc3 = "admin@adventure-works.com";
4945
string bcc = bcc1 + "; " + bcc2 + " " + bcc3;
5046
AssertHelper.PropertyChangedEvent(viewModel, x => x.Bcc, () => viewModel.Bcc = bcc);
5147
Assert.AreEqual(bcc1 + "; " + bcc2 + "; " + bcc3, viewModel.Bcc);
52-
AssertHelper.SequenceEqual(new[] { bcc1, bcc2, bcc3 }, email.Bcc);
48+
AssertHelper.SequenceEqual([ bcc1, bcc2, bcc3 ], email.Bcc);
5349

5450
string newEmail = "mike@adventure-works.com";
55-
AssertHelper.PropertyChangedEvent(viewModel, x => x.To, () => email.To = new[] { newEmail });
51+
AssertHelper.PropertyChangedEvent(viewModel, x => x.To, () => email.To = [ newEmail ]);
5652
Assert.AreEqual(newEmail, viewModel.To);
5753

58-
AssertHelper.PropertyChangedEvent(viewModel, x => x.CC, () => email.CC = new[] { newEmail });
54+
AssertHelper.PropertyChangedEvent(viewModel, x => x.CC, () => email.CC = [ newEmail ]);
5955
Assert.AreEqual(newEmail, viewModel.CC);
6056

61-
AssertHelper.PropertyChangedEvent(viewModel, x => x.Bcc, () => email.Bcc = new[] { newEmail });
57+
AssertHelper.PropertyChangedEvent(viewModel, x => x.Bcc, () => email.Bcc = [ newEmail ]);
6258
Assert.AreEqual(newEmail, viewModel.Bcc);
6359

6460
viewModel.Email = new Email();

src/System.Waf/Samples/InformationManager/EmailClient.Modules.Applications/Controllers/EditEmailAccountController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public EditEmailAccountController(EditEmailAccountViewModel editEmailAccountView
2929
this.basicEmailAccountViewModel = basicEmailAccountViewModel;
3030
this.pop3SettingsViewModelFactory = pop3SettingsViewModelFactory;
3131
this.exchangeSettingsViewModelFactory = exchangeSettingsViewModelFactory;
32-
backCommand = new DelegateCommand(Back, CanBack);
33-
nextCommand = new DelegateCommand(Next, CanNext);
32+
backCommand = new(Back, CanBack);
33+
nextCommand = new(Next, CanNext);
3434
}
3535

3636
public object OwnerWindow { get; set; } = null!;

src/System.Waf/Samples/InformationManager/EmailClient.Modules.Applications/Controllers/EmailAccountsController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public EmailAccountsController(IShellService shellService, ExportFactory<EditEma
3333

3434
private void ShowEmailAccounts()
3535
{
36-
removeEmailAccountCommand = new DelegateCommand(RemoveEmailAccount, CanRemoveEmailAccount);
37-
editEmailAccountCommand = new DelegateCommand(EditEmailAccount, CanEditEmailAccount);
36+
removeEmailAccountCommand = new(RemoveEmailAccount, CanRemoveEmailAccount);
37+
editEmailAccountCommand = new(EditEmailAccount, CanEditEmailAccount);
3838

3939
emailAccountsViewModel = emailAccountsViewModelFactory.CreateExport().Value;
4040
emailAccountsViewModel.EmailClientRoot = Root;

src/System.Waf/Samples/InformationManager/EmailClient.Modules.Applications/Controllers/EmailFolderController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public EmailFolderController(IShellService shellService, EmailLayoutViewModel em
2525
this.emailLayoutViewModel = emailLayoutViewModel;
2626
EmailListViewModel = emailListViewModel;
2727
EmailViewModel = emailViewModel;
28-
deleteEmailCommand = new DelegateCommand(DeleteEmail, CanDeleteEmail);
28+
deleteEmailCommand = new(DeleteEmail, CanDeleteEmail);
2929
}
3030

3131
public EmailFolder EmailFolder { get; set; } = null!;
@@ -38,7 +38,7 @@ public EmailFolderController(IShellService shellService, EmailLayoutViewModel em
3838

3939
public void Initialize()
4040
{
41-
emailsView = new ObservableListView<Email>(EmailFolder.Emails, null, EmailListViewModel.Filter, x => x.OrderByDescending(y => y.Sent));
41+
emailsView = new(EmailFolder.Emails, null, EmailListViewModel.Filter, x => x.OrderByDescending(y => y.Sent));
4242
EmailListViewModel.Emails = emailsView;
4343
EmailListViewModel.DeleteEmailCommand = DeleteEmailCommand;
4444
emailListViewModelPropertyChangedProxy = WeakEvent.PropertyChanged.Add(EmailListViewModel, EmailListViewModelPropertyChanged);

0 commit comments

Comments
 (0)