Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Guard for null checks #4585

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Source/Csla.Channels.Grpc/GrpcPortal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public override async Task<ResponseMessage> Invoke(RequestMessage request, Serve
{
if (request is null)
throw new ArgumentNullException(nameof(request));
if (context is null)
throw new ArgumentNullException(nameof(context));
Guard.NotNull(context);

var operation = request.Operation;
if (operation.Contains("/"))
Expand Down
33 changes: 11 additions & 22 deletions Source/Csla.Channels.RabbitMq/RabbitMqProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,9 @@ private void DisposeRabbitMq()
/// <inheritdoc />
public override async Task<DataPortalResult> Create(Type objectType, object criteria, DataPortalContext context, bool isSync)
{
if (objectType is null)
throw new ArgumentNullException(nameof(objectType));
if (criteria is null)
throw new ArgumentNullException(nameof(criteria));
if (context is null)
throw new ArgumentNullException(nameof(context));
Guard.NotNull(objectType);
Guard.NotNull(criteria);
Guard.NotNull(context);
if (isSync)
throw new NotSupportedException("isSync == true");

Expand All @@ -139,12 +136,9 @@ public override async Task<DataPortalResult> Create(Type objectType, object crit
/// <inheritdoc />
public override async Task<DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
{
if (objectType is null)
throw new ArgumentNullException(nameof(objectType));
if (criteria is null)
throw new ArgumentNullException(nameof(criteria));
if (context is null)
throw new ArgumentNullException(nameof(context));
Guard.NotNull(objectType);
Guard.NotNull(criteria);
Guard.NotNull(context);
if (isSync)
throw new NotSupportedException("isSync == true");

Expand All @@ -162,10 +156,8 @@ public override async Task<DataPortalResult> Fetch(Type objectType, object crite
/// <inheritdoc />
public override async Task<DataPortalResult> Update(object obj, DataPortalContext context, bool isSync)
{
if (obj is null)
throw new ArgumentNullException(nameof(obj));
if (context is null)
throw new ArgumentNullException(nameof(context));
Guard.NotNull(obj);
Guard.NotNull(context);
if (isSync)
throw new NotSupportedException("isSync == true");

Expand All @@ -183,12 +175,9 @@ public override async Task<DataPortalResult> Update(object obj, DataPortalContex
/// <inheritdoc />
public override async Task<DataPortalResult> Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
{
if (objectType is null)
throw new ArgumentNullException(nameof(objectType));
if (criteria is null)
throw new ArgumentNullException(nameof(criteria));
if (context is null)
throw new ArgumentNullException(nameof(context));
Guard.NotNull(objectType);
Guard.NotNull(criteria);
Guard.NotNull(context);
if (isSync)
throw new NotSupportedException("isSync == true");

Expand Down
42 changes: 14 additions & 28 deletions Source/Csla/BusinessBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,7 @@ async Task<object> ISavable.SaveAsync(bool forceUpdate)
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected virtual void OnSaved(T newObject, Exception? e, object? userState)
{
if (newObject is null)
throw new ArgumentNullException(nameof(newObject));
Guard.NotNull(newObject);

SavedEventArgs args = new SavedEventArgs(newObject, e, userState);
_nonSerializableSavedHandlers?.Invoke(this, args);
Expand All @@ -375,8 +374,7 @@ protected virtual void OnSaved(T newObject, Exception? e, object? userState)
/// <exception cref="ArgumentNullException"><paramref name="info"/> is <see langword="null"/>.</exception>
protected static PropertyInfo<P> RegisterProperty<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] P>(PropertyInfo<P> info)
{
if (info is null)
throw new ArgumentNullException(nameof(info));
Guard.NotNull(info);

return Core.FieldManager.PropertyInfoManager.RegisterProperty<P>(typeof(T), info);
}
Expand All @@ -390,8 +388,7 @@ protected virtual void OnSaved(T newObject, Exception? e, object? userState)
/// <exception cref="ArgumentNullException"><paramref name="propertyName"/> is <see langword="null"/>.</exception>
protected static PropertyInfo<P> RegisterProperty<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] P>(string propertyName)
{
if (propertyName is null)
throw new ArgumentNullException(nameof(propertyName));
Guard.NotNull(propertyName);

return RegisterProperty(Core.FieldManager.PropertyInfoFactory.Factory.Create<P>(typeof(T), propertyName));
}
Expand All @@ -405,8 +402,7 @@ protected virtual void OnSaved(T newObject, Exception? e, object? userState)
/// <exception cref="ArgumentNullException"><paramref name="propertyLambdaExpression"/> is <see langword="null"/>.</exception>
protected static PropertyInfo<P> RegisterProperty<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] P>(Expression<Func<T, object>> propertyLambdaExpression)
{
if (propertyLambdaExpression is null)
throw new ArgumentNullException(nameof(propertyLambdaExpression));
Guard.NotNull(propertyLambdaExpression);

PropertyInfo reflectedPropertyInfo = Reflect<T>.GetProperty(propertyLambdaExpression);
return RegisterProperty<P>(reflectedPropertyInfo.Name);
Expand All @@ -422,8 +418,7 @@ protected virtual void OnSaved(T newObject, Exception? e, object? userState)
/// <exception cref="ArgumentNullException"><paramref name="propertyName"/> is <see langword="null"/>.</exception>
protected static PropertyInfo<P> RegisterProperty<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] P>(string propertyName, RelationshipTypes relationship)
{
if (propertyName is null)
throw new ArgumentNullException(nameof(propertyName));
Guard.NotNull(propertyName);

return RegisterProperty(Core.FieldManager.PropertyInfoFactory.Factory.Create<P>(typeof(T), propertyName, string.Empty, relationship));
}
Expand All @@ -438,8 +433,7 @@ protected virtual void OnSaved(T newObject, Exception? e, object? userState)
/// <exception cref="ArgumentNullException"><paramref name="propertyLambdaExpression"/> is <see langword="null"/>.</exception>
protected static PropertyInfo<P> RegisterProperty<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] P>(Expression<Func<T, object>> propertyLambdaExpression, RelationshipTypes relationship)
{
if (propertyLambdaExpression is null)
throw new ArgumentNullException(nameof(propertyLambdaExpression));
Guard.NotNull(propertyLambdaExpression);

PropertyInfo reflectedPropertyInfo = Reflect<T>.GetProperty(propertyLambdaExpression);
return RegisterProperty<P>(reflectedPropertyInfo.Name, relationship);
Expand All @@ -455,8 +449,7 @@ protected virtual void OnSaved(T newObject, Exception? e, object? userState)
/// <exception cref="ArgumentNullException"><paramref name="propertyName"/> is <see langword="null"/>.</exception>
protected static PropertyInfo<P> RegisterProperty<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] P>(string propertyName, string? friendlyName)
{
if (propertyName is null)
throw new ArgumentNullException(nameof(propertyName));
Guard.NotNull(propertyName);

return RegisterProperty(Core.FieldManager.PropertyInfoFactory.Factory.Create<P>(typeof(T), propertyName, friendlyName));
}
Expand All @@ -471,8 +464,7 @@ protected virtual void OnSaved(T newObject, Exception? e, object? userState)
/// <exception cref="ArgumentNullException"><paramref name="propertyLambdaExpression"/> is <see langword="null"/>.</exception>
protected static PropertyInfo<P> RegisterProperty<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] P>(Expression<Func<T, object>> propertyLambdaExpression, string? friendlyName)
{
if (propertyLambdaExpression is null)
throw new ArgumentNullException(nameof(propertyLambdaExpression));
Guard.NotNull(propertyLambdaExpression);

PropertyInfo reflectedPropertyInfo = Reflect<T>.GetProperty(propertyLambdaExpression);
return RegisterProperty<P>(reflectedPropertyInfo.Name, friendlyName);
Expand All @@ -489,8 +481,7 @@ protected virtual void OnSaved(T newObject, Exception? e, object? userState)
/// <exception cref="ArgumentNullException"><paramref name="propertyName"/> is <see langword="null"/>.</exception>
protected static PropertyInfo<P> RegisterProperty<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] P>(string propertyName, string? friendlyName, P? defaultValue)
{
if (propertyName is null)
throw new ArgumentNullException(nameof(propertyName));
Guard.NotNull(propertyName);

return RegisterProperty(Core.FieldManager.PropertyInfoFactory.Factory.Create<P>(typeof(T), propertyName, friendlyName, defaultValue));
}
Expand All @@ -506,8 +497,7 @@ protected virtual void OnSaved(T newObject, Exception? e, object? userState)
/// <exception cref="ArgumentNullException"><paramref name="propertyLambdaExpression"/> is <see langword="null"/>.</exception>
protected static PropertyInfo<P> RegisterProperty<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] P>(Expression<Func<T, object>> propertyLambdaExpression, string? friendlyName, P? defaultValue)
{
if (propertyLambdaExpression is null)
throw new ArgumentNullException(nameof(propertyLambdaExpression));
Guard.NotNull(propertyLambdaExpression);

PropertyInfo reflectedPropertyInfo = Reflect<T>.GetProperty(propertyLambdaExpression);
return RegisterProperty(reflectedPropertyInfo.Name, friendlyName, defaultValue);
Expand All @@ -525,8 +515,7 @@ protected virtual void OnSaved(T newObject, Exception? e, object? userState)
/// <exception cref="ArgumentNullException"><paramref name="propertyName"/> is <see langword="null"/>.</exception>
protected static PropertyInfo<P> RegisterProperty<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] P>(string propertyName, string? friendlyName, P? defaultValue, RelationshipTypes relationship)
{
if (propertyName is null)
throw new ArgumentNullException(nameof(propertyName));
Guard.NotNull(propertyName);

return RegisterProperty(Core.FieldManager.PropertyInfoFactory.Factory.Create<P>(typeof(T), propertyName, friendlyName, defaultValue, relationship));
}
Expand All @@ -543,8 +532,7 @@ protected virtual void OnSaved(T newObject, Exception? e, object? userState)
/// <exception cref="ArgumentNullException"><paramref name="propertyLambdaExpression"/> is <see langword="null"/>.</exception>
protected static PropertyInfo<P> RegisterProperty<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] P>(Expression<Func<T, object>> propertyLambdaExpression, string? friendlyName, P? defaultValue, RelationshipTypes relationship)
{
if (propertyLambdaExpression is null)
throw new ArgumentNullException(nameof(propertyLambdaExpression));
Guard.NotNull(propertyLambdaExpression);

PropertyInfo reflectedPropertyInfo = Reflect<T>.GetProperty(propertyLambdaExpression);
return RegisterProperty(reflectedPropertyInfo.Name, friendlyName, defaultValue, relationship);
Expand All @@ -557,8 +545,7 @@ protected virtual void OnSaved(T newObject, Exception? e, object? userState)
/// <exception cref="ArgumentNullException"><paramref name="methodName"/> is <see langword="null"/>.</exception>
protected static MethodInfo RegisterMethod(string methodName)
{
if (methodName is null)
throw new ArgumentNullException(nameof(methodName));
Guard.NotNull(methodName);

return RegisterMethod(typeof(T), methodName);
}
Expand All @@ -570,8 +557,7 @@ protected static MethodInfo RegisterMethod(string methodName)
/// <exception cref="ArgumentNullException"><paramref name="methodLambdaExpression"/> is <see langword="null"/>.</exception>
protected static MethodInfo RegisterMethod(Expression<Action<T>> methodLambdaExpression)
{
if (methodLambdaExpression is null)
throw new ArgumentNullException(nameof(methodLambdaExpression));
Guard.NotNull(methodLambdaExpression);

System.Reflection.MethodInfo reflectedMethodInfo = Reflect<T>.GetMethod(methodLambdaExpression);
return RegisterMethod(reflectedMethodInfo.Name);
Expand Down
39 changes: 13 additions & 26 deletions Source/Csla/BusinessBindingListBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,7 @@ public void ApplyEdit()

Task IParent.ApplyEditChild(IEditableBusinessObject child)
{
if (child is null)
throw new ArgumentNullException(nameof(child));
Guard.NotNull(child);

EditChildComplete(child);
return Task.CompletedTask;
Expand Down Expand Up @@ -317,8 +316,7 @@ protected override object AddNewCore()
/// <exception cref="ArgumentNullException"><paramref name="child"/> is <see langword="null"/>.</exception>
void IEditableCollection.RemoveChild(IEditableBusinessObject child)
{
if (child is null)
throw new ArgumentNullException(nameof(child));
Guard.NotNull(child);

Remove((C)child);
}
Expand All @@ -331,8 +329,7 @@ object IEditableCollection.GetDeletedList()
/// <inheritdoc />
Task IParent.RemoveChild(IEditableBusinessObject child)
{
if (child is null)
throw new ArgumentNullException(nameof(child));
Guard.NotNull(child);

Remove((C)child);
return Task.CompletedTask;
Expand Down Expand Up @@ -616,8 +613,7 @@ private void AcceptChanges(int parentEditLevel)
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected override void OnGetState(Serialization.Mobile.SerializationInfo info)
{
if (info is null)
throw new ArgumentNullException(nameof(info));
Guard.NotNull(info);

info.AddValue("Csla.BusinessListBase._isChild", _isChild);
info.AddValue("Csla.BusinessListBase._editLevel", EditLevel);
Expand All @@ -636,8 +632,7 @@ protected override void OnGetState(Serialization.Mobile.SerializationInfo info)
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected override void OnSetState(Serialization.Mobile.SerializationInfo info)
{
if (info is null)
throw new ArgumentNullException(nameof(info));
Guard.NotNull(info);

_isChild = info.GetValue<bool>("Csla.BusinessListBase._isChild");
EditLevel = info.GetValue<int>("Csla.BusinessListBase._editLevel");
Expand All @@ -659,10 +654,8 @@ protected override void OnSetState(Serialization.Mobile.SerializationInfo info)
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected override void OnGetChildren(Serialization.Mobile.SerializationInfo info, Serialization.Mobile.MobileFormatter formatter)
{
if (info is null)
throw new ArgumentNullException(nameof(info));
if (formatter is null)
throw new ArgumentNullException(nameof(formatter));
Guard.NotNull(info);
Guard.NotNull(formatter);

base.OnGetChildren(info, formatter);
if (_deletedList != null)
Expand All @@ -686,10 +679,8 @@ protected override void OnGetChildren(Serialization.Mobile.SerializationInfo inf
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected override void OnSetChildren(Serialization.Mobile.SerializationInfo info, Serialization.Mobile.MobileFormatter formatter)
{
if (info is null)
throw new ArgumentNullException(nameof(info));
if (formatter is null)
throw new ArgumentNullException(nameof(formatter));
Guard.NotNull(info);
Guard.NotNull(formatter);

if (info.Children.TryGetValue("_deletedList", out var child))
{
Expand Down Expand Up @@ -906,8 +897,7 @@ protected virtual void Child_Create()
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected virtual void Child_Update(params object?[] parameters)
{
if (parameters is null)
throw new ArgumentNullException(nameof(parameters));
Guard.NotNull(parameters);

using (LoadListMode)
{
Expand All @@ -934,8 +924,7 @@ protected virtual void Child_Update(params object?[] parameters)
[UpdateChild]
protected virtual async Task Child_UpdateAsync(params object?[] parameters)
{
if (parameters is null)
throw new ArgumentNullException(nameof(parameters));
Guard.NotNull(parameters);

using (LoadListMode)
{
Expand Down Expand Up @@ -1215,8 +1204,7 @@ public event EventHandler<SavedEventArgs>? Saved
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected virtual void OnSaved(T newObject, Exception? e, object? userState)
{
if (newObject is null)
throw new ArgumentNullException(nameof(newObject));
Guard.NotNull(newObject);

SavedEventArgs args = new SavedEventArgs(newObject, e, userState);
_nonSerializableSavedHandlers?.Invoke(this, args);
Expand Down Expand Up @@ -1370,8 +1358,7 @@ void IDataPortalTarget.Child_OnDataPortalException(DataPortalEventArgs e, Except
[EditorBrowsable(EditorBrowsableState.Never)]
protected override void Child_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e is null)
throw new ArgumentNullException(nameof(e));
Guard.NotNull(e);

if (RaiseListChangedEvents)
{
Expand Down
Loading
Loading