Skip to content

Commit

Permalink
Minor code cleanup (#87)
Browse files Browse the repository at this point in the history
This removes a few un-used using statements.
It also leverages the CallerArgumentExpression for argument exceptions
  • Loading branch information
Keboo authored Aug 30, 2024
1 parent a0b8141 commit 4892839
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.Datasync.Client.Service;

namespace CommunityToolkit.Datasync.Client;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal DatasyncOfflineOptionsBuilder(IEnumerable<Type> entityTypes)
/// <returns>The current builder for chaining.</returns>
public DatasyncOfflineOptionsBuilder UseHttpClientFactory(IHttpClientFactory httpClientFactory)
{
ArgumentNullException.ThrowIfNull(httpClientFactory, nameof(httpClientFactory));
ArgumentNullException.ThrowIfNull(httpClientFactory);
this._httpClientFactory = httpClientFactory;
return this;
}
Expand All @@ -46,7 +46,7 @@ public DatasyncOfflineOptionsBuilder UseHttpClientFactory(IHttpClientFactory htt
/// <returns>The current builder for chaining.</returns>
public DatasyncOfflineOptionsBuilder UseHttpClient(HttpClient httpClient)
{
ArgumentNullException.ThrowIfNull(httpClient, nameof(httpClient));
ArgumentNullException.ThrowIfNull(httpClient);
this._httpClientFactory = new BasicHttpClientFactory(httpClient);
return this;
}
Expand All @@ -59,7 +59,7 @@ public DatasyncOfflineOptionsBuilder UseHttpClient(HttpClient httpClient)
/// <returns>The current builder for chaining.</returns>
public DatasyncOfflineOptionsBuilder UseEndpoint(Uri endpoint)
{
ArgumentNullException.ThrowIfNull(endpoint, nameof(endpoint));
ArgumentNullException.ThrowIfNull(endpoint);
ThrowIf.IsNotValidEndpoint(endpoint, nameof(endpoint));
this._httpClientFactory = new HttpClientFactory(new HttpClientOptions { Endpoint = endpoint });
return this;
Expand All @@ -73,7 +73,7 @@ public DatasyncOfflineOptionsBuilder UseEndpoint(Uri endpoint)
/// <returns>The current builder for chaining.</returns>
public DatasyncOfflineOptionsBuilder UseHttpClientOptions(HttpClientOptions clientOptions)
{
ArgumentNullException.ThrowIfNull(clientOptions, nameof(clientOptions));
ArgumentNullException.ThrowIfNull(clientOptions);
this._httpClientFactory = new HttpClientFactory(clientOptions);
return this;
}
Expand All @@ -86,7 +86,7 @@ public DatasyncOfflineOptionsBuilder UseHttpClientOptions(HttpClientOptions clie
/// <returns>The current builder for chaining.</returns>
public DatasyncOfflineOptionsBuilder Entity<TEntity>(Action<EntityOfflineOptions<TEntity>> configure) where TEntity : class
{
ArgumentNullException.ThrowIfNull(configure, nameof(configure));
ArgumentNullException.ThrowIfNull(configure);
if (!this._entities.TryGetValue(typeof(TEntity).FullName!, out EntityOfflineOptions? options))
{
throw new DatasyncException($"Entity is not synchronizable.");
Expand All @@ -108,8 +108,8 @@ public DatasyncOfflineOptionsBuilder Entity<TEntity>(Action<EntityOfflineOptions
/// <returns>The current builder for chaining.</returns>
public DatasyncOfflineOptionsBuilder Entity(Type entityType, Action<EntityOfflineOptions> configure)
{
ArgumentNullException.ThrowIfNull(entityType, nameof(entityType));
ArgumentNullException.ThrowIfNull(configure, nameof(configure));
ArgumentNullException.ThrowIfNull(entityType);
ArgumentNullException.ThrowIfNull(configure);
if (!this._entities.TryGetValue(entityType.FullName!, out EntityOfflineOptions? options))
{
throw new DatasyncException($"Entity is not synchronizable.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
/// <returns>The results of the pull operation.</returns>
public Task<PullResult> PullAsync(IEnumerable<Type> entityTypes, PullOptions pullOptions, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(entityTypes, nameof(entityTypes));
ArgumentNullException.ThrowIfNull(entityTypes);
ArgumentValidationException.ThrowIfNotValid(pullOptions, nameof(pullOptions));

OfflineOptions offlineOptions = BuildDatasyncOfflineOptions();
Expand Down Expand Up @@ -281,7 +281,7 @@ public Task<PullResult> PullAsync(Action<PullRequestBuilder> configureAction, Ca
/// <returns>The results of the pull operation.</returns>
internal async Task<PullResult> PullAsync(IEnumerable<PullRequest> pullRequests, PullOptions pullOptions, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(pullRequests, nameof(pullRequests));
ArgumentNullException.ThrowIfNull(pullRequests);
ArgumentValidationException.ThrowIfNotValid(pullOptions, nameof(pullOptions));

if (!pullRequests.Any())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ internal List<EntityEntry> GetChangedEntitiesInScope()
/// </remarks>
internal Dictionary<string, Type> GetEntityMap(OfflineDbContext context)
{
ArgumentNullException.ThrowIfNull(context, nameof(context));
ArgumentNullException.ThrowIfNull(context);

Type[] modelEntities = context.Model.GetEntityTypes().Select(m => m.ClrType).ToArray();
Type[] synchronizableEntities = context.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
Expand Down Expand Up @@ -239,7 +239,7 @@ internal bool IsSynchronizationEntity(PropertyInfo property)
/// <returns>The results of the push operation (asynchronously)</returns>
internal async Task<PushResult> PushAsync(IEnumerable<Type> entityTypes, PushOptions pushOptions, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(entityTypes, nameof(entityTypes));
ArgumentNullException.ThrowIfNull(entityTypes);
ArgumentValidationException.ThrowIfNotValid(pushOptions, nameof(pushOptions));
PushResult pushResult = new();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public ConcurrentObservableCollection(IEnumerable<T> list) : base(list)
/// <param name="collection">The new collection.</param>
public void ReplaceAll(IEnumerable<T> collection)
{
ArgumentNullException.ThrowIfNull(collection, nameof(collection));
ArgumentNullException.ThrowIfNull(collection);
try
{
this.suppressNotification = true;
Expand All @@ -65,7 +65,7 @@ public void ReplaceAll(IEnumerable<T> collection)
/// <returns><c>true</c> if any records were added; <c>false</c> otherwise.</returns>
public bool AddRange(IEnumerable<T> collection)
{
ArgumentNullException.ThrowIfNull(collection, nameof(collection));
ArgumentNullException.ThrowIfNull(collection);
bool changed = false;
try
{
Expand Down Expand Up @@ -97,8 +97,8 @@ public bool AddRange(IEnumerable<T> collection)
/// <returns><c>true</c> if the item was added, <c>false</c> otherwise.</returns>
public bool AddIfMissing(Func<T, bool> match, T item)
{
ArgumentNullException.ThrowIfNull(match, nameof(match));
ArgumentNullException.ThrowIfNull(item, nameof(item));
ArgumentNullException.ThrowIfNull(match);
ArgumentNullException.ThrowIfNull(item);
if (!this.Any(match))
{
Add(item);
Expand All @@ -115,7 +115,7 @@ public bool AddIfMissing(Func<T, bool> match, T item)
/// <returns><c>true</c> if an item was removed, <c>false</c> otherwise.</returns>
public bool RemoveIf(Func<T, bool> match)
{
ArgumentNullException.ThrowIfNull(match, nameof(match));
ArgumentNullException.ThrowIfNull(match);
T[] itemsToRemove = this.Where(match).ToArray();
foreach (T? item in itemsToRemove)
{
Expand All @@ -134,8 +134,8 @@ public bool RemoveIf(Func<T, bool> match)
/// <returns><c>true</c> if an item was replaced, <c>false</c> otherwise.</returns>
public bool ReplaceIf(Func<T, bool> match, T replacement)
{
ArgumentNullException.ThrowIfNull(match, nameof(match));
ArgumentNullException.ThrowIfNull(replacement, nameof(replacement));
ArgumentNullException.ThrowIfNull(match);
ArgumentNullException.ThrowIfNull(replacement);
T[] itemsToReplace = this.Where(match).ToArray();
foreach (T? item in itemsToReplace)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal static class IAsyncEnumerableExtensions
/// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
internal static async ValueTask<TSource[]> ToDatasyncArrayAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(source, nameof(source));
ArgumentNullException.ThrowIfNull(source);
List<TSource> list = await source.ToDatasyncListAsync(cancellationToken).ConfigureAwait(false);
return [.. list];
}
Expand Down Expand Up @@ -58,8 +58,8 @@ internal static ValueTask<Dictionary<TKey, TSource>> ToDatasyncDictionaryAsync<T
/// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
internal static async ValueTask<Dictionary<TKey, TSource>> ToDatasyncDictionaryAsync<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken = default) where TKey : notnull
{
ArgumentNullException.ThrowIfNull(source, nameof(source));
ArgumentNullException.ThrowIfNull(keySelector, nameof(keySelector));
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(keySelector);
Dictionary<TKey, TSource> d = new(comparer);
await foreach (TSource? item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
{
Expand Down Expand Up @@ -94,7 +94,7 @@ internal static ValueTask<HashSet<TSource>> ToDatasyncHashSetAsync<TSource>(this
/// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
internal static async ValueTask<HashSet<TSource>> ToDatasyncHashSetAsync<TSource>(this IAsyncEnumerable<TSource> source, IEqualityComparer<TSource>? comparer, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(source, nameof(source));
ArgumentNullException.ThrowIfNull(source);
HashSet<TSource> set = new(comparer);
await foreach (TSource? item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
{
Expand All @@ -114,7 +114,7 @@ internal static async ValueTask<HashSet<TSource>> ToDatasyncHashSetAsync<TSource
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
internal static async ValueTask<List<TSource>> ToDatasyncListAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(source, nameof(source));
ArgumentNullException.ThrowIfNull(source);
List<TSource> list = [];
await foreach (TSource? item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
{
Expand Down Expand Up @@ -146,7 +146,7 @@ internal static ValueTask<ConcurrentObservableCollection<TSource>> ToDatasyncObs
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
internal static async ValueTask<ConcurrentObservableCollection<TSource>> ToDatasyncObservableCollectionAsync<TSource>(this IAsyncEnumerable<TSource> source, ConcurrentObservableCollection<TSource> existingCollection, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(source, nameof(source));
ArgumentNullException.ThrowIfNull(source);
List<TSource> list = await source.ToDatasyncListAsync(cancellationToken).ConfigureAwait(false);
existingCollection.ReplaceAll(list);
return existingCollection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ internal static QueryNode Compile(Expression filterExpression, JsonNamingPolicy
/// <returns>the table member name</returns>
internal static string GetTableMemberName(Expression expression, JsonNamingPolicy namingPolicy)
{
ArgumentNullException.ThrowIfNull(expression, nameof(expression));
ArgumentNullException.ThrowIfNull(expression);
if (expression is MemberExpression member)
{
if (member.Expression.NodeType == ExpressionType.Parameter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal class QueryTranslator<T> where T : class
/// <param name="query">The <see cref="IDatasyncQueryable{TEntity}"/> to translate.</param>
internal QueryTranslator(IDatasyncQueryable<T> query)
{
ArgumentNullException.ThrowIfNull(query, nameof(query));
ArgumentNullException.ThrowIfNull(query);
Query = query.Queryable;
QueryDescription = new() { QueryParameters = query.QueryParameters, RequestTotalCount = query.RequestTotalCount };
NamingPolicy = ((DatasyncServiceClient<T>)query.ServiceClient).JsonSerializerOptions.PropertyNamingPolicy;
Expand All @@ -41,7 +41,7 @@ internal QueryTranslator(IDatasyncQueryable<T> query)
/// <param name="query">The <see cref="IDatasyncPullQuery{TEntity}"/> to translate.</param>
internal QueryTranslator(IDatasyncPullQuery<T> query)
{
ArgumentNullException.ThrowIfNull(query, nameof(query));
ArgumentNullException.ThrowIfNull(query);
Query = query.Queryable;
QueryDescription = new() { QueryParameters = query.QueryParameters, RequestTotalCount = false };
NamingPolicy = DatasyncSerializer.JsonSerializerOptions.PropertyNamingPolicy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ private VisitorHelper()
/// </returns>
public static Expression VisitAll(Expression expression, Func<Expression, Func<Expression, Expression>, Expression> visitor)
{
ArgumentNullException.ThrowIfNull(expression, nameof(expression));
ArgumentNullException.ThrowIfNull(visitor, nameof(visitor));
ArgumentNullException.ThrowIfNull(expression);
ArgumentNullException.ThrowIfNull(visitor);
return new VisitorHelper() { visitor = visitor }.Visit(expression);
}

Expand All @@ -73,8 +73,8 @@ public static Expression VisitAll(Expression expression, Func<Expression, Func<E
/// </returns>
public static Expression VisitMembers(Expression expression, Func<MemberExpression, Func<MemberExpression, Expression>, Expression> visitor)
{
ArgumentNullException.ThrowIfNull(expression, nameof(expression));
ArgumentNullException.ThrowIfNull(visitor, nameof(visitor));
ArgumentNullException.ThrowIfNull(expression);
ArgumentNullException.ThrowIfNull(visitor);
return new VisitorHelper() { memberVisitor = visitor }.Visit(expression);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public override QueryNode Visit(UnaryOperatorNode node)
/// <param name="node">The node to visit</param>
protected void Accept(QueryNode parent, QueryNode node)
{
ArgumentNullException.ThrowIfNull(node, nameof(node));
ArgumentNullException.ThrowIfNull(node);
node.Accept(this);
}
}
Loading

0 comments on commit 4892839

Please sign in to comment.