From b0e39cecf94cf46816ff646ee7a27af56d45775d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hakan=20F=C4=B1st=C4=B1k?= Date: Wed, 11 Jun 2025 10:59:50 +0200 Subject: [PATCH 1/5] Introduced `RegisterValueType()` method to allow custom value type registration using generics --- src/Marten/StoreOptions.Identity.cs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Marten/StoreOptions.Identity.cs b/src/Marten/StoreOptions.Identity.cs index 89ba02173d..8bc93e68b1 100644 --- a/src/Marten/StoreOptions.Identity.cs +++ b/src/Marten/StoreOptions.Identity.cs @@ -21,7 +21,8 @@ internal IDocumentStorage ResolveCorrectedDocumentStorage( var provider = Providers.StorageFor(); var raw = provider.Select(tracking); - if (raw is IDocumentStorage storage) return storage; + if (raw is IDocumentStorage storage) + return storage; var valueTypeInfo = TryFindValueType(raw.IdType); if (valueTypeInfo == null) @@ -76,8 +77,10 @@ internal IIdGeneration DetermineIdStrategy(Type documentType, MemberInfo idMembe private bool idMemberIsSettable(MemberInfo idMember) { - if (idMember is FieldInfo f) return f.IsPublic; - if (idMember is PropertyInfo p) return p.CanWrite && p.SetMethod != null; + if (idMember is FieldInfo f) + return f.IsPublic; + if (idMember is PropertyInfo p) + return p.CanWrite && p.SetMethod != null; return false; } @@ -93,6 +96,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" @@ -122,7 +138,8 @@ public ValueTypeInfo RegisterValueType(Type type) valueProperty = type.GetProperties().SingleOrDefaultIfMany(); } - if (valueProperty == null || !valueProperty.CanRead) throw new InvalidValueTypeException(type, "Must be only a single public, 'gettable' property"); + if (valueProperty == null || !valueProperty.CanRead) + throw new InvalidValueTypeException(type, "Must be only a single public, 'gettable' property"); var ctor = type.GetConstructors() .FirstOrDefault(x => x.GetParameters().Length == 1 && x.GetParameters()[0].ParameterType == valueProperty.PropertyType); From a18dfe3d12b555e07561b9029868b4faf538e7b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hakan=20F=C4=B1st=C4=B1k?= Date: Wed, 11 Jun 2025 11:01:42 +0200 Subject: [PATCH 2/5] Revert "Introduced `RegisterValueType()` method to allow custom value type registration using generics" This reverts commit b0e39cecf94cf46816ff646ee7a27af56d45775d. --- src/Marten/StoreOptions.Identity.cs | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/src/Marten/StoreOptions.Identity.cs b/src/Marten/StoreOptions.Identity.cs index 8bc93e68b1..89ba02173d 100644 --- a/src/Marten/StoreOptions.Identity.cs +++ b/src/Marten/StoreOptions.Identity.cs @@ -21,8 +21,7 @@ internal IDocumentStorage ResolveCorrectedDocumentStorage( var provider = Providers.StorageFor(); var raw = provider.Select(tracking); - if (raw is IDocumentStorage storage) - return storage; + if (raw is IDocumentStorage storage) return storage; var valueTypeInfo = TryFindValueType(raw.IdType); if (valueTypeInfo == null) @@ -77,10 +76,8 @@ internal IIdGeneration DetermineIdStrategy(Type documentType, MemberInfo idMembe private bool idMemberIsSettable(MemberInfo idMember) { - if (idMember is FieldInfo f) - return f.IsPublic; - if (idMember is PropertyInfo p) - return p.CanWrite && p.SetMethod != null; + if (idMember is FieldInfo f) return f.IsPublic; + if (idMember is PropertyInfo p) return p.CanWrite && p.SetMethod != null; return false; } @@ -96,19 +93,6 @@ 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" @@ -138,8 +122,7 @@ public ValueTypeInfo RegisterValueType(Type type) valueProperty = type.GetProperties().SingleOrDefaultIfMany(); } - if (valueProperty == null || !valueProperty.CanRead) - throw new InvalidValueTypeException(type, "Must be only a single public, 'gettable' property"); + if (valueProperty == null || !valueProperty.CanRead) throw new InvalidValueTypeException(type, "Must be only a single public, 'gettable' property"); var ctor = type.GetConstructors() .FirstOrDefault(x => x.GetParameters().Length == 1 && x.GetParameters()[0].ParameterType == valueProperty.PropertyType); From c0ab093af7bb768cefb36802ab37cfc34ef2db1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hakan=20F=C4=B1st=C4=B1k?= Date: Wed, 11 Jun 2025 11:02:43 +0200 Subject: [PATCH 3/5] Introduced `RegisterValueType()` method to allow custom value type registration using generics --- src/Marten/StoreOptions.Identity.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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" From 8a248a57e0109cc34a8895ec68a7e94330e479fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hakan=20F=C4=B1st=C4=B1k?= Date: Wed, 11 Jun 2025 11:17:37 +0200 Subject: [PATCH 4/5] use ArgumentNullException.ThrowIfNull in the whole solution --- ...ug_2025_event_inheritance_in_projection.cs | 2 +- .../Harness/SpecificationExtensions.cs | 3 +- src/Marten/Events/EventGraph.cs | 5 +- .../Internal/Sessions/DocumentSessionBase.cs | 15 +-- .../EventTracingConnectionLifetime.cs | 5 +- src/Marten/JsonLoader.cs | 10 +- src/Marten/Linq/Members/QueryableMember.cs | 2 +- .../Linq/SqlGeneration/FilterStatement.cs | 5 +- src/Marten/QueryableExtensions.cs | 125 ++++-------------- src/Marten/Schema/Arguments/UpsertArgument.cs | 5 +- src/Marten/Schema/SchemaBuilder.cs | 5 +- src/Marten/Services/JsonArrayPool.cs | 5 +- src/Marten/Storage/StorageFeatures.cs | 5 +- src/Marten/Storage/UpsertFunction.cs | 5 +- src/Marten/StoreOptions.MemberFactory.cs | 5 +- 15 files changed, 42 insertions(+), 160 deletions(-) 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.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(); From f9809d9338986a9f83b30d1c652a7270874496e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hakan=20F=C4=B1st=C4=B1k?= Date: Sun, 15 Jun 2025 11:27:41 +0200 Subject: [PATCH 5/5] Add 'readonly' modifier to structs solving message IDE0250 https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0250 --- src/EventSourcingTests/archiving_events.cs | 4 ++-- .../StrongTypedId/guid_based_document_operations.cs | 4 ++-- .../StrongTypedId/int_based_document_operations.cs | 2 +- .../StrongTypedId/linq_querying_with_value_types.cs | 8 ++++---- .../StrongTypedId/long_based_document_operations.cs | 2 +- .../StrongTypedId/string_id_document_operations.cs | 4 ++-- src/ValueTypeTests/TestingTypes.cs | 12 ++++++------ .../VogenIds/guid_based_document_operations.cs | 4 ++-- .../VogenIds/int_based_document_operations.cs | 2 +- .../VogenIds/linq_querying_with_value_types.cs | 8 ++++---- .../VogenIds/long_based_document_operations.cs | 2 +- .../VogenIds/string_id_document_operations.cs | 4 ++-- src/ValueTypeTests/include_usage.cs | 6 +++--- src/ValueTypeTests/linq_querying_with_value_types.cs | 4 ++-- src/ValueTypeTests/registration.cs | 2 +- 15 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/EventSourcingTests/archiving_events.cs b/src/EventSourcingTests/archiving_events.cs index 0c023581e4..ef8afe0bc6 100644 --- a/src/EventSourcingTests/archiving_events.cs +++ b/src/EventSourcingTests/archiving_events.cs @@ -647,7 +647,7 @@ public record Deleted; public record MaybeDeleted(bool ShouldDelete); [StronglyTypedId(Template.Guid)] -public partial struct GuidId; +public readonly partial struct GuidId; public class SimpleAggregateStrongTypedGuid { @@ -690,7 +690,7 @@ public void Apply(EEvent _) } [StronglyTypedId(Template.String)] -public partial struct StringId; +public readonly partial struct StringId; public class SimpleAggregateStrongTypedString { diff --git a/src/ValueTypeTests/StrongTypedId/guid_based_document_operations.cs b/src/ValueTypeTests/StrongTypedId/guid_based_document_operations.cs index 72431d9c34..d5c39674de 100644 --- a/src/ValueTypeTests/StrongTypedId/guid_based_document_operations.cs +++ b/src/ValueTypeTests/StrongTypedId/guid_based_document_operations.cs @@ -279,10 +279,10 @@ public async Task bulk_writing_sync() [StronglyTypedId(Template.Guid)] -public partial struct Invoice2Id; +public readonly partial struct Invoice2Id; [StronglyTypedId(Template.Guid)] -public partial struct WrongId; +public readonly partial struct WrongId; public class Invoice2 { diff --git a/src/ValueTypeTests/StrongTypedId/int_based_document_operations.cs b/src/ValueTypeTests/StrongTypedId/int_based_document_operations.cs index 43bfa515a7..b8a5c2e09d 100644 --- a/src/ValueTypeTests/StrongTypedId/int_based_document_operations.cs +++ b/src/ValueTypeTests/StrongTypedId/int_based_document_operations.cs @@ -262,7 +262,7 @@ public async Task use_in_LINQ_select_clause() #region sample_order2_with_STRONG_TYPED_identifier [StronglyTypedId(Template.Int)] -public partial struct Order2Id; +public readonly partial struct Order2Id; public class Order2 { diff --git a/src/ValueTypeTests/StrongTypedId/linq_querying_with_value_types.cs b/src/ValueTypeTests/StrongTypedId/linq_querying_with_value_types.cs index 524bc3433e..3dd96dfc2b 100644 --- a/src/ValueTypeTests/StrongTypedId/linq_querying_with_value_types.cs +++ b/src/ValueTypeTests/StrongTypedId/linq_querying_with_value_types.cs @@ -207,16 +207,16 @@ public async Task store_several_and_use_in_LINQ_select() } [StronglyTypedId(Template.Long)] -public partial struct UpperLimit; +public readonly partial struct UpperLimit; [StronglyTypedId(Template.Int)] -public partial struct LowerLimit; +public readonly partial struct LowerLimit; [StronglyTypedId(Template.String)] -public partial struct Description; +public readonly partial struct Description; [StronglyTypedId(Template.Guid)] -public partial struct GuidId; +public readonly partial struct GuidId; public class LimitedDoc { diff --git a/src/ValueTypeTests/StrongTypedId/long_based_document_operations.cs b/src/ValueTypeTests/StrongTypedId/long_based_document_operations.cs index 7eb2360ddf..766b7ea197 100644 --- a/src/ValueTypeTests/StrongTypedId/long_based_document_operations.cs +++ b/src/ValueTypeTests/StrongTypedId/long_based_document_operations.cs @@ -267,7 +267,7 @@ public async Task use_in_LINQ_select_clause() } [StronglyTypedId(Template.Long)] -public partial struct Issue2Id; +public readonly partial struct Issue2Id; public class Issue2 { diff --git a/src/ValueTypeTests/StrongTypedId/string_id_document_operations.cs b/src/ValueTypeTests/StrongTypedId/string_id_document_operations.cs index 1fd74b9e4a..ca5fa955ae 100644 --- a/src/ValueTypeTests/StrongTypedId/string_id_document_operations.cs +++ b/src/ValueTypeTests/StrongTypedId/string_id_document_operations.cs @@ -237,10 +237,10 @@ public async Task use_in_LINQ_select_clause() } [StronglyTypedId(Template.String)] -public partial struct Team2Id; +public readonly partial struct Team2Id; [StronglyTypedId(Template.String)] -public partial struct WrongStringId; +public readonly partial struct WrongStringId; public class Team2 diff --git a/src/ValueTypeTests/TestingTypes.cs b/src/ValueTypeTests/TestingTypes.cs index 40e2334a8d..e45c558251 100644 --- a/src/ValueTypeTests/TestingTypes.cs +++ b/src/ValueTypeTests/TestingTypes.cs @@ -6,19 +6,19 @@ namespace ValueTypeTests; [ValueObject] -public partial struct GuidId; +public readonly partial struct GuidId; [ValueObject] -public partial struct IntId; +public readonly partial struct IntId; [ValueObject] -public partial struct LongId; +public readonly partial struct LongId; [ValueObject] -public partial struct StringId; +public readonly partial struct StringId; [ValueObject] -public partial struct DateId; +public readonly partial struct DateId; public record struct NewGuidId(Guid Value); public record struct NewIntId(int Value); @@ -39,7 +39,7 @@ public record struct TaskId(Guid Value); /// with a public static method that takes in the /// inner value /// -public struct Task2Id +public readonly struct Task2Id { private Task2Id(Guid value) => Value = value; diff --git a/src/ValueTypeTests/VogenIds/guid_based_document_operations.cs b/src/ValueTypeTests/VogenIds/guid_based_document_operations.cs index 552580aad7..a2e731d992 100644 --- a/src/ValueTypeTests/VogenIds/guid_based_document_operations.cs +++ b/src/ValueTypeTests/VogenIds/guid_based_document_operations.cs @@ -287,7 +287,7 @@ public async Task bulk_writing_sync() #region sample_invoice_with_vogen_id [ValueObject] -public partial struct InvoiceId; +public readonly partial struct InvoiceId; public class Invoice { @@ -303,4 +303,4 @@ public class Invoice [ValueObject] -public partial struct WrongId; +public readonly partial struct WrongId; diff --git a/src/ValueTypeTests/VogenIds/int_based_document_operations.cs b/src/ValueTypeTests/VogenIds/int_based_document_operations.cs index 0701a2791c..f6c4965733 100644 --- a/src/ValueTypeTests/VogenIds/int_based_document_operations.cs +++ b/src/ValueTypeTests/VogenIds/int_based_document_operations.cs @@ -259,7 +259,7 @@ public async Task use_in_LINQ_select_clause() } [ValueObject] -public partial struct OrderId; +public readonly partial struct OrderId; public class Order { diff --git a/src/ValueTypeTests/VogenIds/linq_querying_with_value_types.cs b/src/ValueTypeTests/VogenIds/linq_querying_with_value_types.cs index 979e0ff869..0eb2e3197e 100644 --- a/src/ValueTypeTests/VogenIds/linq_querying_with_value_types.cs +++ b/src/ValueTypeTests/VogenIds/linq_querying_with_value_types.cs @@ -217,16 +217,16 @@ public async Task store_several_and_use_in_LINQ_select() #region sample_limited_doc [ValueObject] -public partial struct UpperLimit; +public readonly partial struct UpperLimit; [ValueObject] -public partial struct LowerLimit; +public readonly partial struct LowerLimit; [ValueObject] -public partial struct Description; +public readonly partial struct Description; [ValueObject] -public partial struct GuidId; +public readonly partial struct GuidId; public class LimitedDoc { diff --git a/src/ValueTypeTests/VogenIds/long_based_document_operations.cs b/src/ValueTypeTests/VogenIds/long_based_document_operations.cs index 94a280d708..3806b0c700 100644 --- a/src/ValueTypeTests/VogenIds/long_based_document_operations.cs +++ b/src/ValueTypeTests/VogenIds/long_based_document_operations.cs @@ -259,7 +259,7 @@ public async Task use_in_LINQ_select_clause() } [ValueObject] -public partial struct IssueId; +public readonly partial struct IssueId; public class Issue { diff --git a/src/ValueTypeTests/VogenIds/string_id_document_operations.cs b/src/ValueTypeTests/VogenIds/string_id_document_operations.cs index 81ec1f1401..929206e557 100644 --- a/src/ValueTypeTests/VogenIds/string_id_document_operations.cs +++ b/src/ValueTypeTests/VogenIds/string_id_document_operations.cs @@ -236,10 +236,10 @@ public async Task use_in_LINQ_select_clause() } [ValueObject] -public partial struct TeamId; +public readonly partial struct TeamId; [ValueObject] -public partial struct WrongStringId; +public readonly partial struct WrongStringId; public class Team diff --git a/src/ValueTypeTests/include_usage.cs b/src/ValueTypeTests/include_usage.cs index 78a019f540..5285f1e461 100644 --- a/src/ValueTypeTests/include_usage.cs +++ b/src/ValueTypeTests/include_usage.cs @@ -108,7 +108,7 @@ public async Task include_multiple_references() } [ValueObject] -public partial struct TeacherId; +public readonly partial struct TeacherId; public class Teacher { @@ -116,7 +116,7 @@ public class Teacher } [ValueObject] -public partial struct ClassId; +public readonly partial struct ClassId; public class Class { @@ -125,7 +125,7 @@ public class Class } [ValueObject] -public partial struct GradeId; +public readonly partial struct GradeId; public class Grade { diff --git a/src/ValueTypeTests/linq_querying_with_value_types.cs b/src/ValueTypeTests/linq_querying_with_value_types.cs index b92d520f47..931a38820c 100644 --- a/src/ValueTypeTests/linq_querying_with_value_types.cs +++ b/src/ValueTypeTests/linq_querying_with_value_types.cs @@ -127,10 +127,10 @@ public Expression, IEnumerable>> Q #region sample_limited_doc [ValueObject] -public partial struct UpperLimit; +public readonly partial struct UpperLimit; [ValueObject] -public partial struct LowerLimit; +public readonly partial struct LowerLimit; public class LimitedDoc { diff --git a/src/ValueTypeTests/registration.cs b/src/ValueTypeTests/registration.cs index 184dbd4de8..daed2b77ff 100644 --- a/src/ValueTypeTests/registration.cs +++ b/src/ValueTypeTests/registration.cs @@ -41,7 +41,7 @@ public void sad_path_registration(Type type) public record struct ExternalId(string Value); -public struct SpecialValue +public readonly struct SpecialValue { private SpecialValue(string value) {