Skip to content

Commit

Permalink
Merge code tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnmbond committed Mar 27, 2024
2 parents 0ad613c + f156caf commit 4994d51
Show file tree
Hide file tree
Showing 137 changed files with 531 additions and 938 deletions.
2 changes: 1 addition & 1 deletion src/Simple.OData.Benchmarks.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public static ODataClient GetClient(string metadataFilename, string responseFile
{
MetadataDocument = MetadataResolver.GetMetadataDocument(metadataFilename),
IgnoreUnmappedProperties = true
}.WithHttpResponses(new[] { @"..\..\..\..\..\..\..\Resources\" + responseFilename }));
}.WithHttpResponses([@"..\..\..\..\..\..\..\Resources\" + responseFilename]));
}
}
18 changes: 5 additions & 13 deletions src/Simple.OData.Client.Core/Adapter/BatchWriterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,12 @@

namespace Simple.OData.Client;

public abstract class BatchWriterBase : IBatchWriter
public abstract class BatchWriterBase(ISession session, IDictionary<object, IDictionary<string, object>> batchEntries) : IBatchWriter
{
protected readonly ISession _session;
private readonly Dictionary<IDictionary<string, object>, string> _contentIdMap;
protected readonly ISession _session = session;
private readonly Dictionary<IDictionary<string, object>, string> _contentIdMap = [];
protected bool _pendingChangeSet;

protected BatchWriterBase(ISession session, IDictionary<object, IDictionary<string, object>> batchEntries)
{
_session = session;
LastOperationId = 0;
_contentIdMap = new Dictionary<IDictionary<string, object>, string>();
BatchEntries = batchEntries;
}

public abstract Task StartBatchAsync();
public abstract Task<HttpRequestMessage> EndBatchAsync();

Expand Down Expand Up @@ -62,7 +54,7 @@ await action(client)
protected abstract Task EndChangesetAsync();
protected abstract Task<object> CreateOperationMessageAsync(Uri uri, string method, string collection, string contentId, bool resultRequired);

public int LastOperationId { get; private set; }
public int LastOperationId { get; private set; } = 0;

public string NextContentId()
{
Expand Down Expand Up @@ -90,7 +82,7 @@ public void MapContentId(IDictionary<string, object> entryData, string contentId
}
}

public IDictionary<object, IDictionary<string, object>> BatchEntries { get; private set; }
public IDictionary<object, IDictionary<string, object>> BatchEntries { get; private set; } = batchEntries;

public async Task<object> CreateOperationMessageAsync(Uri uri, string method, string collection, IDictionary<string, object> entryData, bool resultRequired)
{
Expand Down
11 changes: 3 additions & 8 deletions src/Simple.OData.Client.Core/Adapter/CommandFormatterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

namespace Simple.OData.Client;

public abstract class CommandFormatterBase : ICommandFormatter
public abstract class CommandFormatterBase(ISession session) : ICommandFormatter
{
protected readonly ISession _session;

protected CommandFormatterBase(ISession session)
{
_session = session;
}
protected readonly ISession _session = session;

public abstract string ConvertValueToUriLiteral(object value, bool escapeString);

Expand Down Expand Up @@ -356,7 +351,7 @@ protected string FormatOrderByItem(KeyValuePair<string, bool> pathWithOrder, Ent
new KeyValuePair<ODataExpandAssociation, ODataExpandOptions>(x,
ODataExpandOptions.ByValue())))
.Select(x => new KeyValuePair<string, ODataExpandOptions>(a.Key.Name + "/" + x.Key, x.Value))
: new[] { new KeyValuePair<string, ODataExpandOptions>(a.Key.Name, a.Value) })
: [new KeyValuePair<string, ODataExpandOptions>(a.Key.Name, a.Value)])
.ToList();
}
}
19 changes: 6 additions & 13 deletions src/Simple.OData.Client.Core/Adapter/MetadataBase.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
namespace Simple.OData.Client;

