Skip to content

Commit

Permalink
Nullable errors fixed in already migrated projects
Browse files Browse the repository at this point in the history
Fixes #1233
  • Loading branch information
StefanOssendorf committed Oct 23, 2024
1 parent 12ac4a0 commit 995490a
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public IPrincipal GetUser()
/// IHostEnvironmentAuthenticationStateProvider service.
/// </summary>
/// <param name="principal">Principal object.</param>
public virtual void SetUser(IPrincipal principal)
public virtual void SetUser(IPrincipal? principal)
{
if (!ReferenceEquals(CurrentPrincipal, principal))
{
Expand All @@ -151,13 +151,17 @@ public virtual void SetUser(IPrincipal principal)
}
else if (HttpContext is not null)
{
HttpContext.User = (ClaimsPrincipal)principal;
if (principal is not ClaimsPrincipal claimsPrincipal)
{
throw new ArgumentException("typeof(principal) != ClaimsPrincipal");
}
HttpContext.User = claimsPrincipal;
}
else
{
throw new InvalidOperationException("HttpContext==null, !CircuitExists");
}
CurrentPrincipal = principal;
CurrentPrincipal = principal ?? UnauthenticatedPrincipal;
}
}

Expand Down
9 changes: 3 additions & 6 deletions Source/Csla.Channels.Grpc/GrpcPortal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public async Task<DataPortalResponse> Delete(CriteriaRequest request)
request = ConvertRequest(request);

// unpack criteria data into object
object? criteria = GetCriteria(_applicationContext, request.CriteriaData);
object criteria = GetCriteria(_applicationContext, request.CriteriaData);
if (criteria is DataPortalClient.PrimitiveCriteria primitiveCriteria)
{
criteria = primitiveCriteria.Value;
Expand Down Expand Up @@ -345,12 +345,9 @@ public async Task<DataPortalResponse> Delete(CriteriaRequest request)

#region Criteria

private static object? GetCriteria(ApplicationContext applicationContext, byte[]? criteriaData)
private static object GetCriteria(ApplicationContext applicationContext, byte[] criteriaData)
{
object? criteria = null;
if (criteriaData != null)
criteria = applicationContext.GetRequiredService<ISerializationFormatter>().Deserialize(criteriaData);
return criteria;
return applicationContext.GetRequiredService<ISerializationFormatter>().Deserialize(criteriaData) ?? throw new SerializationException(Resources.ServerSideDataPortalRequestDeserializationFailed);
}

#endregion Criteria
Expand Down
11 changes: 4 additions & 7 deletions Source/Csla.Channels.RabbitMq/RabbitMqPortal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ private async Task<DataPortalResponse> Update(UpdateRequest request)
{
request = ConvertRequest(request);
// unpack object
object obj = GetCriteria(_applicationContext, request.ObjectData) ?? throw new InvalidOperationException(Resources.ObjectToBeUpdatedCouldNotBeDeserialized);
object obj = GetCriteria(_applicationContext, request.ObjectData ?? throw new InvalidOperationException(Resources.ObjectToBeUpdatedCouldNotBeDeserialized));

var context = new DataPortalContext(
_applicationContext, Deserialize<IPrincipal>(request.Principal),
Expand Down Expand Up @@ -278,7 +278,7 @@ private async Task<DataPortalResponse> Delete(CriteriaRequest request)
request = ConvertRequest(request);

// unpack criteria data into object
object? criteria = GetCriteria(_applicationContext, request.CriteriaData);
object criteria = GetCriteria(_applicationContext, request.CriteriaData);
if (criteria is DataPortalClient.PrimitiveCriteria primitiveCriteria)
{
criteria = primitiveCriteria.Value;
Expand Down Expand Up @@ -312,12 +312,9 @@ private async Task<DataPortalResponse> Delete(CriteriaRequest request)

#region Criteria

private static object? GetCriteria(ApplicationContext applicationContext, byte[]? criteriaData)
private static object GetCriteria(ApplicationContext applicationContext, byte[] criteriaData)
{
object? criteria = null;
if (criteriaData != null)
criteria = applicationContext.GetRequiredService<ISerializationFormatter>().Deserialize(criteriaData);
return criteria;
return applicationContext.GetRequiredService<ISerializationFormatter>().Deserialize(criteriaData) ?? throw new SerializationException(Resources.ServerSideDataPortalRequestDeserializationFailed);
}

#endregion Criteria
Expand Down
68 changes: 28 additions & 40 deletions Source/Csla.Channels.RabbitMq/RabbitMqProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,15 @@ private void DisposeRabbitMq()
Connection = null;
}

/// <summary>
/// Called by <see cref="DataPortal" /> to create a
/// new business object.
/// </summary>
/// <param name="objectType">Type of business object to create.</param>
/// <param name="criteria">Criteria object describing business object.</param>
/// <param name="context">DataPortalContext object passed to the server.</param>
/// <param name="isSync">True if the client-side proxy should synchronously invoke the server.</param>
public override async Task<DataPortalResult> Create(Type objectType, object? criteria, DataPortalContext context, bool isSync)
/// <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));
if (isSync)
throw new NotSupportedException("isSync == true");

Expand All @@ -137,18 +136,15 @@ public override async Task<DataPortalResult> Create(Type objectType, object? cri
}
}

/// <summary>
/// Called by <see cref="DataPortal" /> to load an
/// existing business object.
/// </summary>
/// <param name="objectType">Type of business object to create.</param>
/// <param name="criteria">Criteria object describing business object.</param>
/// <param name="context">
/// <see cref="Server.DataPortalContext" /> object passed to the server.
/// </param>
/// <param name="isSync">True if the client-side proxy should synchronously invoke the server.</param>
public override async Task<DataPortalResult> Fetch(Type objectType, object? criteria, DataPortalContext context, bool isSync)
/// <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));
if (isSync)
throw new NotSupportedException("isSync == true");

Expand All @@ -163,17 +159,12 @@ public override async Task<DataPortalResult> Fetch(Type objectType, object? crit
}
}

/// <summary>
/// Called by <see cref="DataPortal" /> to update a
/// business object.
/// </summary>
/// <param name="obj">The business object to update.</param>
/// <param name="context">
/// <see cref="Server.DataPortalContext" /> object passed to the server.
/// </param>
/// <param name="isSync">True if the client-side proxy should synchronously invoke the server.</param>
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));
if (isSync)
throw new NotSupportedException("isSync == true");

