From 27c478561f44edf8995a351deb2fe1e7fc0b85bf Mon Sep 17 00:00:00 2001 From: Alex Zaytsev Date: Mon, 11 Mar 2024 20:10:32 +1000 Subject: [PATCH] Use null propagation where possible (#651) +semver:patch --- src/FluentNHibernate/Automapping/AutoMapping.cs | 6 ++---- src/FluentNHibernate/Automapping/AutoSubClassPart.cs | 2 +- src/FluentNHibernate/Automapping/Steps/PropertyStep.cs | 5 ++--- src/FluentNHibernate/Mapping/ClasslikeMapBase.cs | 2 +- src/FluentNHibernate/Mapping/CompositeIdentityPart.cs | 8 ++------ src/FluentNHibernate/Mapping/ManyToManyPart.cs | 6 ++---- src/FluentNHibernate/Mapping/Member.cs | 5 +---- src/FluentNHibernate/Mapping/ToManyBase.cs | 8 +++----- .../MappingModel/ClassBased/ReferenceComponentMapping.cs | 3 +-- src/FluentNHibernate/MappingModel/TypeReference.cs | 5 +---- .../Visitors/ComponentReferenceResolutionVisitor.cs | 5 +---- src/FluentNHibernate/Visitors/ConventionVisitor.cs | 3 +-- .../Visitors/RelationshipPairingVisitor.cs | 5 +---- 13 files changed, 19 insertions(+), 44 deletions(-) diff --git a/src/FluentNHibernate/Automapping/AutoMapping.cs b/src/FluentNHibernate/Automapping/AutoMapping.cs index 2a2d6193b..7afe006d1 100644 --- a/src/FluentNHibernate/Automapping/AutoMapping.cs +++ b/src/FluentNHibernate/Automapping/AutoMapping.cs @@ -133,8 +133,7 @@ public AutoJoinedSubClassPart JoinedSubClass(string keyCol var genericType = typeof(AutoJoinedSubClassPart<>).MakeGenericType(typeof(TSubclass)); var joinedclass = (AutoJoinedSubClassPart)Activator.CreateInstance(genericType, keyColumn); - if (action is not null) - action(joinedclass); + action?.Invoke(joinedclass); providers.Subclasses[typeof(TSubclass)] = joinedclass; @@ -167,8 +166,7 @@ public AutoSubClassPart SubClass(object discriminatorValue var genericType = typeof(AutoSubClassPart<>).MakeGenericType(typeof(TSubclass)); var subclass = (AutoSubClassPart)Activator.CreateInstance(genericType, null, discriminatorValue); - if (action is not null) - action(subclass); + action?.Invoke(subclass); // remove any mappings for the same type, then re-add providers.Subclasses[typeof(TSubclass)] = subclass; diff --git a/src/FluentNHibernate/Automapping/AutoSubClassPart.cs b/src/FluentNHibernate/Automapping/AutoSubClassPart.cs index 69e208b8a..0bdb3ba57 100644 --- a/src/FluentNHibernate/Automapping/AutoSubClassPart.cs +++ b/src/FluentNHibernate/Automapping/AutoSubClassPart.cs @@ -70,7 +70,7 @@ public void SubClass(string discriminatorValue, Action).MakeGenericType(typeof(TSubclass)); var subclass = (AutoSubClassPart)Activator.CreateInstance(genericType, discriminatorValue); - if (action is not null) action(subclass); + action?.Invoke(subclass); providers.Subclasses[typeof(TSubclass)] = subclass; } diff --git a/src/FluentNHibernate/Automapping/Steps/PropertyStep.cs b/src/FluentNHibernate/Automapping/Steps/PropertyStep.cs index c57925f61..bca373f04 100644 --- a/src/FluentNHibernate/Automapping/Steps/PropertyStep.cs +++ b/src/FluentNHibernate/Automapping/Steps/PropertyStep.cs @@ -47,9 +47,8 @@ private bool HasExplicitTypeConvention(Member property) var criteria = new ConcreteAcceptanceCriteria(); var acceptance = c as IConventionAcceptance; - - if (acceptance is not null) - acceptance.Accept(criteria); + + acceptance?.Accept(criteria); var propertyMapping = new PropertyMapping { diff --git a/src/FluentNHibernate/Mapping/ClasslikeMapBase.cs b/src/FluentNHibernate/Mapping/ClasslikeMapBase.cs index 81a8b8bce..fdc50b88d 100644 --- a/src/FluentNHibernate/Mapping/ClasslikeMapBase.cs +++ b/src/FluentNHibernate/Mapping/ClasslikeMapBase.cs @@ -294,7 +294,7 @@ ComponentPart Component(Member member, Action(typeof(T), member); - if (action is not null) action(part); + action?.Invoke(part); providers.Components.Add(part); diff --git a/src/FluentNHibernate/Mapping/CompositeIdentityPart.cs b/src/FluentNHibernate/Mapping/CompositeIdentityPart.cs index 9202156d8..1678e85e3 100644 --- a/src/FluentNHibernate/Mapping/CompositeIdentityPart.cs +++ b/src/FluentNHibernate/Mapping/CompositeIdentityPart.cs @@ -162,8 +162,7 @@ protected virtual CompositeIdentityPart KeyReference(Member member, IEnumerab var keyPart = new KeyManyToOnePart(key); - if (customMapping is not null) - customMapping(keyPart); + customMapping?.Invoke(keyPart); keys.Add(key); @@ -173,10 +172,7 @@ protected virtual CompositeIdentityPart KeyReference(Member member, IEnumerab public virtual CompositeIdentityPart CustomType() { var key = keys.Where(x => x is KeyPropertyMapping).Cast().LastOrDefault(); - if (key is not null) - { - key.Set(x => x.Type, Layer.Defaults, new TypeReference(typeof(CType))); - } + key?.Set(x => x.Type, Layer.Defaults, new TypeReference(typeof(CType))); return this; } diff --git a/src/FluentNHibernate/Mapping/ManyToManyPart.cs b/src/FluentNHibernate/Mapping/ManyToManyPart.cs index 67b9f2912..dd1a06380 100644 --- a/src/FluentNHibernate/Mapping/ManyToManyPart.cs +++ b/src/FluentNHibernate/Mapping/ManyToManyPart.cs @@ -125,8 +125,7 @@ public ManyToManyPart AsTernaryAssociation(string indexColumn, string va manyToManyIndex.Column(indexColumn); manyToManyIndex.Type(indexType); - if (indexAction is not null) - indexAction(manyToManyIndex); + indexAction?.Invoke(manyToManyIndex); ChildKeyColumn(valueColumn); valueType = typeOfValue; @@ -154,8 +153,7 @@ public ManyToManyPart AsTernaryAssociation(Type indexType, string indexC manyToManyIndex.Column(indexColumn); manyToManyIndex.Type(indexType); - if (indexAction is not null) - indexAction(manyToManyIndex); + indexAction?.Invoke(manyToManyIndex); ChildKeyColumn(valueColumn); valueType = typeOfValue; diff --git a/src/FluentNHibernate/Mapping/Member.cs b/src/FluentNHibernate/Mapping/Member.cs index d650adf94..dd292ba83 100644 --- a/src/FluentNHibernate/Mapping/Member.cs +++ b/src/FluentNHibernate/Mapping/Member.cs @@ -290,10 +290,7 @@ public PropertyMember(PropertyInfo member) MethodMember GetMember(MethodInfo method) { - if (method is null) - return null; - - return (MethodMember)method.ToMember(); + return (MethodMember)method?.ToMember(); } public override void SetValue(object target, object value) diff --git a/src/FluentNHibernate/Mapping/ToManyBase.cs b/src/FluentNHibernate/Mapping/ToManyBase.cs index f761ebe32..617dd0da3 100644 --- a/src/FluentNHibernate/Mapping/ToManyBase.cs +++ b/src/FluentNHibernate/Mapping/ToManyBase.cs @@ -405,8 +405,7 @@ private void CreateIndexMapping(Action customIndex) { var indexPart = new IndexPart(typeof(T)); - if (customIndex is not null) - customIndex(indexPart); + customIndex?.Invoke(indexPart); #pragma warning disable 612,618 indexMapping = indexPart.GetIndexMapping(); @@ -418,8 +417,7 @@ private void CreateListIndexMapping(Action customIndex) indexMapping = new IndexMapping(); var builder = new ListIndexPart(indexMapping); - if (customIndex is not null) - customIndex(builder); + customIndex?.Invoke(builder); } /// @@ -445,7 +443,7 @@ public T Element(string columnName) public T Element(string columnName, Action customElementMapping) { Element(columnName); - if (customElementMapping is not null) customElementMapping(elementPart); + customElementMapping?.Invoke(elementPart); return (T)this; } diff --git a/src/FluentNHibernate/MappingModel/ClassBased/ReferenceComponentMapping.cs b/src/FluentNHibernate/MappingModel/ClassBased/ReferenceComponentMapping.cs index 3d4884783..636aa5ff3 100644 --- a/src/FluentNHibernate/MappingModel/ClassBased/ReferenceComponentMapping.cs +++ b/src/FluentNHibernate/MappingModel/ClassBased/ReferenceComponentMapping.cs @@ -31,8 +31,7 @@ public void AcceptVisitor(IMappingModelVisitor visitor) { visitor.ProcessComponent(this); - if (mergedComponent is not null) - mergedComponent.AcceptVisitor(visitor); + mergedComponent?.AcceptVisitor(visitor); } public bool IsSpecified(string name) diff --git a/src/FluentNHibernate/MappingModel/TypeReference.cs b/src/FluentNHibernate/MappingModel/TypeReference.cs index 06a2f0e49..420e0ca69 100644 --- a/src/FluentNHibernate/MappingModel/TypeReference.cs +++ b/src/FluentNHibernate/MappingModel/TypeReference.cs @@ -80,10 +80,7 @@ public bool IsGenericTypeDefinition public Type GetGenericTypeDefinition() { - if (innerType is null) - return null; - - return innerType.GetGenericTypeDefinition(); + return innerType?.GetGenericTypeDefinition(); } public Type GenericTypeDefinition diff --git a/src/FluentNHibernate/Visitors/ComponentReferenceResolutionVisitor.cs b/src/FluentNHibernate/Visitors/ComponentReferenceResolutionVisitor.cs index 8daecf491..d51e410ba 100644 --- a/src/FluentNHibernate/Visitors/ComponentReferenceResolutionVisitor.cs +++ b/src/FluentNHibernate/Visitors/ComponentReferenceResolutionVisitor.cs @@ -29,10 +29,7 @@ public ExternalComponentMapping Resolve(ComponentResolutionContext context, IEnu var provider = providers.SingleOrDefault(); - if (provider is null) - return null; - - return provider.GetComponentMapping(); + return provider?.GetComponentMapping(); } } diff --git a/src/FluentNHibernate/Visitors/ConventionVisitor.cs b/src/FluentNHibernate/Visitors/ConventionVisitor.cs index 86502149c..1c71b40dc 100644 --- a/src/FluentNHibernate/Visitors/ConventionVisitor.cs +++ b/src/FluentNHibernate/Visitors/ConventionVisitor.cs @@ -266,8 +266,7 @@ static void Apply(IEnumerable conventions, TInstance inst var criteria = new ConcreteAcceptanceCriteria(); var acceptance = convention as IConventionAcceptance; - if (acceptance is not null) - acceptance.Accept(criteria); + acceptance?.Accept(criteria); if (criteria.Matches(instance)) convention.Apply(instance); diff --git a/src/FluentNHibernate/Visitors/RelationshipPairingVisitor.cs b/src/FluentNHibernate/Visitors/RelationshipPairingVisitor.cs index 92b3bd891..560730a5b 100644 --- a/src/FluentNHibernate/Visitors/RelationshipPairingVisitor.cs +++ b/src/FluentNHibernate/Visitors/RelationshipPairingVisitor.cs @@ -193,10 +193,7 @@ static CollectionMapping FindAlternative(IEnumerable rs, Coll .OrderBy(x => x.Differences) .FirstOrDefault(); - if (alternative is null) - return null; - - return alternative.Collection; + return alternative?.Collection; } static bool AnyHaveSameLikeness(IEnumerable likenesses, LikenessContainer current)