Skip to content

Commit e296a0a

Browse files
authored
Merge pull request #3140 from RLittlesII/chore/nullability/mvvm
chore: enable nullability for core mvvm
2 parents fd8b659 + 1ce4c73 commit e296a0a

9 files changed

+86
-108
lines changed

src/Prism.Core/Mvvm/BindableBase.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
using System;
2-
using System.Collections.Generic;
31
using System.ComponentModel;
42
using System.Runtime.CompilerServices;
53

4+
#nullable enable
65
namespace Prism.Mvvm
76
{
87
/// <summary>
@@ -13,7 +12,7 @@ public abstract class BindableBase : INotifyPropertyChanged
1312
/// <summary>
1413
/// Occurs when a property value changes.
1514
/// </summary>
16-
public event PropertyChangedEventHandler PropertyChanged;
15+
public event PropertyChangedEventHandler? PropertyChanged;
1716

1817
/// <summary>
1918
/// Checks if a property already matches a desired value. Sets the property and
@@ -27,7 +26,7 @@ public abstract class BindableBase : INotifyPropertyChanged
2726
/// support CallerMemberName.</param>
2827
/// <returns>True if the value was changed, false if the existing value matched the
2928
/// desired value.</returns>
30-
protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
29+
protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string? propertyName = null)
3130
{
3231
if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
3332

@@ -50,7 +49,8 @@ protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName]
5049
/// <param name="onChanged">Action that is called after the property value has been changed.</param>
5150
/// <returns>True if the value was changed, false if the existing value matched the
5251
/// desired value.</returns>
53-
protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)
52+
protected virtual bool SetProperty<T>(ref T storage, T value, Action? onChanged,
53+
[CallerMemberName] string? propertyName = null)
5454
{
5555
if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
5656

@@ -67,7 +67,7 @@ protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged,
6767
/// <param name="propertyName">Name of the property used to notify listeners. This
6868
/// value is optional and can be provided automatically when invoked from compilers
6969
/// that support <see cref="CallerMemberNameAttribute"/>.</param>
70-
protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
70+
protected void RaisePropertyChanged([CallerMemberName] string? propertyName = null)
7171
{
7272
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
7373
}

src/Prism.Core/Mvvm/ErrorsContainer.cs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
41
using System.Linq.Expressions;
52

3+
#nullable enable
64
namespace Prism.Mvvm
75
{
86
/// <summary>
@@ -53,21 +51,17 @@ public bool HasErrors
5351
/// Returns all the errors in the container.
5452
/// </summary>
5553
/// <returns>The dictionary of errors per property.</returns>
56-
public Dictionary<string, List<T>> GetErrors()
57-
{
58-
return validationResults;
59-
}
54+
public Dictionary<string, List<T>> GetErrors() => validationResults;
6055

6156
/// <summary>
6257
/// Gets the validation errors for a specified property.
6358
/// </summary>
6459
/// <param name="propertyName">The name of the property.</param>
6560
/// <returns>The validation errors of type <typeparamref name="T"/> for the property.</returns>
66-
public IEnumerable<T> GetErrors(string propertyName)
61+
public IEnumerable<T> GetErrors(string? propertyName)
6762
{
6863
var localPropertyName = propertyName ?? string.Empty;
69-
List<T> currentValidationResults = null;
70-
if (this.validationResults.TryGetValue(localPropertyName, out currentValidationResults))
64+
if (this.validationResults.TryGetValue(localPropertyName, out var currentValidationResults))
7165
{
7266
return currentValidationResults;
7367
}
@@ -110,10 +104,7 @@ public void ClearErrors<TProperty>(Expression<Func<TProperty>> propertyExpressio
110104
/// <example>
111105
/// container.ClearErrors("SomeProperty");
112106
/// </example>
113-
public void ClearErrors(string propertyName)
114-
{
115-
this.SetErrors(propertyName, new List<T>());
116-
}
107+
public void ClearErrors(string? propertyName) => this.SetErrors(propertyName, new List<T>());
117108

118109
/// <summary>
119110
/// Sets the validation errors for the specified property.
@@ -122,7 +113,7 @@ public void ClearErrors(string propertyName)
122113
/// <param name="propertyExpression">The <see cref="Expression"/> indicating the property.</param>
123114
/// <param name="propertyErrors">The list of errors to set for the property.</param>
124115
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
125-
public void SetErrors<TProperty>(Expression<Func<TProperty>> propertyExpression, IEnumerable<T> propertyErrors)
116+
public void SetErrors<TProperty>(Expression<Func<TProperty>> propertyExpression, IEnumerable<T>? propertyErrors)
126117
{
127118
var propertyName = PropertySupport.ExtractPropertyName(propertyExpression);
128119
this.SetErrors(propertyName, propertyErrors);
@@ -136,7 +127,7 @@ public void SetErrors<TProperty>(Expression<Func<TProperty>> propertyExpression,
136127
/// </remarks>
137128
/// <param name="propertyName">The name of the property.</param>
138129
/// <param name="newValidationResults">The new validation errors.</param>
139-
public void SetErrors(string propertyName, IEnumerable<T> newValidationResults)
130+
public void SetErrors(string? propertyName, IEnumerable<T>? newValidationResults)
140131
{
141132
var localPropertyName = propertyName ?? string.Empty;
142133
var hasCurrentValidationResults = this.validationResults.ContainsKey(localPropertyName);

src/Prism.Core/Mvvm/IViewRegistry.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using Prism.Ioc;
4-
5-
namespace Prism.Mvvm;
1+
namespace Prism.Mvvm;
62

73
/// <summary>
84
/// Provides an abstraction layer for ViewRegistration that can be mocked

src/Prism.Core/Mvvm/PropertySupport.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
2-
3-
using System;
41
using System.Linq.Expressions;
52
using System.Reflection;
63
using Prism.Properties;
74

5+
#nullable enable
86
namespace Prism.Mvvm
97
{
108
///<summary>
@@ -24,7 +22,7 @@ public static class PropertySupport
2422
/// The <see cref="MemberExpression"/> does not represent a property.<br/>
2523
/// Or, the property is static.
2624
/// </exception>
27-
public static string ExtractPropertyName<T>(Expression<Func<T>> propertyExpression)
25+
public static string? ExtractPropertyName<T>(Expression<Func<T>> propertyExpression)
2826
{
2927
if (propertyExpression == null)
3028
throw new ArgumentNullException(nameof(propertyExpression));
@@ -42,7 +40,7 @@ public static string ExtractPropertyName<T>(Expression<Func<T>> propertyExpressi
4240
/// The <see cref="MemberExpression"/> does not represent a property.<br/>
4341
/// Or, the property is static.
4442
/// </exception>
45-
internal static string ExtractPropertyNameFromLambda(LambdaExpression expression)
43+
internal static string? ExtractPropertyNameFromLambda(LambdaExpression expression)
4644
{
4745
if (expression == null)
4846
throw new ArgumentNullException(nameof(expression));

src/Prism.Core/Mvvm/ViewCreationException.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
1+
#nullable enable
32
namespace Prism.Mvvm;
43

54
/// <summary>
@@ -23,7 +22,7 @@ public ViewCreationException(string viewName, ViewType viewType)
2322
/// <param name="viewName">The name of the view that failed to create.</param>
2423
/// <param name="viewType">The type of view that failed to create (Page, Region, or Dialog).</param>
2524
/// <param name="innerException">The inner exception that caused the view creation to fail.</param>
26-
public ViewCreationException(string viewName, ViewType viewType, Exception innerException)
25+
public ViewCreationException(string viewName, ViewType viewType, Exception? innerException)
2726
: base($"Unable to create {viewType} '{viewName}'.", innerException)
2827
{
2928
ViewName = viewName;

src/Prism.Core/Mvvm/ViewModelCreationException.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
using System;
2-
using System.ComponentModel;
1+
using System.ComponentModel;
32

3+
#nullable enable
44
namespace Prism.Mvvm;
55

66
/// <summary>
77
/// Exception thrown when an error occurs during ViewModel creation.
88
/// </summary>
99
public class ViewModelCreationException : Exception
1010
{
11-
private static Func<object, string> _viewNameDelegate = null;
11+
private static Func<object, string>? _viewNameDelegate;
1212

1313
/// <summary>
1414
/// Gets the name of the view associated with the exception, based on platform-specific logic.

src/Prism.Core/Mvvm/ViewModelLocationProvider.cs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
2-
3-
using System;
4-
using System.Collections.Generic;
51
using System.ComponentModel;
62
using System.Diagnostics.CodeAnalysis;
73
using System.Globalization;
84
using System.Reflection;
95

6+
#nullable enable
107
namespace Prism.Mvvm
118
{
129
/// <summary>
@@ -52,24 +49,24 @@ public static void Reset()
5249
/// <summary>
5350
/// ViewModelFactory that provides the View instance and ViewModel type as parameters.
5451
/// </summary>
55-
static Func<object, Type, object> _defaultViewModelFactoryWithViewParameter;
52+
static Func<object, Type, object>? _defaultViewModelFactoryWithViewParameter;
5653

5754
/// <summary>
5855
/// Default view type to view model type resolver, assumes the view model is in same assembly as the view type, but in the "ViewModels" namespace.
5956
/// </summary>
60-
static Func<Type, Type> _defaultViewTypeToViewModelTypeResolver = DefaultViewTypeToViewModel;
57+
static Func<Type, Type?> _defaultViewTypeToViewModelTypeResolver = DefaultViewTypeToViewModel;
6158

62-
private static Type DefaultViewTypeToViewModel(Type viewType)
59+
private static Type? DefaultViewTypeToViewModel(Type viewType)
6360
{
6461
var viewName = viewType.FullName;
65-
viewName = viewName.Replace(".Views.", ".ViewModels.");
62+
viewName = viewName?.Replace(".Views.", ".ViewModels.");
6663
var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName;
67-
var suffix = viewName.EndsWith("View") ? "Model" : "ViewModel";
64+
var suffix = viewName != null && viewName.EndsWith("View") ? "Model" : "ViewModel";
6865
var viewModelName = string.Format(CultureInfo.InvariantCulture, "{0}{1}, {2}", viewName, suffix, viewAssemblyName);
6966
return Type.GetType(viewModelName);
7067
}
7168

72-
static Func<object, Type> _defaultViewToViewModelTypeResolver = view => null;
69+
static Func<object, Type?> _defaultViewToViewModelTypeResolver = view => null;
7370

7471
/// <summary>
7572
/// Sets the default view model factory.
@@ -93,7 +90,7 @@ public static void SetDefaultViewModelFactory(Func<object, Type, object> viewMod
9390
/// Sets the default view type to view model type resolver.
9491
/// </summary>
9592
/// <param name="viewTypeToViewModelTypeResolver">The view type to view model type resolver.</param>
96-
public static void SetDefaultViewTypeToViewModelTypeResolver(Func<Type, Type> viewTypeToViewModelTypeResolver)
93+
public static void SetDefaultViewTypeToViewModelTypeResolver(Func<Type, Type?> viewTypeToViewModelTypeResolver)
9794
{
9895
_defaultViewTypeToViewModelTypeResolver = viewTypeToViewModelTypeResolver;
9996
}
@@ -102,7 +99,7 @@ public static void SetDefaultViewTypeToViewModelTypeResolver(Func<Type, Type> vi
10299
/// Sets the default ViewModel Type Resolver given the View instance. This can be used to evaluate the View for
103100
/// custom attributes or Attached Properties to determine the ViewModel Type.
104101
/// </summary>
105-
public static void SetDefaultViewToViewModelTypeResolver(Func<object, Type> viewToViewModelTypeResolver) =>
102+
public static void SetDefaultViewToViewModelTypeResolver(Func<object, Type?> viewToViewModelTypeResolver) =>
106103
_defaultViewToViewModelTypeResolver = viewToViewModelTypeResolver;
107104

108105
/// <summary>
@@ -114,7 +111,7 @@ public static void SetDefaultViewToViewModelTypeResolver(Func<object, Type> view
114111
public static void AutoWireViewModelChanged(object view, Action<object, object> setDataContextCallback)
115112
{
116113
// Try mappings first
117-
object viewModel = GetViewModelForView(view);
114+
object? viewModel = GetViewModelForView(view);
118115

119116
// try to use ViewModel type
120117
if (viewModel == null)
@@ -144,30 +141,24 @@ public static void AutoWireViewModelChanged(object view, Action<object, object>
144141
/// </summary>
145142
/// <param name="view">The view that the view model wants.</param>
146143
/// <returns>The ViewModel that corresponds to the view passed as a parameter.</returns>
147-
private static object GetViewModelForView(object view)
144+
private static object? GetViewModelForView(object view)
148145
{
149146
var viewKey = view.GetType().ToString();
150147

151148
// Mapping of view models base on view type (or instance) goes here
152-
if (_factories.ContainsKey(viewKey))
153-
return _factories[viewKey]();
154-
155-
return null;
149+
return _factories.ContainsKey(viewKey) ? _factories[viewKey]() : null;
156150
}
157151

158152
/// <summary>
159153
/// Gets the ViewModel type for the specified view.
160154
/// </summary>
161155
/// <param name="view">The View that the ViewModel wants.</param>
162156
/// <returns>The ViewModel type that corresponds to the View.</returns>
163-
private static Type GetViewModelTypeForView(Type view)
157+
private static Type? GetViewModelTypeForView(Type view)
164158
{
165159
var viewKey = view.ToString();
166160

167-
if (_typeFactories.ContainsKey(viewKey))
168-
return _typeFactories[viewKey];
169-
170-
return null;
161+
return _typeFactories.ContainsKey(viewKey) ? _typeFactories[viewKey] : null;
171162
}
172163

173164
/// <summary>

src/Prism.Core/Mvvm/ViewRegistration.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
3-
namespace Prism.Mvvm;
1+
namespace Prism.Mvvm;
42

53
/// <summary>
64
/// Represents information about a registered view.

0 commit comments

Comments
 (0)