Skip to content

Commit dea63fd

Browse files
committed
Remove ObjectExtensions.AsEnumerable/AsArray/AsList
1 parent 9546bcb commit dea63fd

File tree

23 files changed

+43
-58
lines changed

23 files changed

+43
-58
lines changed

src/Examples/DapperExample/TranslationToSql/Builders/SelectStatementBuilder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ private void TrackPrimaryTable(TableAccessorNode tableAccessor)
106106
_selectorsPerTable[tableAccessor] = _selectShape switch
107107
{
108108
SelectShape.Columns => Array.Empty<SelectorNode>(),
109-
SelectShape.Count => new CountSelectorNode(null).AsArray(),
110-
_ => new OneSelectorNode(null).AsArray()
109+
SelectShape.Count => [new CountSelectorNode(null)],
110+
_ => [new OneSelectorNode(null)]
111111
};
112112
}
113113

src/Examples/DapperExample/TranslationToSql/Builders/UpdateClearOneToOneStatementBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public UpdateNode Build(ResourceType resourceType, string setColumnName, string
2929
ColumnNode whereColumn = table.GetColumn(whereColumnName, null, table.Alias);
3030
WhereNode where = GetWhere(whereColumn, whereValue);
3131

32-
return new UpdateNode(table, columnAssignment.AsList(), where);
32+
return new UpdateNode(table, [columnAssignment], where);
3333
}
3434

3535
private WhereNode GetWhere(ColumnNode column, object? value)

src/JsonApiDotNetCore.Annotations/CollectionConverter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public IReadOnlyCollection<IIdentifiable> ExtractResources(object? value)
7474
HashSet<IIdentifiable> resourceSet => resourceSet,
7575
IReadOnlyCollection<IIdentifiable> resourceCollection => resourceCollection,
7676
IEnumerable<IIdentifiable> resources => resources.ToList(),
77-
IIdentifiable resource => resource.AsArray(),
77+
IIdentifiable resource => [resource],
7878
_ => Array.Empty<IIdentifiable>()
7979
};
8080
}

src/JsonApiDotNetCore.Annotations/ObjectExtensions.cs

-15
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,6 @@ namespace JsonApiDotNetCore;
44

