diff --git a/src/EventSourcingTests/Bugs/Bug_2025_event_inheritance_in_projection.cs b/src/EventSourcingTests/Bugs/Bug_2025_event_inheritance_in_projection.cs index d39003f7a9..64db2efe91 100644 --- a/src/EventSourcingTests/Bugs/Bug_2025_event_inheritance_in_projection.cs +++ b/src/EventSourcingTests/Bugs/Bug_2025_event_inheritance_in_projection.cs @@ -54,7 +54,7 @@ public Identity(UserCreated @event) public void Apply(IdentityAdded @event) { - if (@event is null) throw new ArgumentNullException(); + ArgumentNullException.ThrowIfNull(@event); LastName = @event.LastName; FirstName = @event.FirstName; diff --git a/src/Marten.Testing/Harness/SpecificationExtensions.cs b/src/Marten.Testing/Harness/SpecificationExtensions.cs index bf91ee87e6..e851dcc444 100644 --- a/src/Marten.Testing/Harness/SpecificationExtensions.cs +++ b/src/Marten.Testing/Harness/SpecificationExtensions.cs @@ -74,8 +74,7 @@ public static void ShouldBeEqualWithDbPrecision(this DateTimeOffset actual, Date public static void ShouldContain(this DbObjectName[] names, string qualifiedName) { - if (names == null) - throw new ArgumentNullException(nameof(names)); + ArgumentNullException.ThrowIfNull(names); var function = DbObjectName.Parse(PostgresqlProvider.Instance, qualifiedName); names.ShouldContain(function); diff --git a/src/Marten/Events/EventGraph.cs b/src/Marten/Events/EventGraph.cs index 3fb6b00a00..7468707981 100644 --- a/src/Marten/Events/EventGraph.cs +++ b/src/Marten/Events/EventGraph.cs @@ -158,10 +158,7 @@ public string AggregateAliasFor(Type aggregateType) public IEvent BuildEvent(object eventData) { - if (eventData == null) - { - throw new ArgumentNullException(nameof(eventData)); - } + ArgumentNullException.ThrowIfNull(eventData); var mapping = EventMappingFor(eventData.GetType()); return mapping.Wrap(eventData); diff --git a/src/Marten/Internal/Sessions/DocumentSessionBase.cs b/src/Marten/Internal/Sessions/DocumentSessionBase.cs index 83ef6fc6df..3c23d73b23 100644 --- a/src/Marten/Internal/Sessions/DocumentSessionBase.cs +++ b/src/Marten/Internal/Sessions/DocumentSessionBase.cs @@ -62,10 +62,7 @@ public void Store(IEnumerable entities) where T : notnull public void Store(params T[] entities) where T : notnull { - if (entities == null) - { - throw new ArgumentNullException(nameof(entities)); - } + ArgumentNullException.ThrowIfNull(entities); if (typeof(T).IsGenericEnumerable()) { @@ -124,10 +121,7 @@ public void Insert(params T[] entities) where T : notnull { assertNotDisposed(); - if (entities == null) - { - throw new ArgumentNullException(nameof(entities)); - } + ArgumentNullException.ThrowIfNull(entities); if (typeof(T).IsGenericEnumerable()) { @@ -162,10 +156,7 @@ public void Update(params T[] entities) where T : notnull { assertNotDisposed(); - if (entities == null) - { - throw new ArgumentNullException(nameof(entities)); - } + ArgumentNullException.ThrowIfNull(entities); if (typeof(T).IsGenericEnumerable()) { diff --git a/src/Marten/Internal/Sessions/EventTracingConnectionLifetime.cs b/src/Marten/Internal/Sessions/EventTracingConnectionLifetime.cs index b508a31132..07a61e30f8 100644 --- a/src/Marten/Internal/Sessions/EventTracingConnectionLifetime.cs +++ b/src/Marten/Internal/Sessions/EventTracingConnectionLifetime.cs @@ -28,10 +28,7 @@ internal class EventTracingConnectionLifetime: public EventTracingConnectionLifetime(IConnectionLifetime innerConnectionLifetime, string tenantId, OpenTelemetryOptions telemetryOptions) { - if (innerConnectionLifetime == null) - { - throw new ArgumentNullException(nameof(innerConnectionLifetime)); - } + ArgumentNullException.ThrowIfNull(innerConnectionLifetime); if (string.IsNullOrWhiteSpace(tenantId)) { diff --git a/src/Marten/JsonLoader.cs b/src/Marten/JsonLoader.cs index d3e1d4be3c..8e81ea1403 100644 --- a/src/Marten/JsonLoader.cs +++ b/src/Marten/JsonLoader.cs @@ -20,10 +20,7 @@ public JsonLoader(QuerySession session) public Task FindByIdAsync(object id, CancellationToken token = default) where T : class { - if (id == null) - { - throw new ArgumentNullException(nameof(id)); - } + ArgumentNullException.ThrowIfNull(id); var streamer = typeof(Streamer<,>).CloseAndBuildAs>(this, typeof(T), id.GetType()); return streamer.FindByIdAsync(id, token); @@ -51,10 +48,7 @@ public JsonLoader(QuerySession session) public Task StreamById(object id, Stream destination, CancellationToken token = default) where T : class { - if (id == null) - { - throw new ArgumentNullException(nameof(id)); - } + ArgumentNullException.ThrowIfNull(id); var streamer = typeof(Streamer<,>).CloseAndBuildAs>(this, typeof(T), id.GetType()); return streamer.StreamJsonById(id, destination, token); diff --git a/src/Marten/Linq/Members/QueryableMember.cs b/src/Marten/Linq/Members/QueryableMember.cs index af9b8f8637..ff2cf2e108 100644 --- a/src/Marten/Linq/Members/QueryableMember.cs +++ b/src/Marten/Linq/Members/QueryableMember.cs @@ -28,7 +28,7 @@ public abstract class QueryableMember: IQueryableMember, IHasChildrenMembers /// protected QueryableMember(IQueryableMember parent, Casing casing, MemberInfo member) { - if (parent == null) throw new ArgumentNullException(nameof(parent)); + ArgumentNullException.ThrowIfNull(parent); Member = member; MemberType = member is ElementMember m ? m.ReflectedType : member.GetMemberType()!; diff --git a/src/Marten/Linq/SqlGeneration/FilterStatement.cs b/src/Marten/Linq/SqlGeneration/FilterStatement.cs index 49c7406dee..18f688796d 100644 --- a/src/Marten/Linq/SqlGeneration/FilterStatement.cs +++ b/src/Marten/Linq/SqlGeneration/FilterStatement.cs @@ -55,10 +55,7 @@ internal class FilterStatement: SelectorStatement { public FilterStatement(IMartenSession session, Statement parent, ISqlFragment where) { - if (where == null) - { - throw new ArgumentNullException(nameof(where)); - } + ArgumentNullException.ThrowIfNull(where); Wheres.Add(where); diff --git a/src/Marten/QueryableExtensions.cs b/src/Marten/QueryableExtensions.cs index 890ff5b8eb..cd39f08c04 100644 --- a/src/Marten/QueryableExtensions.cs +++ b/src/Marten/QueryableExtensions.cs @@ -303,10 +303,7 @@ public static Task AnyAsync( this IQueryable source, CancellationToken token = default) where TSource : notnull { - if (source == null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); return source.As>().AnyAsync(token); } @@ -316,15 +313,9 @@ public static Task AnyAsync( Expression> predicate, CancellationToken token = default) where TSource : notnull { - if (source == null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); - if (predicate == null) - { - throw new ArgumentNullException(nameof(predicate)); - } + ArgumentNullException.ThrowIfNull(predicate); return source.Where(predicate).AnyAsync(token); } @@ -337,10 +328,7 @@ public static Task SumAsync( this IQueryable source, Expression> expression, CancellationToken token = default) where TResult : notnull { - if (source == null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); return source.Select(expression).As>().SumAsync(token); } @@ -349,10 +337,7 @@ public static Task MaxAsync( this IQueryable source, Expression> expression, CancellationToken token = default) where TResult : notnull { - if (source == null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); return source.Select(expression).As>().MaxAsync(token); } @@ -361,10 +346,7 @@ public static Task MinAsync( this IQueryable source, Expression> expression, CancellationToken token = default) where TResult : notnull { - if (source == null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); return source.Select(expression).As>().MinAsync(token); } @@ -373,10 +355,7 @@ public static Task AverageAsync( this IQueryable source, Expression> expression, CancellationToken token = default) where TMember : notnull { - if (source == null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); return source.Select(expression).As>().AverageAsync(token); } @@ -389,10 +368,7 @@ public static Task CountAsync( this IQueryable source, CancellationToken token = default) where TSource : notnull { - if (source == null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); return source.As>().CountAsync(token); } @@ -402,15 +378,9 @@ public static Task CountAsync( Expression> predicate, CancellationToken token = default) where TSource : notnull { - if (source == null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); - if (predicate == null) - { - throw new ArgumentNullException(nameof(predicate)); - } + ArgumentNullException.ThrowIfNull(predicate); return source.Where(predicate).CountAsync(token); } @@ -419,10 +389,7 @@ public static Task LongCountAsync( this IQueryable source, CancellationToken token = default) where TSource : notnull { - if (source == null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); return source.As>().CountLongAsync(token); } @@ -432,15 +399,9 @@ public static Task LongCountAsync( Expression> predicate, CancellationToken token = default) where TSource : notnull { - if (source == null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); - if (predicate == null) - { - throw new ArgumentNullException(nameof(predicate)); - } + ArgumentNullException.ThrowIfNull(predicate); return source.Where(predicate).LongCountAsync(token); } @@ -453,10 +414,7 @@ public static Task FirstAsync( this IQueryable source, CancellationToken token = default) where TSource : notnull { - if (source == null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); return source.As>().FirstAsync(token); } @@ -466,15 +424,9 @@ public static Task FirstAsync( Expression> predicate, CancellationToken token = default) where TSource : notnull { - if (source == null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); - if (predicate == null) - { - throw new ArgumentNullException(nameof(predicate)); - } + ArgumentNullException.ThrowIfNull(predicate); return source.Where(predicate).FirstAsync(token); } @@ -483,10 +435,7 @@ public static Task FirstAsync( this IQueryable source, CancellationToken token = default) where TSource : notnull { - if (source == null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); return source.As>().FirstOrDefaultAsync(token); } @@ -496,15 +445,9 @@ public static Task FirstAsync( Expression> predicate, CancellationToken token = default) where TSource : notnull { - if (source == null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); - if (predicate == null) - { - throw new ArgumentNullException(nameof(predicate)); - } + ArgumentNullException.ThrowIfNull(predicate); return source.Where(predicate).FirstOrDefaultAsync(token); } @@ -517,10 +460,7 @@ public static Task SingleAsync( this IQueryable source, CancellationToken token = default) where TSource : notnull { - if (source == null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); return source.As>().SingleAsync(token); } @@ -530,15 +470,9 @@ public static Task SingleAsync( Expression> predicate, CancellationToken token = default) where TSource : notnull { - if (source == null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); - if (predicate == null) - { - throw new ArgumentNullException(nameof(predicate)); - } + ArgumentNullException.ThrowIfNull(predicate); return source.Where(predicate).SingleAsync(token); } @@ -547,10 +481,7 @@ public static Task SingleAsync( this IQueryable source, CancellationToken token = default) where TSource : notnull { - if (source == null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); return source.As>().SingleOrDefaultAsync(token); } @@ -560,15 +491,9 @@ public static Task SingleAsync( Expression> predicate, CancellationToken token = default) where TSource : notnull { - if (source == null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); - if (predicate == null) - { - throw new ArgumentNullException(nameof(predicate)); - } + ArgumentNullException.ThrowIfNull(predicate); return source.Where(predicate).SingleOrDefaultAsync(token); } diff --git a/src/Marten/Schema/Arguments/UpsertArgument.cs b/src/Marten/Schema/Arguments/UpsertArgument.cs index 2802f634db..59cb1da756 100644 --- a/src/Marten/Schema/Arguments/UpsertArgument.cs +++ b/src/Marten/Schema/Arguments/UpsertArgument.cs @@ -35,10 +35,7 @@ public string PostgresType get => _postgresType; set { - if (value == null) - { - throw new ArgumentNullException(); - } + ArgumentNullException.ThrowIfNull(value); _postgresType = value.Contains("(") ? value.Split('(')[0].Trim() diff --git a/src/Marten/Schema/SchemaBuilder.cs b/src/Marten/Schema/SchemaBuilder.cs index d4a35b6072..29a120e571 100644 --- a/src/Marten/Schema/SchemaBuilder.cs +++ b/src/Marten/Schema/SchemaBuilder.cs @@ -16,10 +16,7 @@ public static string GetSqlScript(string databaseSchemaName, string script) public static string GetJavascript(StoreOptions options, string jsfile, string? @namespace = null) { - if (options == null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); @namespace ??= typeof(SchemaBuilder).Namespace; diff --git a/src/Marten/Services/JsonArrayPool.cs b/src/Marten/Services/JsonArrayPool.cs index 231f8be91a..910e391432 100644 --- a/src/Marten/Services/JsonArrayPool.cs +++ b/src/Marten/Services/JsonArrayPool.cs @@ -20,10 +20,7 @@ public T[] Rent(int minimumLength) public void Return(T[]? array) { - if (array == null) - { - throw new ArgumentNullException(nameof(array)); - } + ArgumentNullException.ThrowIfNull(array); _inner.Return(array); } diff --git a/src/Marten/Storage/StorageFeatures.cs b/src/Marten/Storage/StorageFeatures.cs index 60f70660c9..d3d57fcfef 100644 --- a/src/Marten/Storage/StorageFeatures.cs +++ b/src/Marten/Storage/StorageFeatures.cs @@ -193,10 +193,7 @@ internal DocumentMapping MappingFor(Type documentType) internal IDocumentMapping FindMapping(Type documentType) { - if (documentType == null) - { - throw new ArgumentNullException(nameof(documentType)); - } + ArgumentNullException.ThrowIfNull(documentType); if (!_mappings.Value.TryFind(documentType, out var value)) { diff --git a/src/Marten/Storage/UpsertFunction.cs b/src/Marten/Storage/UpsertFunction.cs index 6ee7871e68..7eb73e640f 100644 --- a/src/Marten/Storage/UpsertFunction.cs +++ b/src/Marten/Storage/UpsertFunction.cs @@ -35,10 +35,7 @@ public UpsertFunction(DocumentMapping mapping, DbObjectName? identifier = null, { _mapping = mapping; _disableConcurrency = disableConcurrency; - if (mapping == null) - { - throw new ArgumentNullException(nameof(mapping)); - } + ArgumentNullException.ThrowIfNull(mapping); _tableName = mapping.TableName; diff --git a/src/Marten/StoreOptions.Identity.cs b/src/Marten/StoreOptions.Identity.cs index 89ba02173d..a1961de463 100644 --- a/src/Marten/StoreOptions.Identity.cs +++ b/src/Marten/StoreOptions.Identity.cs @@ -93,6 +93,19 @@ internal ValueTypeInfo FindOrCreateValueType(Type idType) return valueType ?? RegisterValueType(idType); } + /// + /// Register a custom value type with Marten. Doing this enables Marten + /// to use this type correctly within LINQ expressions. The "TValueType" + /// should wrap a single, primitive value with a single public get-able + /// property + /// + /// + /// + public ValueTypeInfo RegisterValueType() where TValueType : notnull + { + return RegisterValueType(typeof(TValueType)); + } + /// /// Register a custom value type with Marten. Doing this enables Marten /// to use this type correctly within LINQ expressions. The "value type" diff --git a/src/Marten/StoreOptions.MemberFactory.cs b/src/Marten/StoreOptions.MemberFactory.cs index f22cebae9d..ff08fbf8e7 100644 --- a/src/Marten/StoreOptions.MemberFactory.cs +++ b/src/Marten/StoreOptions.MemberFactory.cs @@ -22,10 +22,7 @@ public partial class StoreOptions { internal IQueryableMember CreateQueryableMember(MemberInfo member, IQueryableMember parent) { - if (member == null) - { - throw new ArgumentNullException(nameof(member)); - } + ArgumentNullException.ThrowIfNull(member); var memberType = member.GetMemberType();