public abstract class MetadataBase : IMetadata
public abstract class MetadataBase(INameMatchResolver nameMatchResolver, bool ignoreUnmappedProperties, bool unqualifiedNameCall) : IMetadata
{
protected MetadataBase(INameMatchResolver nameMatchResolver, bool ignoreUnmappedProperties, bool unqualifiedNameCall)
{
IgnoreUnmappedProperties = ignoreUnmappedProperties;
NameMatchResolver = nameMatchResolver;
UnqualifiedNameCall = unqualifiedNameCall;
}

public bool IgnoreUnmappedProperties { get; }
public bool IgnoreUnmappedProperties { get; } = ignoreUnmappedProperties;

public INameMatchResolver NameMatchResolver { get; }
public INameMatchResolver NameMatchResolver { get; } = nameMatchResolver;

public bool UnqualifiedNameCall { get; }
public bool UnqualifiedNameCall { get; } = unqualifiedNameCall;

public abstract string GetEntityCollectionExactName(string collectionName);

Expand Down Expand Up @@ -123,7 +116,7 @@ private EntityCollection NavigateToCollection(EntityCollection rootCollection, I

protected static bool SegmentsIncludeTypeSpecification(IEnumerable<string> segments)
{
return segments.Last().Contains(".");
return segments.Last().Contains('.');
}

protected static bool IsSingleSegmentWithTypeSpecification(IEnumerable<string> segments)
Expand Down Expand Up @@ -183,6 +176,6 @@ protected static bool IsSingleSegmentWithTypeSpecification(IEnumerable<string> s

public static IEnumerable<string> GetCollectionPathSegments(string path)
{
return path.Split('/').Select(x => x.Contains("(") ? x.Substring(0, x.IndexOf("(", StringComparison.Ordinal)) : x);
return path.Split('/').Select(x => x.Contains('(') ? x.Substring(0, x.IndexOf("(", StringComparison.Ordinal)) : x);
}
}
87 changes: 28 additions & 59 deletions src/Simple.OData.Client.Core/Adapter/MetadataCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,35 @@ namespace Simple.OData.Client.Adapter;
/// <summary>
/// A caching layer for <see cref="IMetadata"/>
/// </summary>
public class MetadataCache : IMetadata
public class MetadataCache(IMetadata metadata) : IMetadata
{
private readonly IMetadata metadata;
private readonly ConcurrentDictionary<string, EntityCollection> ec;
private readonly ConcurrentDictionary<string, EntityCollection> nav;
private readonly ConcurrentDictionary<string, string> ecen;
private readonly ConcurrentDictionary<string, bool> roc;
private readonly ConcurrentDictionary<string, string> eten;
private readonly ConcurrentDictionary<string, string> qtn;
private readonly ConcurrentDictionary<string, bool> ot;
private readonly ConcurrentDictionary<string, bool> tid;
private readonly ConcurrentDictionary<string, bool> sp;
private readonly ConcurrentDictionary<string, bool> np;
private readonly ConcurrentDictionary<string, IList<string>> npn;
private readonly ConcurrentDictionary<string, bool> npc;
private readonly ConcurrentDictionary<string, IList<string>> spns;
private readonly ConcurrentDictionary<string, IList<string>> dkpns;
private readonly ConcurrentDictionary<string, IList<IList<string>>> akpns;
private readonly ConcurrentDictionary<string, string> npen;
private readonly ConcurrentDictionary<string, string> spp;
private readonly ConcurrentDictionary<string, string> ffn;
private readonly ConcurrentDictionary<string, string> fv;
private readonly ConcurrentDictionary<string, string> afn;
private readonly ConcurrentDictionary<string, string> nppt;
private readonly ConcurrentDictionary<string, EntityCollection> arc;
private readonly ConcurrentDictionary<string, EntityCollection> frc;
private readonly ConcurrentDictionary<string, string> spen;

public MetadataCache(IMetadata metadata)
{
this.metadata = metadata;
IgnoreUnmappedProperties = (metadata as MetadataBase).IgnoreUnmappedProperties;

ec = new ConcurrentDictionary<string, EntityCollection>();
nav = new ConcurrentDictionary<string, EntityCollection>();
ecen = new ConcurrentDictionary<string, string>();
roc = new ConcurrentDictionary<string, bool>();
eten = new ConcurrentDictionary<string, string>();
qtn = new ConcurrentDictionary<string, string>();
ot = new ConcurrentDictionary<string, bool>();
tid = new ConcurrentDictionary<string, bool>();
sp = new ConcurrentDictionary<string, bool>();
np = new ConcurrentDictionary<string, bool>();
npn = new ConcurrentDictionary<string, IList<string>>();
npc = new ConcurrentDictionary<string, bool>();
spns = new ConcurrentDictionary<string, IList<string>>();
dkpns = new ConcurrentDictionary<string, IList<string>>();
akpns = new ConcurrentDictionary<string, IList<IList<string>>>();
spp = new ConcurrentDictionary<string, string>();
npen = new ConcurrentDictionary<string, string>();
ffn = new ConcurrentDictionary<string, string>();
fv = new ConcurrentDictionary<string, string>();
afn = new ConcurrentDictionary<string, string>();
nppt = new ConcurrentDictionary<string, string>();
arc = new ConcurrentDictionary<string, EntityCollection>();
frc = new ConcurrentDictionary<string, EntityCollection>();
spen = new ConcurrentDictionary<string, string>();
}

public bool IgnoreUnmappedProperties { get; }
private readonly IMetadata metadata = metadata;
private readonly ConcurrentDictionary<string, EntityCollection> ec = new ConcurrentDictionary<string, EntityCollection>();
private readonly ConcurrentDictionary<string, EntityCollection> nav = new ConcurrentDictionary<string, EntityCollection>();
private readonly ConcurrentDictionary<string, string> ecen = new ConcurrentDictionary<string, string>();
private readonly ConcurrentDictionary<string, bool> roc = new ConcurrentDictionary<string, bool>();
private readonly ConcurrentDictionary<string, string> eten = new ConcurrentDictionary<string, string>();
private readonly ConcurrentDictionary<string, string> qtn = new ConcurrentDictionary<string, string>();
private readonly ConcurrentDictionary<string, bool> ot = new ConcurrentDictionary<string, bool>();
private readonly ConcurrentDictionary<string, bool> tid = new ConcurrentDictionary<string, bool>();
private readonly ConcurrentDictionary<string, bool> sp = new ConcurrentDictionary<string, bool>();
private readonly ConcurrentDictionary<string, bool> np = new ConcurrentDictionary<string, bool>();
private readonly ConcurrentDictionary<string, IList<string>> npn = new ConcurrentDictionary<string, IList<string>>();
private readonly ConcurrentDictionary<string, bool> npc = new ConcurrentDictionary<string, bool>();
private readonly ConcurrentDictionary<string, IList<string>> spns = new ConcurrentDictionary<string, IList<string>>();
private readonly ConcurrentDictionary<string, IList<string>> dkpns = new ConcurrentDictionary<string, IList<string>>();
private readonly ConcurrentDictionary<string, IList<IList<string>>> akpns = new ConcurrentDictionary<string, IList<IList<string>>>();
private readonly ConcurrentDictionary<string, string> npen = new ConcurrentDictionary<string, string>();
private readonly ConcurrentDictionary<string, string> spp = new ConcurrentDictionary<string, string>();
private readonly ConcurrentDictionary<string, string> ffn = new ConcurrentDictionary<string, string>();
private readonly ConcurrentDictionary<string, string> fv = new ConcurrentDictionary<string, string>();
private readonly ConcurrentDictionary<string, string> afn = new ConcurrentDictionary<string, string>();
private readonly ConcurrentDictionary<string, string> nppt = new ConcurrentDictionary<string, string>();
private readonly ConcurrentDictionary<string, EntityCollection> arc = new ConcurrentDictionary<string, EntityCollection>();
private readonly ConcurrentDictionary<string, EntityCollection> frc = new ConcurrentDictionary<string, EntityCollection>();
private readonly ConcurrentDictionary<string, string> spen = new ConcurrentDictionary<string, string>();

public bool IgnoreUnmappedProperties { get; } = (metadata as MetadataBase).IgnoreUnmappedProperties;

public EntityCollection GetEntityCollection(string collectionPath)
{
Expand Down
16 changes: 3 additions & 13 deletions src/Simple.OData.Client.Core/Adapter/ODataAdapterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ public async virtual Task<IODataModelAdapter> CreateModelAdapterAsync(HttpRespon
public virtual IODataModelAdapter CreateModelAdapter(string metadataString, ITypeCache typeCache)
{
var protocolVersion = GetMetadataProtocolVersion(metadataString);
var loadModelAdapter = GetModelAdapterLoader(protocolVersion, metadataString, typeCache);
if (loadModelAdapter is null)
{
throw new NotSupportedException($"OData protocol {protocolVersion} is not supported");
}

var loadModelAdapter = GetModelAdapterLoader(protocolVersion, metadataString, typeCache) ?? throw new NotSupportedException($"OData protocol {protocolVersion} is not supported");
return loadModelAdapter();
}

Expand All @@ -54,12 +49,7 @@ public virtual IODataModelAdapter CreateModelAdapter(string metadataString, ITyp
{
var modelAdapter = CreateModelAdapter(metadataString, typeCache);

var loadAdapter = GetAdapterLoader(modelAdapter, typeCache);
if (loadAdapter is null)
{
throw new NotSupportedException($"OData protocol {modelAdapter.ProtocolVersion} is not supported");
}

var loadAdapter = GetAdapterLoader(modelAdapter, typeCache) ?? throw new NotSupportedException($"OData protocol {modelAdapter.ProtocolVersion} is not supported");
return loadAdapter;
}

Expand All @@ -85,7 +75,7 @@ protected static async Task<IEnumerable<string>> GetSupportedProtocolVersionsAsy
.ReadAsStringAsync()
.ConfigureAwait(false);
var protocolVersion = GetMetadataProtocolVersion(metadataString);
return new[] { protocolVersion };
return [protocolVersion];
}
catch (Exception)
{
Expand Down
14 changes: 4 additions & 10 deletions src/Simple.OData.Client.Core/Adapter/RequestWriterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@

namespace Simple.OData.Client;

public abstract class RequestWriterBase : IRequestWriter
public abstract class RequestWriterBase(ISession session, Lazy<IBatchWriter> deferredBatchWriter) : IRequestWriter
{
protected readonly ISession _session;
protected readonly Lazy<IBatchWriter> _deferredBatchWriter;

protected RequestWriterBase(ISession session, Lazy<IBatchWriter> deferredBatchWriter)
{
_session = session;
_deferredBatchWriter = deferredBatchWriter;
}
protected readonly ISession _session = session;
protected readonly Lazy<IBatchWriter> _deferredBatchWriter = deferredBatchWriter;

protected bool IsBatch => _deferredBatchWriter is not null;

Expand Down Expand Up @@ -63,7 +57,7 @@ protected RequestWriterBase(ISession session, Lazy<IBatchWriter> deferredBatchWr
IDictionary<string, string>? headers = null)
{
var segments = commandText.Split('/');
if (segments.Length > 1 && segments.Last().Contains("."))
if (segments.Length > 1 && segments.Last().Contains('.'))
{
commandText = commandText.Substring(0, commandText.Length - segments.Last().Length - 1);
}
Expand Down
9 changes: 2 additions & 7 deletions src/Simple.OData.Client.Core/Adapter/ResponseReaderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

namespace Simple.OData.Client;

public abstract class ResponseReaderBase : IResponseReader
public abstract class ResponseReaderBase(ISession session) : IResponseReader
{
protected readonly ISession _session;

protected ResponseReaderBase(ISession session)
{
_session = session;
}
protected readonly ISession _session = session;

protected ITypeCache TypeCache => _session.TypeCache;

Expand Down
2 changes: 1 addition & 1 deletion src/Simple.OData.Client.Core/Cache/TypeCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public bool TryConvert(object? value, Type targetType, out object? result)
}
else if ((targetType == typeof(DateTime) || targetType == typeof(DateTime?)) && ImplicitConversionTo<DateTime>(value) is MethodInfo implicitMethod)
{
result = (DateTime)implicitMethod.Invoke(value, new object[] { value });
result = (DateTime)implicitMethod.Invoke(value, [value]);
}
else if ((targetType == typeof(DateTimeOffset) || targetType == typeof(DateTimeOffset?)) && value is DateTime time)
{
Expand Down
15 changes: 4 additions & 11 deletions src/Simple.OData.Client.Core/CachedPluralizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@

namespace Simple.OData.Client;

public class CachedPluralizer : IPluralizer
public class CachedPluralizer(IPluralizer pluralizer) : IPluralizer
{
private readonly IPluralizer pluralizer;
private readonly ConcurrentDictionary<string, string> singles;
private readonly ConcurrentDictionary<string, string> plurals;

public CachedPluralizer(IPluralizer pluralizer)
{
this.pluralizer = pluralizer;
singles = new ConcurrentDictionary<string, string>();
plurals = new ConcurrentDictionary<string, string>();
}
private readonly IPluralizer pluralizer = pluralizer;
private readonly ConcurrentDictionary<string, string> singles = new ConcurrentDictionary<string, string>();
private readonly ConcurrentDictionary<string, string> plurals = new ConcurrentDictionary<string, string>();

public string Pluralize(string word)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public override DynamicMetaObject BindGetMember(GetMemberBinder binder)
if (FunctionMapping.ContainsFunction(binder.Name, 0))
{
ctor = CtorWithExpressionAndString;
ctorArguments = new Expression[] { Expression.Convert(Expression, LimitType), Expression.Constant(binder.Name) };
ctorArguments = [Expression.Convert(Expression, LimitType), Expression.Constant(binder.Name)];
}
else
{
Expand All @@ -76,7 +76,7 @@ public override DynamicMetaObject BindGetMember(GetMemberBinder binder)
: binder.Name;
var referenceExpression = Expression.Invoke(calculateReference, Expression.Constant(HasValue), Expression.Convert(Expression, LimitType));
ctor = CtorWithString;
ctorArguments = new Expression[] { referenceExpression };
ctorArguments = [referenceExpression];
}

return new DynamicMetaObject(
Expand All @@ -98,7 +98,7 @@ public override DynamicMetaObject BindSetMember(SetMemberBinder binder, DynamicM
public override DynamicMetaObject BindInvokeMember(
InvokeMemberBinder binder, DynamicMetaObject[] args)
{
var expressionFunctionConstructor = typeof(ExpressionFunction).GetConstructor(new[] { typeof(string), typeof(IEnumerable<object>) });
var expressionFunctionConstructor = typeof(ExpressionFunction).GetConstructor([typeof(string), typeof(IEnumerable<object>)]);
if (FunctionMapping.ContainsFunction(binder.Name, args.Length))
{
var expression = Expression.New(CtorWithExpressionAndExpressionFunction,
Expand Down Expand Up @@ -160,7 +160,7 @@ public override DynamicMetaObject BindBinaryOperation(BinaryOperationBinder bind
{
if (arg.RuntimeType is not null && arg.RuntimeType.IsEnumType())
{
var ctor = typeof(ODataExpression).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.NonPublic, null, new[] { typeof(object) }, null);
var ctor = typeof(ODataExpression).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.NonPublic, null, [typeof(object)], null);
var expression = Expression.New(CtorWithExpressionAndExpressionAndOperator,
Expression.Convert(Expression, LimitType),
Expression.New(ctor, Expression.Convert(arg.Expression, typeof(object))),
Expand Down
2 changes: 1 addition & 1 deletion src/Simple.OData.Client.Core/EntryDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void AddProperty(string propertyName, object propertyValue)
{
if (!Links.TryGetValue(linkName, out var links))
{
links = new List<ReferenceLink>();
links = [];
Links.Add(linkName, links);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ private static IEnumerable<string> ExtractColumnNames(MemberExpression memberExp

return memberExpression.Expression is MemberExpression
? memberExpression.Expression.ExtractColumnNames(typeCache).Select(x => string.Join("/", x, memberName))
: new[] { memberName };
: [memberName];
}
}
11 changes: 3 additions & 8 deletions src/Simple.OData.Client.Core/Expressions/ExpandExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,11 @@ internal static class ExpandExpression
}
}

internal class ExpandExpressionVisitor : ExpressionVisitor
internal class ExpandExpressionVisitor(ITypeCache typeCache) : ExpressionVisitor
{
private readonly ITypeCache _typeCache;
private readonly ITypeCache _typeCache = typeCache;

public ExpandExpressionVisitor(ITypeCache typeCache)
{
_typeCache = typeCache;
}

public List<ODataExpandAssociation> ExpandAssociations { get; } = new List<ODataExpandAssociation>();
public List<ODataExpandAssociation> ExpandAssociations { get; } = [];

protected override Expression VisitMember(MemberExpression node)
{
Expand Down

0 comments on commit 4994d51

Please sign in to comment.