Skip to content

Commit

Permalink
InfoMan: simplify code by using new C# features
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Nov 25, 2023
1 parent c7907e2 commit 932c55d
Show file tree
Hide file tree
Showing 36 changed files with 98 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ public class ContactListViewModelTest : AddressBookTest
public void PropertiesTest()
{
var viewModel = Get<ContactListViewModel>();
var contacts = new List<Contact>()
{
new(),
new()
};
var contacts = new List<Contact>() { new(), new() };

Assert.IsNull(viewModel.Contacts);
viewModel.Contacts = contacts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ContactController(IShellService shellService, ContactLayoutViewModel cont
ContactListViewModel = contactListViewModel;
ContactViewModel = contactViewModel;
NewContactCommand = new DelegateCommand(NewContact);
deleteContactCommand = new DelegateCommand(DeleteContact, CanDeleteContact);
deleteContactCommand = new(DeleteContact, CanDeleteContact);
}

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

public void Initialize()
{
contactsView = new ObservableListView<Contact>(Root!.Contacts, null, ContactListViewModel.Filter, null);
contactsView = new(Root!.Contacts, null, ContactListViewModel.Filter, null);
ContactListViewModel.Contacts = contactsView;
ContactListViewModel.DeleteContactCommand = DeleteContactCommand;
WeakEvent.PropertyChanged.Add(ContactListViewModel, ContactListViewModelPropertyChanged);
Expand All @@ -60,7 +60,7 @@ public void Shutdown()

private void NewContact()
{
Contact newContact = Root.AddNewContact();
var newContact = Root.AddNewContact();
ContactListViewModel.SelectedContact = newContact;
ContactListViewModel.FocusItem();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public ModuleController(IShellService shellService, IDocumentService documentSer
this.navigationService = navigationService;
this.contactControllerFactory = contactControllerFactory;
this.selectContactControllerFactory = selectContactControllerFactory;
serializer = new Lazy<DataContractSerializer>(CreateDataContractSerializer);
serializer = new(CreateDataContractSerializer);
}

internal AddressBookRoot Root { get; private set; } = null!;
Expand All @@ -44,7 +44,7 @@ public void Initialize()
{
if (stream.Length == 0)
{
Root = new AddressBookRoot();
Root = new();
foreach (var x in SampleDataProvider.CreateContacts()) Root.AddContact(x);
}
else Root = (AddressBookRoot)serializer.Value.ReadObject(stream)!;
Expand Down Expand Up @@ -80,7 +80,7 @@ private void ShowAddressBook()

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

private void CloseAddressBook()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public SelectContactController(SelectContactViewModel selectContactViewModel, Co
{
this.selectContactViewModel = selectContactViewModel;
ContactListViewModel = contactListViewModel;
selectContactCommand = new DelegateCommand(SelectContact, CanSelectContact);
selectContactCommand = new(SelectContact, CanSelectContact);
}

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

public void Initialize()
{
contactsView = new ObservableListView<Contact>(Root.Contacts, null, ContactListViewModel.Filter, null);
contactsView = new(Root.Contacts, null, ContactListViewModel.Filter, null);
ContactListViewModel.Contacts = contactsView;
ContactListViewModel.SelectedContact = Root.Contacts.FirstOrDefault();
selectContactViewModel.ContactListView = ContactListViewModel.View;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace Waf.InformationManager.AddressBook.Modules.Applications;

internal static class DtoFactory
{
[return: NotNullIfNotNull("contact")]
public static ContactDto? ToDto(this Contact? contact) => contact != null ? new ContactDto(contact.Firstname, contact.Lastname, contact.Email) : null;
[return: NotNullIfNotNull(nameof(contact))]
public static ContactDto? ToDto(this Contact? contact) => contact != null ? new(contact.Firstname, contact.Lastname, contact.Email) : null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,13 @@ namespace Waf.InformationManager.AddressBook.Modules.Applications.SampleData;
[GeneratedCode("ToSuppressCodeAnalysis", "1.0.0.0")]
public static class SampleDataProvider
{
public static IReadOnlyList<Contact> CreateContacts()
{
var contacts = new List<Contact>()
{
CreateContact("Jesper", "Aaberg", "jesper.aaberg@example.com", "(111) 555-0100", "A. Datum Corporation", "Main St. 4567", "Buffalo", "New York", "98052", "United States"),
CreateContact("Lori", "Penor", "lori.penor@fabrikam.com", "(111) 555-0104", "Baldwin Museum of Science", "Front St. 3598", "Seattle", "Washington", "12345", "United States"),
CreateContact("Michael", "Pfeiffer", "michael.pfeiffer@fabrikam.com", "(222) 555-0105", "Blue Yonder Airlines", "Front St. 1234", "Seattle", "Washington", "12345", "United States"),
CreateContact("Terry", "Adams", "terry.adams@adventure-works.com", "(333) 555-0102", "Adventure Works", "Main St. 789", "Buffalo", "New York", "98052", "United States"),
CreateContact("Miles", "Reid", "miles.reid@adventure-works.com", "(444) 555-0123", "Adventure Works", "22nd St NE 349", "Miami", "Florida", "98052", "United States")
};
return contacts;
}
public static IReadOnlyList<Contact> CreateContacts() => [
CreateContact("Jesper", "Aaberg", "jesper.aaberg@example.com", "(111) 555-0100", "A. Datum Corporation", "Main St. 4567", "Buffalo", "New York", "98052", "United States"),
CreateContact("Lori", "Penor", "lori.penor@fabrikam.com", "(111) 555-0104", "Baldwin Museum of Science", "Front St. 3598", "Seattle", "Washington", "12345", "United States"),
CreateContact("Michael", "Pfeiffer", "michael.pfeiffer@fabrikam.com", "(222) 555-0105", "Blue Yonder Airlines", "Front St. 1234", "Seattle", "Washington", "12345", "United States"),
CreateContact("Terry", "Adams", "terry.adams@adventure-works.com", "(333) 555-0102", "Adventure Works", "Main St. 789", "Buffalo", "New York", "98052", "United States"),
CreateContact("Miles", "Reid", "miles.reid@adventure-works.com", "(444) 555-0123", "Adventure Works", "22nd St NE 349", "Miami", "Florida", "98052", "United States")
];

private static Contact CreateContact(string firstname, string lastname, string email, string phone, string company, string street, string city, string state, string postalCode, string country)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ public void AddAndRemoveContacts()
Assert.IsFalse(root.Contacts.Any());

var contact1 = root.AddNewContact();
AssertHelper.SequenceEqual(new[] { contact1 }, root.Contacts);
AssertHelper.SequenceEqual([ contact1 ], root.Contacts);

var contact2 = new Contact();
root.AddContact(contact2);
AssertHelper.SequenceEqual(new[] { contact1, contact2 }, root.Contacts);
AssertHelper.SequenceEqual([ contact1, contact2 ], root.Contacts);

root.RemoveContact(contact1);
AssertHelper.SequenceEqual(new[] { contact2 }, root.Contacts);
AssertHelper.SequenceEqual([ contact2 ], root.Contacts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ namespace Waf.InformationManager.AddressBook.Modules.Domain;
public class AddressBookRoot : ValidatableModel
{
[DataMember]
private readonly ObservableList<Contact> contacts;

public AddressBookRoot()
{
contacts = new();
}
private readonly ObservableList<Contact> contacts = [];

public IReadOnlyList<Contact> Contacts => contacts;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public partial class ContactListView : IContactListView
public ContactListView()
{
InitializeComponent();
viewModel = new Lazy<ContactListViewModel>(() => ViewHelper.GetViewModel<ContactListViewModel>(this)!);
viewModel = new(() => ViewHelper.GetViewModel<ContactListViewModel>(this)!);
Loaded += LoadedHandler;
}

Expand All @@ -30,13 +30,13 @@ public void FocusItem()
return;
}

Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)(() =>
Dispatcher.BeginInvoke(DispatcherPriority.Background, () =>
{
// It is necessary to delay this code because data binding updates the values asynchronously.
contactsBox.ScrollIntoView(ViewModel.SelectedContact);
var selectedListBoxItem = (ListBoxItem)contactsBox.ItemContainerGenerator.ContainerFromItem(ViewModel.SelectedContact);
selectedListBoxItem?.Focus();
}));
});
}

private void LoadedHandler(object sender, RoutedEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public partial class SelectContactWindow : ISelectContactView
public SelectContactWindow()
{
InitializeComponent();
viewModel = new Lazy<SelectContactViewModel>(() => ViewHelper.GetViewModel<SelectContactViewModel>(this)!);
viewModel = new(() => ViewHelper.GetViewModel<SelectContactViewModel>(this)!);
}

public SelectContactViewModel ViewModel => viewModel.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private static readonly (string loggerNamePattern, LogLevel minLevel)[] logSetti

private AggregateCatalog? catalog;
private CompositionContainer? container;
private IEnumerable<IModuleController> moduleControllers = Array.Empty<IModuleController>();
private IEnumerable<IModuleController> moduleControllers = [];

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

container = new CompositionContainer(catalog, CompositionOptions.DisableSilentRejection);
container = new(catalog, CompositionOptions.DisableSilentRejection);
var batch = new CompositionBatch();
batch.AddExportedValue(container);
container.Compose(batch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected override void OnInitialize()

OnCatalogInitialize(catalog);

Container = new CompositionContainer(catalog, CompositionOptions.DisableSilentRejection);
Container = new(catalog, CompositionOptions.DisableSilentRejection);
var batch = new CompositionBatch();
batch.AddExportedValue(Container);
Container.Compose(batch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ public class EmailListViewModelTest : EmailClientTest
public void PropertiesTest()
{
var viewModel = Get<EmailListViewModel>();
var emails = new List<Email>()
{
new(),
new(),
};
var emails = new List<Email>() { new(), new() };

Assert.IsNull(viewModel.SelectedEmail);
AssertHelper.PropertyChangedEvent(viewModel, x => x.SelectedEmail, () => viewModel.SelectedEmail = emails[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ public void PropertiesTest()

// Email accounts tests

var emailAccounts = new List<EmailAccount>()
{
new(),
new()
};
var emailAccounts = new List<EmailAccount>() { new(), new() };

AssertHelper.PropertyChangedEvent(viewModel, x => x.SelectedEmailAccount, () => viewModel.SelectedEmailAccount = emailAccounts[0]);
Assert.AreEqual(emailAccounts[0], viewModel.SelectedEmailAccount);
Expand All @@ -41,24 +37,24 @@ public void PropertiesTest()
string cc = cc1 + ", " + cc2;
AssertHelper.PropertyChangedEvent(viewModel, x => x.CC, () => viewModel.CC = cc);
Assert.AreEqual(cc1 + "; " + cc2, viewModel.CC);
AssertHelper.SequenceEqual(new[] { cc1, cc2 }, email.CC);
AssertHelper.SequenceEqual([ cc1, cc2 ], email.CC);

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

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

AssertHelper.PropertyChangedEvent(viewModel, x => x.CC, () => email.CC = new[] { newEmail });
AssertHelper.PropertyChangedEvent(viewModel, x => x.CC, () => email.CC = [ newEmail ]);
Assert.AreEqual(newEmail, viewModel.CC);

AssertHelper.PropertyChangedEvent(viewModel, x => x.Bcc, () => email.Bcc = new[] { newEmail });
AssertHelper.PropertyChangedEvent(viewModel, x => x.Bcc, () => email.Bcc = [ newEmail ]);
Assert.AreEqual(newEmail, viewModel.Bcc);

viewModel.Email = new Email();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public EditEmailAccountController(EditEmailAccountViewModel editEmailAccountView
this.basicEmailAccountViewModel = basicEmailAccountViewModel;
this.pop3SettingsViewModelFactory = pop3SettingsViewModelFactory;
this.exchangeSettingsViewModelFactory = exchangeSettingsViewModelFactory;
backCommand = new DelegateCommand(Back, CanBack);
nextCommand = new DelegateCommand(Next, CanNext);
backCommand = new(Back, CanBack);
nextCommand = new(Next, CanNext);
}

public object OwnerWindow { get; set; } = null!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public EmailAccountsController(IShellService shellService, ExportFactory<EditEma

private void ShowEmailAccounts()
{
removeEmailAccountCommand = new DelegateCommand(RemoveEmailAccount, CanRemoveEmailAccount);
editEmailAccountCommand = new DelegateCommand(EditEmailAccount, CanEditEmailAccount);
removeEmailAccountCommand = new(RemoveEmailAccount, CanRemoveEmailAccount);
editEmailAccountCommand = new(EditEmailAccount, CanEditEmailAccount);

emailAccountsViewModel = emailAccountsViewModelFactory.CreateExport().Value;
emailAccountsViewModel.EmailClientRoot = Root;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public EmailFolderController(IShellService shellService, EmailLayoutViewModel em
this.emailLayoutViewModel = emailLayoutViewModel;
EmailListViewModel = emailListViewModel;
EmailViewModel = emailViewModel;
deleteEmailCommand = new DelegateCommand(DeleteEmail, CanDeleteEmail);
deleteEmailCommand = new(DeleteEmail, CanDeleteEmail);
}

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

public void Initialize()
{
emailsView = new ObservableListView<Email>(EmailFolder.Emails, null, EmailListViewModel.Filter, x => x.OrderByDescending(y => y.Sent));
emailsView = new(EmailFolder.Emails, null, EmailListViewModel.Filter, x => x.OrderByDescending(y => y.Sent));
EmailListViewModel.Emails = emailsView;
EmailListViewModel.DeleteEmailCommand = DeleteEmailCommand;
emailListViewModelPropertyChangedProxy = WeakEvent.PropertyChanged.Add(EmailListViewModel, EmailListViewModelPropertyChanged);
Expand Down
Loading

0 comments on commit 932c55d

Please sign in to comment.