Skip to content

Commit 27c4785

Browse files
authored
Use null propagation where possible (#651)
+semver:patch
1 parent ba6f6d0 commit 27c4785

13 files changed

+19
-44
lines changed

src/FluentNHibernate/Automapping/AutoMapping.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ public AutoJoinedSubClassPart<TSubclass> JoinedSubClass<TSubclass>(string keyCol
133133
var genericType = typeof(AutoJoinedSubClassPart<>).MakeGenericType(typeof(TSubclass));
134134
var joinedclass = (AutoJoinedSubClassPart<TSubclass>)Activator.CreateInstance(genericType, keyColumn);
135135

136-
if (action is not null)
137-
action(joinedclass);
136+
action?.Invoke(joinedclass);
138137

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

@@ -167,8 +166,7 @@ public AutoSubClassPart<TSubclass> SubClass<TSubclass>(object discriminatorValue
167166
var genericType = typeof(AutoSubClassPart<>).MakeGenericType(typeof(TSubclass));
168167
var subclass = (AutoSubClassPart<TSubclass>)Activator.CreateInstance(genericType, null, discriminatorValue);
169168

170-
if (action is not null)
171-
action(subclass);
169+
action?.Invoke(subclass);
172170

173171
// remove any mappings for the same type, then re-add
174172
providers.Subclasses[typeof(TSubclass)] = subclass;

src/FluentNHibernate/Automapping/AutoSubClassPart.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void SubClass<TSubclass>(string discriminatorValue, Action<AutoSubClassPa
7070
var genericType = typeof(AutoSubClassPart<>).MakeGenericType(typeof(TSubclass));
7171
var subclass = (AutoSubClassPart<TSubclass>)Activator.CreateInstance(genericType, discriminatorValue);
7272

73-
if (action is not null) action(subclass);
73+
action?.Invoke(subclass);
7474

7575
providers.Subclasses[typeof(TSubclass)] = subclass;
7676
}

src/FluentNHibernate/Automapping/Steps/PropertyStep.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ private bool HasExplicitTypeConvention(Member property)
4747

4848
var criteria = new ConcreteAcceptanceCriteria<IPropertyInspector>();
4949
var acceptance = c as IConventionAcceptance<IPropertyInspector>;
50-
51-
if (acceptance is not null)
52-
acceptance.Accept(criteria);
50+
51+
acceptance?.Accept(criteria);
5352

5453
var propertyMapping = new PropertyMapping
5554
{

src/FluentNHibernate/Mapping/ClasslikeMapBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ ComponentPart<TComponent> Component<TComponent>(Member member, Action<ComponentP
294294

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

297-
if (action is not null) action(part);
297+
action?.Invoke(part);
298298

299299
providers.Components.Add(part);
300300

src/FluentNHibernate/Mapping/CompositeIdentityPart.cs

+2-6
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ protected virtual CompositeIdentityPart<T> KeyReference(Member member, IEnumerab
162162

163163
var keyPart = new KeyManyToOnePart(key);
164164

165-
if (customMapping is not null)
166-
customMapping(keyPart);
165+
customMapping?.Invoke(keyPart);
167166

168167
keys.Add(key);
169168

@@ -173,10 +172,7 @@ protected virtual CompositeIdentityPart<T> KeyReference(Member member, IEnumerab
173172
public virtual CompositeIdentityPart<T> CustomType<CType>()
174173
{
175174
var key = keys.Where(x => x is KeyPropertyMapping).Cast<KeyPropertyMapping>().LastOrDefault();
176-
if (key is not null)
177-
{
178-
key.Set(x => x.Type, Layer.Defaults, new TypeReference(typeof(CType)));
179-
}
175+
key?.Set(x => x.Type, Layer.Defaults, new TypeReference(typeof(CType)));
180176
return this;
181177
}
182178

src/FluentNHibernate/Mapping/ManyToManyPart.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ public ManyToManyPart<TChild> AsTernaryAssociation(string indexColumn, string va
125125
manyToManyIndex.Column(indexColumn);
126126
manyToManyIndex.Type(indexType);
127127

128-
if (indexAction is not null)
129-
indexAction(manyToManyIndex);
128+
indexAction?.Invoke(manyToManyIndex);
130129

131130
ChildKeyColumn(valueColumn);
132131
valueType = typeOfValue;
@@ -154,8 +153,7 @@ public ManyToManyPart<TChild> AsTernaryAssociation(Type indexType, string indexC
154153
manyToManyIndex.Column(indexColumn);
155154
manyToManyIndex.Type(indexType);
156155

157-
if (indexAction is not null)
158-
indexAction(manyToManyIndex);
156+
indexAction?.Invoke(manyToManyIndex);
159157

160158
ChildKeyColumn(valueColumn);
161159
valueType = typeOfValue;

src/FluentNHibernate/Mapping/Member.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,7 @@ public PropertyMember(PropertyInfo member)
290290

291291
MethodMember GetMember(MethodInfo method)
292292
{
293-
if (method is null)
294-
return null;
295-
296-
return (MethodMember)method.ToMember();
293+
return (MethodMember)method?.ToMember();
297294
}
298295

299296
public override void SetValue(object target, object value)

src/FluentNHibernate/Mapping/ToManyBase.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,7 @@ private void CreateIndexMapping(Action<IndexPart> customIndex)
405405
{
406406
var indexPart = new IndexPart(typeof(T));
407407

408-
if (customIndex is not null)
409-
customIndex(indexPart);
408+
customIndex?.Invoke(indexPart);
410409

411410
#pragma warning disable 612,618
412411
indexMapping = indexPart.GetIndexMapping();
@@ -418,8 +417,7 @@ private void CreateListIndexMapping(Action<ListIndexPart> customIndex)
418417
indexMapping = new IndexMapping();
419418
var builder = new ListIndexPart(indexMapping);
420419

421-
if (customIndex is not null)
422-
customIndex(builder);
420+
customIndex?.Invoke(builder);
423421
}
424422

425423
/// <summary>
@@ -445,7 +443,7 @@ public T Element(string columnName)
445443
public T Element(string columnName, Action<ElementPart> customElementMapping)
446444
{
447445
Element(columnName);
448-
if (customElementMapping is not null) customElementMapping(elementPart);
446+
customElementMapping?.Invoke(elementPart);
449447
return (T)this;
450448
}
451449

src/FluentNHibernate/MappingModel/ClassBased/ReferenceComponentMapping.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ public void AcceptVisitor(IMappingModelVisitor visitor)
3131
{
3232
visitor.ProcessComponent(this);
3333

34-
if (mergedComponent is not null)
35-
mergedComponent.AcceptVisitor(visitor);
34+
mergedComponent?.AcceptVisitor(visitor);
3635
}
3736

3837
public bool IsSpecified(string name)

src/FluentNHibernate/MappingModel/TypeReference.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,7 @@ public bool IsGenericTypeDefinition
8080

8181
public Type GetGenericTypeDefinition()
8282
{
83-
if (innerType is null)
84-
return null;
85-
86-
return innerType.GetGenericTypeDefinition();
83+
return innerType?.GetGenericTypeDefinition();
8784
}
8885

8986
public Type GenericTypeDefinition

src/FluentNHibernate/Visitors/ComponentReferenceResolutionVisitor.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ public ExternalComponentMapping Resolve(ComponentResolutionContext context, IEnu
2929

3030
var provider = providers.SingleOrDefault();
3131

32-
if (provider is null)
33-
return null;
34-
35-
return provider.GetComponentMapping();
32+
return provider?.GetComponentMapping();
3633
}
3734
}
3835

src/FluentNHibernate/Visitors/ConventionVisitor.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,7 @@ static void Apply<TInspector, TInstance>(IEnumerable conventions, TInstance inst
266266
var criteria = new ConcreteAcceptanceCriteria<TInspector>();
267267
var acceptance = convention as IConventionAcceptance<TInspector>;
268268

269-
if (acceptance is not null)
270-
acceptance.Accept(criteria);
269+
acceptance?.Accept(criteria);
271270

272271
if (criteria.Matches(instance))
273272
convention.Apply(instance);

src/FluentNHibernate/Visitors/RelationshipPairingVisitor.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,7 @@ static CollectionMapping FindAlternative(IEnumerable<CollectionMapping> rs, Coll
193193
.OrderBy(x => x.Differences)
194194
.FirstOrDefault();
195195

196-
if (alternative is null)
197-
return null;
198-
199-
return alternative.Collection;
196+
return alternative?.Collection;
200197
}
201198

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

0 commit comments

Comments
 (0)