Skip to content

Commit

Permalink
Use null propagation where possible (#651)
Browse files Browse the repository at this point in the history
+semver:patch
  • Loading branch information
hazzik authored Mar 11, 2024
1 parent ba6f6d0 commit 27c4785
Show file tree
Hide file tree
Showing 13 changed files with 19 additions and 44 deletions.
6 changes: 2 additions & 4 deletions src/FluentNHibernate/Automapping/AutoMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ public AutoJoinedSubClassPart<TSubclass> JoinedSubClass<TSubclass>(string keyCol
var genericType = typeof(AutoJoinedSubClassPart<>).MakeGenericType(typeof(TSubclass));
var joinedclass = (AutoJoinedSubClassPart<TSubclass>)Activator.CreateInstance(genericType, keyColumn);

if (action is not null)
action(joinedclass);
action?.Invoke(joinedclass);

providers.Subclasses[typeof(TSubclass)] = joinedclass;

Expand Down Expand Up @@ -167,8 +166,7 @@ public AutoSubClassPart<TSubclass> SubClass<TSubclass>(object discriminatorValue
var genericType = typeof(AutoSubClassPart<>).MakeGenericType(typeof(TSubclass));
var subclass = (AutoSubClassPart<TSubclass>)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;
Expand Down
2 changes: 1 addition & 1 deletion src/FluentNHibernate/Automapping/AutoSubClassPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void SubClass<TSubclass>(string discriminatorValue, Action<AutoSubClassPa
var genericType = typeof(AutoSubClassPart<>).MakeGenericType(typeof(TSubclass));
var subclass = (AutoSubClassPart<TSubclass>)Activator.CreateInstance(genericType, discriminatorValue);

if (action is not null) action(subclass);
action?.Invoke(subclass);

providers.Subclasses[typeof(TSubclass)] = subclass;
}
Expand Down
5 changes: 2 additions & 3 deletions src/FluentNHibernate/Automapping/Steps/PropertyStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ private bool HasExplicitTypeConvention(Member property)

var criteria = new ConcreteAcceptanceCriteria<IPropertyInspector>();
var acceptance = c as IConventionAcceptance<IPropertyInspector>;

if (acceptance is not null)
acceptance.Accept(criteria);

acceptance?.Accept(criteria);

var propertyMapping = new PropertyMapping
{
Expand Down
2 changes: 1 addition & 1 deletion src/FluentNHibernate/Mapping/ClasslikeMapBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ ComponentPart<TComponent> Component<TComponent>(Member member, Action<ComponentP

var part = new ComponentPart<TComponent>(typeof(T), member);

if (action is not null) action(part);
action?.Invoke(part);

providers.Components.Add(part);

Expand Down
8 changes: 2 additions & 6 deletions src/FluentNHibernate/Mapping/CompositeIdentityPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ protected virtual CompositeIdentityPart<T> KeyReference(Member member, IEnumerab

var keyPart = new KeyManyToOnePart(key);

if (customMapping is not null)
customMapping(keyPart);
customMapping?.Invoke(keyPart);

keys.Add(key);

Expand All @@ -173,10 +172,7 @@ protected virtual CompositeIdentityPart<T> KeyReference(Member member, IEnumerab
public virtual CompositeIdentityPart<T> CustomType<CType>()
{
var key = keys.Where(x => x is KeyPropertyMapping).Cast<KeyPropertyMapping>().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;
}

Expand Down
6 changes: 2 additions & 4 deletions src/FluentNHibernate/Mapping/ManyToManyPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ public ManyToManyPart<TChild> 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;
Expand Down Expand Up @@ -154,8 +153,7 @@ public ManyToManyPart<TChild> 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;
Expand Down
5 changes: 1 addition & 4 deletions src/FluentNHibernate/Mapping/Member.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions src/FluentNHibernate/Mapping/ToManyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,7 @@ private void CreateIndexMapping(Action<IndexPart> 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();
Expand All @@ -418,8 +417,7 @@ private void CreateListIndexMapping(Action<ListIndexPart> customIndex)
indexMapping = new IndexMapping();
var builder = new ListIndexPart(indexMapping);

if (customIndex is not null)
customIndex(builder);
customIndex?.Invoke(builder);
}

/// <summary>
Expand All @@ -445,7 +443,7 @@ public T Element(string columnName)
public T Element(string columnName, Action<ElementPart> customElementMapping)
{
Element(columnName);
if (customElementMapping is not null) customElementMapping(elementPart);
customElementMapping?.Invoke(elementPart);
return (T)this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions src/FluentNHibernate/MappingModel/TypeReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/FluentNHibernate/Visitors/ConventionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,7 @@ static void Apply<TInspector, TInstance>(IEnumerable conventions, TInstance inst
var criteria = new ConcreteAcceptanceCriteria<TInspector>();
var acceptance = convention as IConventionAcceptance<TInspector>;

if (acceptance is not null)
acceptance.Accept(criteria);
acceptance?.Accept(criteria);

if (criteria.Matches(instance))
convention.Apply(instance);
Expand Down
5 changes: 1 addition & 4 deletions src/FluentNHibernate/Visitors/RelationshipPairingVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,7 @@ static CollectionMapping FindAlternative(IEnumerable<CollectionMapping> rs, Coll
.OrderBy(x => x.Differences)
.FirstOrDefault();

if (alternative is null)
return null;

return alternative.Collection;
return alternative?.Collection;
}

static bool AnyHaveSameLikeness(IEnumerable<LikenessContainer> likenesses, LikenessContainer current)
Expand Down

0 comments on commit 27c4785

Please sign in to comment.