Skip to content

Commit

Permalink
More code optimizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertmclaws committed Oct 30, 2023
1 parent 3ccea9e commit 1908c03
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ private Task InvokeProcessorMethodAsync(SubmitContext context, ChangeSetItem ite
if (expectedMethod is null)
{
var actualMethodName = expectedMethodName.Replace(dataModification.ExpectedResourceType.Name, dataModification.ResourceSetName);
var actualMethod = targetApiType.GetQualifiedMethod(actualMethodName);
if (actualMethod is not null)
if (targetApiType.GetQualifiedMethod(actualMethodName) is not null)
{
Trace.WriteLine($"Restier ConventionBasedChangeSetItemFilter expected'{expectedMethodName}' but found '{actualMethodName}'. Your method will not be called until you correct the method name.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,9 @@ public Task ValidateChangeSetItemAsync( SubmitContext context, ChangeSetItem ite
{
validationContext.MemberName = property.Name;

var validationAttributes = property.Attributes.OfType<ValidationAttribute>();
foreach (var validationAttribute in validationAttributes)
foreach (var validationAttribute in property.Attributes.OfType<ValidationAttribute>())
{
var value = property.GetValue(resource);
var validationResult = validationAttribute.GetValidationResult(value, validationContext);
var validationResult = validationAttribute.GetValidationResult(property.GetValue(resource), validationContext);
if (validationResult != ValidationResult.Success)
{
validationResults.Add(new ChangeSetItemValidationResult()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public Task<bool> AuthorizeAsync(OperationContext context, CancellationToken can
Ensure.NotNull(context, nameof(context));
var result = true;

var returnType = typeof(bool);
var expectedMethodName = ConventionBasedMethodNameFactory.GetFunctionMethodName(context, RestierPipelineState.Authorization, RestierOperationMethod.Execute);

//RWM: This prefers the Sync name over the Async name, because in V1 Sync has been the only option for a decade. In v2, we'll probably just make everything Async without Sync calls.
Expand All @@ -51,7 +50,7 @@ public Task<bool> AuthorizeAsync(OperationContext context, CancellationToken can
return Task.FromResult(result);
}

if (expectedMethod.ReturnType != returnType)
if (expectedMethod.ReturnType != typeof(bool))
{
Trace.WriteLine($"Restier ConventionBasedOperationAuthorizer found '{expectedMethodName}' but it does not return a boolean value. Your method will not be called until you correct the return type.");
return Task.FromResult(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ public Expression Process(QueryExpressionContext context)

if (context.ModelReference is DataSourceStubModelReference dataSourceStubReference)
{
if (!(dataSourceStubReference.Element is IEdmEntitySet entitySet))
if (dataSourceStubReference.Element is not IEdmEntitySet entitySet)
{
return null;
}

if (!(entitySet.Type is IEdmCollectionType collectionType))
if (entitySet.Type is not IEdmCollectionType collectionType)
{
return null;
}

if (!(collectionType.ElementType.Definition is IEdmEntityType entityType))
if (collectionType.ElementType.Definition is not IEdmEntityType entityType)
{
return null;
}
Expand All @@ -77,7 +77,7 @@ public Expression Process(QueryExpressionContext context)
propType = collectionType.ElementType;
}

if (!(propType.Definition is IEdmEntityType entityType))
if (propType.Definition is not IEdmEntityType entityType)
{
return null;
}
Expand Down Expand Up @@ -146,8 +146,7 @@ private Expression AppendOnFilterExpression(QueryExpressionContext context, IEdm
// For collection property of derived type, will be like "Param_0.Prop.AsQueryable().Where(...).OfType()"
var returnType = context.VisitedNode.Type.FindGenericType(typeof(IQueryable<>));
var enumerableQueryParameter = (object)context.VisitedNode;
Type elementType = null;

Type elementType;
if (returnType is null)
{
// This means append for properties model reference
Expand All @@ -169,7 +168,7 @@ private Expression AppendOnFilterExpression(QueryExpressionContext context, IEdm

var queryType = typeof(EnumerableQuery<>).MakeGenericType(elementType);
var query = Activator.CreateInstance(queryType, enumerableQueryParameter);
if (!(expectedMethod.Invoke(apiBase, new object[] { query }) is IQueryable result))
if (expectedMethod.Invoke(apiBase, new object[] { query }) is not IQueryable result)
{
return null;
}
Expand Down

0 comments on commit 1908c03

Please sign in to comment.