55
internal static class ObjectExtensions
66
{
7-
public static IEnumerable<T> AsEnumerable<T>(this T element)
8-
{
9-
yield return element;
10-
}
11-
12-
public static T[] AsArray<T>(this T element)
13-
{
14-
return [element];
15-
}
16-
17-
public static List<T> AsList<T>(this T element)
18-
{
19-
return [element];
20-
}
21-
227
public static HashSet<T> AsHashSet<T>(this T element)
238
{
249
return [element];

src/JsonApiDotNetCore/Configuration/ServiceCollectionExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static IServiceCollection AddJsonApi<TDbContext>(this IServiceCollection
3636
Action<ServiceDiscoveryFacade>? discovery = null, Action<ResourceGraphBuilder>? resources = null, IMvcCoreBuilder? mvcBuilder = null)
3737
where TDbContext : DbContext
3838
{
39-
return AddJsonApi(services, options, discovery, resources, mvcBuilder, typeof(TDbContext).AsArray());
39+
return AddJsonApi(services, options, discovery, resources, mvcBuilder, [typeof(TDbContext)]);
4040
}
4141

4242
private static void SetupApplicationBuilder(IServiceCollection services, Action<JsonApiOptions>? configureOptions,

src/JsonApiDotNetCore/Errors/JsonApiException.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public JsonApiException(ErrorObject error, Exception? innerException = null)
2626
{
2727
ArgumentGuard.NotNull(error);
2828

29-
Errors = error.AsArray();
29+
Errors = [error];
3030
}
3131

3232
public JsonApiException(IEnumerable<ErrorObject> errors, Exception? innerException = null)

src/JsonApiDotNetCore/Middleware/ExceptionHandler.cs

+15-9
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,21 @@ protected virtual IReadOnlyList<ErrorObject> CreateErrorResponse(Exception excep
7474
IReadOnlyList<ErrorObject> errors = exception switch
7575
{
7676
JsonApiException jsonApiException => jsonApiException.Errors,
77-
OperationCanceledException => new ErrorObject((HttpStatusCode)499)
78-
{
79-
Title = "Request execution was canceled."
80-
}.AsArray(),
81-
_ => new ErrorObject(HttpStatusCode.InternalServerError)
82-
{
83-
Title = "An unhandled error occurred while processing this request.",
84-
Detail = exception.Message
85-
}.AsArray()
77+
OperationCanceledException =>
78+
[
79+
new ErrorObject((HttpStatusCode)499)
80+
{
81+
Title = "Request execution was canceled."
82+
}
83+
],
84+
_ =>
85+
[
86+
new ErrorObject(HttpStatusCode.InternalServerError)
87+
{
88+
Title = "An unhandled error occurred while processing this request.",
89+
Detail = exception.Message
90+
}
91+
]
8692
};
8793

8894
if (_options.IncludeExceptionStackTraceInErrors && exception is not InvalidModelStateException)

src/JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ private static async Task FlushResponseAsync(HttpResponse httpResponse, JsonSeri
205205

206206
var errorDocument = new Document
207207
{
208-
Errors = error.AsList()
208+
Errors = [error]
209209
};
210210

211211
await JsonSerializer.SerializeAsync(httpResponse.Body, errorDocument, serializerOptions);

src/JsonApiDotNetCore/Queries/Parsing/IncludeParser.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private void ParseRelationshipChain(string source, IncludeTreeNode treeRoot)
8686
// that there's currently no way to include Products without Articles. We could add such optional upcast syntax
8787
// in the future, if desired.
8888

89-
ICollection<IncludeTreeNode> children = ParseRelationshipName(source, treeRoot.AsList());
89+
ICollection<IncludeTreeNode> children = ParseRelationshipName(source, [treeRoot]);
9090

9191
while (TokenStack.TryPeek(out Token? nextToken) && nextToken.Kind == TokenKind.Period)
9292
{

src/JsonApiDotNetCore/Queries/QueryLayerComposer.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public QueryLayer ComposeForGetById<TId>(TId id, ResourceType primaryResourceTyp
272272
QueryLayer queryLayer = ComposeFromConstraints(primaryResourceType);
273273
queryLayer.Sort = null;
274274
queryLayer.Pagination = null;
275-
queryLayer.Filter = CreateFilterByIds(id.AsArray(), idAttribute, queryLayer.Filter);
275+
queryLayer.Filter = CreateFilterByIds([id], idAttribute, queryLayer.Filter);
276276

277277
if (fieldSelection == TopFieldSelection.OnlyIdAttribute)
278278
{
@@ -342,7 +342,7 @@ public QueryLayer WrapLayerForSecondaryEndpoint<TId>(QueryLayer secondaryLayer,
342342
return new QueryLayer(primaryResourceType)
343343
{
344344
Include = RewriteIncludeForSecondaryEndpoint(innerInclude, relationship),
345-
Filter = CreateFilterByIds(primaryId.AsArray(), primaryIdAttribute, primaryFilter),
345+
Filter = CreateFilterByIds([primaryId], primaryIdAttribute, primaryFilter),
346346
Selection = primarySelection
347347
};
348348
}
@@ -390,7 +390,7 @@ public QueryLayer ComposeForUpdate<TId>(TId id, ResourceType primaryResourceType
390390
primaryLayer.Include = includeElements.Any() ? new IncludeExpression(includeElements) : IncludeExpression.Empty;
391391
primaryLayer.Sort = null;
392392
primaryLayer.Pagination = null;
393-
primaryLayer.Filter = CreateFilterByIds(id.AsArray(), primaryIdAttribute, primaryLayer.Filter);
393+
primaryLayer.Filter = CreateFilterByIds([id], primaryIdAttribute, primaryLayer.Filter);
394394
primaryLayer.Selection = null;
395395

396396
return primaryLayer;
@@ -449,7 +449,7 @@ public QueryLayer ComposeForHasMany<TId>(HasManyAttribute hasManyRelationship, T
449449
AttrAttribute rightIdAttribute = GetIdAttribute(hasManyRelationship.RightType);
450450
HashSet<object> rightTypedIds = rightResourceIds.Select(resource => resource.GetTypedId()).ToHashSet();
451451

452-
FilterExpression? leftFilter = CreateFilterByIds(leftId.AsArray(), leftIdAttribute, null);
452+
FilterExpression? leftFilter = CreateFilterByIds([leftId], leftIdAttribute, null);
453453
FilterExpression? rightFilter = CreateFilterByIds(rightTypedIds, rightIdAttribute, null);
454454

455455
var secondarySelection = new FieldSelection();

src/JsonApiDotNetCore/Queries/QueryableBuilding/IncludeClauseBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,6 @@ private static Expression IncludeExtensionMethodCall(Expression source, Type ent
7575
{
7676
Expression navigationExpression = Expression.Constant(navigationPropertyPath);
7777

78-
return Expression.Call(typeof(EntityFrameworkQueryableExtensions), "Include", entityType.AsArray(), source, navigationExpression);
78+
return Expression.Call(typeof(EntityFrameworkQueryableExtensions), "Include", [entityType], source, navigationExpression);
7979
}
8080
}

src/JsonApiDotNetCore/Queries/QueryableBuilding/SelectClauseBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ private static Expression TestForNull(Expression expressionToTest, Expression if
242242

243243
private static Expression CopyCollectionExtensionMethodCall(Expression source, string operationName, Type elementType)
244244
{
245-
return Expression.Call(typeof(Enumerable), operationName, elementType.AsArray(), source);
245+
return Expression.Call(typeof(Enumerable), operationName, [elementType], source);
246246
}
247247

248248
private static Expression SelectExtensionMethodCall(Type extensionType, Expression source, Type elementType, Expression selectBody)

src/JsonApiDotNetCore/Queries/QueryableBuilding/SkipTakeClauseBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ private static Expression ExtensionMethodCall(Expression source, string operatio
3838
{
3939
Expression constant = value.CreateTupleAccessExpressionForConstant(typeof(int));
4040

41-
return Expression.Call(context.ExtensionType, operationName, context.LambdaScope.Parameter.Type.AsArray(), source, constant);
41+
return Expression.Call(context.ExtensionType, operationName, [context.LambdaScope.Parameter.Type], source, constant);
4242
}
4343
}

src/JsonApiDotNetCore/Queries/QueryableBuilding/WhereClauseBuilder.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private LambdaExpression GetPredicateLambda(FilterExpression filter, QueryClause
3333

3434
private static Expression WhereExtensionMethodCall(LambdaExpression predicate, QueryClauseBuilderContext context)
3535
{
36-
return Expression.Call(context.ExtensionType, "Where", context.LambdaScope.Parameter.Type.AsArray(), context.Source, predicate);
36+
return Expression.Call(context.ExtensionType, "Where", [context.LambdaScope.Parameter.Type], context.Source, predicate);
3737
}
3838

3939
public override Expression VisitHas(HasExpression expression, QueryClauseBuilderContext context)
@@ -67,8 +67,8 @@ public override Expression VisitHas(HasExpression expression, QueryClauseBuilder
6767
private static MethodCallExpression AnyExtensionMethodCall(Type elementType, Expression source, Expression? predicate)
6868
{
6969
return predicate != null
70-
? Expression.Call(typeof(Enumerable), "Any", elementType.AsArray(), source, predicate)
71-
: Expression.Call(typeof(Enumerable), "Any", elementType.AsArray(), source);
70+
? Expression.Call(typeof(Enumerable), "Any", [elementType], source, predicate)
71+
: Expression.Call(typeof(Enumerable), "Any", [elementType], source);
7272
}
7373

7474
public override Expression VisitIsType(IsTypeExpression expression, QueryClauseBuilderContext context)
@@ -125,7 +125,7 @@ public override Expression VisitAny(AnyExpression expression, QueryClauseBuilder
125125

126126
private static Expression ContainsExtensionMethodCall(Expression collection, Expression value)
127127
{
128-
return Expression.Call(typeof(Enumerable), "Contains", value.Type.AsArray(), collection, value);
128+
return Expression.Call(typeof(Enumerable), "Contains", [value.Type], collection, value);
129129
}
130130

131131
public override Expression VisitLogical(LogicalExpression expression, QueryClauseBuilderContext context)

src/JsonApiDotNetCore/QueryStrings/IncludeQueryStringParameterReader.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,6 @@ public virtual IReadOnlyCollection<ExpressionInScope> GetConstraints()
7272
? new ExpressionInScope(null, _includeExpression)
7373
: new ExpressionInScope(null, IncludeExpression.Empty);
7474

75-
return expressionInScope.AsArray();
75+
return [expressionInScope];
7676
}
7777
}

src/JsonApiDotNetCore/QueryStrings/SparseFieldSetQueryStringParameterReader.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private SparseFieldSetExpression GetSparseFieldSet(string parameterValue, Resour
100100
public virtual IReadOnlyCollection<ExpressionInScope> GetConstraints()
101101
{
102102
return _sparseFieldTableBuilder.Any()
103-
? new ExpressionInScope(null, new SparseFieldTableExpression(_sparseFieldTableBuilder.ToImmutable())).AsArray()
103+
? [new ExpressionInScope(null, new SparseFieldTableExpression(_sparseFieldTableBuilder.ToImmutable()))]
104104
: Array.Empty<ExpressionInScope>();
105105
}
106106
}

src/JsonApiDotNetCore/Serialization/Response/ResponseModelAdapter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public Document Convert(object? model)
107107
}
108108
else if (model is ErrorObject errorObject)
109109
{
110-
document.Errors = errorObject.AsArray();
110+
document.Errors = [errorObject];
111111
}
112112
else
113113
{

test/JsonApiDotNetCoreTests/IntegrationTests/CompositeKeys/CarExpressionRewriter.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Collections.Immutable;
22
using System.Reflection;
3-
using JsonApiDotNetCore;
43
using JsonApiDotNetCore.Configuration;
54
using JsonApiDotNetCore.Queries.Expressions;
65
using JsonApiDotNetCore.Resources;
@@ -42,7 +41,7 @@ public CarExpressionRewriter(IResourceGraph resourceGraph)
4241
}
4342

4443
string carStringId = (string)rightConstant.TypedValue;
45-
return RewriteFilterOnCarStringIds(leftChain, carStringId.AsEnumerable());
44+
return RewriteFilterOnCarStringIds(leftChain, [carStringId]);
4645
}
4746
}
4847

test/JsonApiDotNetCoreTests/IntegrationTests/QueryStrings/CustomFunctions/Sum/SumWhereClauseBuilder.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Linq.Expressions;
2-
using JsonApiDotNetCore;
32
using JsonApiDotNetCore.Configuration;
43
using JsonApiDotNetCore.Queries.Expressions;
54
using JsonApiDotNetCore.Queries.QueryableBuilding;
@@ -42,6 +41,6 @@ private LambdaExpression GetSelectorLambda(QueryExpression expression, QueryClau
4241

4342
private static Expression SumExtensionMethodCall(LambdaExpression selector, QueryClauseBuilderContext context)
4443
{
45-
return Expression.Call(context.ExtensionType, "Sum", context.LambdaScope.Parameter.Type.AsArray(), context.Source, selector);
44+
return Expression.Call(context.ExtensionType, "Sum", [context.LambdaScope.Parameter.Type], context.Source, selector);
4645
}
4746
}

test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Updating/Resources/ReplaceToManyRelationshipTests.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Net;
22
using FluentAssertions;
3-
using JsonApiDotNetCore;
43
using JsonApiDotNetCore.Configuration;
54
using JsonApiDotNetCore.Serialization.Objects;
65
using Microsoft.EntityFrameworkCore;
@@ -916,7 +915,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
916915
dbContext.WorkItems.Add(existingWorkItem);
917916
await dbContext.SaveChangesAsync();
918917

919-
existingWorkItem.Children = existingWorkItem.AsList();
918+
existingWorkItem.Children = [existingWorkItem];
920919
await dbContext.SaveChangesAsync();
921920
});
922921

test/JsonApiDotNetCoreTests/UnitTests/FieldChains/FieldChainPatternInheritanceMatchTests.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using FluentAssertions;
22
using JetBrains.Annotations;
3-
using JsonApiDotNetCore;
43
using JsonApiDotNetCore.Configuration;
54
using JsonApiDotNetCore.QueryStrings.FieldChains;
65
using JsonApiDotNetCore.Resources;
@@ -47,7 +46,7 @@ public sealed class FieldChainPatternInheritanceMatchTests
4746
public FieldChainPatternInheritanceMatchTests(ITestOutputHelper testOutputHelper)
4847
{
4948
var loggerProvider = new XUnitLoggerProvider(testOutputHelper, null, LogOutputFields.Message);
50-
_loggerFactory = new LoggerFactory(loggerProvider.AsEnumerable());
49+
_loggerFactory = new LoggerFactory([loggerProvider]);
5150

5251
var options = new JsonApiOptions();
5352
_resourceGraph = new ResourceGraphBuilder(options, NullLoggerFactory.Instance).Add<Base, long>().Add<DerivedQ, long>().Add<DerivedV, long>().Build();

test/JsonApiDotNetCoreTests/UnitTests/FieldChains/FieldChainPatternMatchTests.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using FluentAssertions;
22
using JetBrains.Annotations;
3-
using JsonApiDotNetCore;
43
using JsonApiDotNetCore.Configuration;
54
using JsonApiDotNetCore.QueryStrings.FieldChains;
65
using JsonApiDotNetCore.Resources;
@@ -33,7 +32,7 @@ public sealed class FieldChainPatternMatchTests
3332
public FieldChainPatternMatchTests(ITestOutputHelper testOutputHelper)
3433
{
3534
var loggerProvider = new XUnitLoggerProvider(testOutputHelper, null, LogOutputFields.Message);
36-
_loggerFactory = new LoggerFactory(loggerProvider.AsEnumerable());
35+
_loggerFactory = new LoggerFactory([loggerProvider]);
3736

3837
var options = new JsonApiOptions();
3938
IResourceGraph resourceGraph = new ResourceGraphBuilder(options, NullLoggerFactory.Instance).Add<Resource, long>().Build();

test/TestBuildingBlocks/TestableDbContext.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Diagnostics;
2-
using JsonApiDotNetCore;
32
using Microsoft.EntityFrameworkCore;
43
using Microsoft.EntityFrameworkCore.Metadata;
54
using Microsoft.Extensions.Logging;
@@ -21,7 +20,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder builder)
2120
[Conditional("DEBUG")]
2221
private static void WriteSqlStatementsToOutputWindow(DbContextOptionsBuilder builder)
2322
{
24-
builder.LogTo(message => Debug.WriteLine(message), DbLoggerCategory.Database.Name.AsArray(), LogLevel.Information);
23+
builder.LogTo(message => Debug.WriteLine(message), [DbLoggerCategory.Database.Name], LogLevel.Information);
2524
}
2625

2726
protected override void OnModelCreating(ModelBuilder builder)

0 commit comments

Comments
 (0)