Expand All @@ -188,18 +179,15 @@ public override async Task<DataPortalResult> Update(object obj, DataPortalContex
}
}

/// <summary>
/// Called by <see cref="DataPortal" /> to delete a
/// business object.
/// </summary>
/// <param name="objectType">Type of business object to create.</param>
/// <param name="criteria">Criteria object describing business object.</param>
/// <param name="context">
/// <see cref="Server.DataPortalContext" /> object passed to the server.
/// </param>
/// <param name="isSync">True if the client-side proxy should synchronously invoke the server.</param>
public override async Task<DataPortalResult> Delete(Type objectType, object? criteria, DataPortalContext context, bool isSync)
/// <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));
if (isSync)
throw new NotSupportedException("isSync == true");

Expand Down
2 changes: 1 addition & 1 deletion Source/Csla.Web.Mvc.Shared/ApplicationContextManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public System.Security.Principal.IPrincipal GetUser()
/// Sets the current principal.
/// </summary>
/// <param name="principal">Principal object.</param>
public void SetUser(System.Security.Principal.IPrincipal principal)
public void SetUser(System.Security.Principal.IPrincipal? principal)
{
HttpContext.Current.User = principal;
}
Expand Down
35 changes: 23 additions & 12 deletions Source/Csla.Web.Mvc.Shared/Server/Hosts/HttpPortal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ namespace Csla.Server.Hosts
/// </summary>
public class HttpPortal
{
private IDataPortalServer dataPortalServer;
private ApplicationContext _applicationContext;
private readonly IDataPortalServer dataPortalServer;
private readonly ApplicationContext _applicationContext;

/// <summary>
/// Creates an instance of the type
Expand All @@ -31,16 +31,20 @@ public class HttpPortal
/// <param name="dataPortal">Data portal server service</param>
public HttpPortal(ApplicationContext applicationContext, IDataPortalServer dataPortal)
{
dataPortalServer = dataPortal;
_applicationContext = applicationContext;
dataPortalServer = dataPortal ?? throw new ArgumentNullException(nameof(dataPortal));
_applicationContext = applicationContext ?? throw new ArgumentNullException(nameof(applicationContext));
}

/// <summary>
/// Create and initialize an existing business object.
/// </summary>
/// <param name="request">The request parameter object.</param>
/// <exception cref="ArgumentNullException"><paramref name="request"/> is <see langword="null"/>.</exception>
public async Task<DataPortalResponse> Create(CriteriaRequest request)
{
if (request is null)
throw new ArgumentNullException(nameof(request));

var result = _applicationContext.CreateInstanceDI<DataPortalResponse>();
try
{
Expand Down Expand Up @@ -83,8 +87,12 @@ public async Task<DataPortalResponse> Create(CriteriaRequest request)
/// Get an existing business object.
/// </summary>
/// <param name="request">The request parameter object.</param>
/// <exception cref="ArgumentNullException"><paramref name="request"/> is <see langword="null"/>.</exception>
public async Task<DataPortalResponse> Fetch(CriteriaRequest request)
{
if (request is null)
throw new ArgumentNullException(nameof(request));

var result = _applicationContext.CreateInstanceDI<DataPortalResponse>();
try
{
Expand Down Expand Up @@ -127,14 +135,18 @@ public async Task<DataPortalResponse> Fetch(CriteriaRequest request)
/// Update a business object.
/// </summary>
/// <param name="request">The request parameter object.</param>
/// <exception cref="ArgumentNullException"><paramref name="request"/> is <see langword="null"/>.</exception>
public async Task<DataPortalResponse> Update(UpdateRequest request)
{
if (request is null)
throw new ArgumentNullException(nameof(request));

var result = _applicationContext.CreateInstanceDI<DataPortalResponse>();
try
{
request = ConvertRequest(request);
// unpack object
object obj = GetCriteria(_applicationContext, request.ObjectData) ?? throw new InvalidOperationException(Resources.ObjectToBeUpdatedCouldNotBeDeserialized);
object obj = GetCriteria(_applicationContext, request.ObjectData ?? throw new InvalidOperationException(Resources.ObjectToBeUpdatedCouldNotBeDeserialized));

var context = new DataPortalContext(
_applicationContext, Deserialize<IPrincipal>(request.Principal),
Expand Down Expand Up @@ -166,8 +178,12 @@ public async Task<DataPortalResponse> Update(UpdateRequest request)
/// Delete a business object.
/// </summary>
/// <param name="request">The request parameter object.</param>
/// <exception cref="ArgumentNullException"><paramref name="request"/> is <see langword="null"/>.</exception>
public async Task<DataPortalResponse> Delete(CriteriaRequest request)
{
if (request is null)
throw new ArgumentNullException(nameof(request));

var result = _applicationContext.CreateInstanceDI<DataPortalResponse>();
try
{
Expand Down Expand Up @@ -208,12 +224,9 @@ public async Task<DataPortalResponse> Delete(CriteriaRequest request)

#region Criteria

private static object? GetCriteria(ApplicationContext applicationContext, byte[]? criteriaData)
private static object GetCriteria(ApplicationContext applicationContext, byte[] criteriaData)
{
object? criteria = null;
if (criteriaData != null)
criteria = applicationContext.GetRequiredService<ISerializationFormatter>().Deserialize(criteriaData);
return criteria;
return applicationContext.GetRequiredService<ISerializationFormatter>().Deserialize(criteriaData) ?? throw new SerializationException(Resources.ServerSideDataPortalRequestDeserializationFailed);
}

#endregion Criteria
Expand Down Expand Up @@ -256,9 +269,7 @@ private T Deserialize<T>(byte[] data)
{
var deserializedData = _applicationContext.GetRequiredService<ISerializationFormatter>().Deserialize(data) ?? throw new SerializationException(Resources.ServerSideDataPortalRequestDeserializationFailed);
if (deserializedData is not T castedData)
{
throw new SerializationException(string.Format(Resources.DeserializationFailedDueToWrongData, typeof(T).FullName));
}

return castedData;
}
Expand Down
Loading

0 comments on commit 995490a

Please sign in to comment.