Skip to content

Commit

Permalink
Refactored code.
Browse files Browse the repository at this point in the history
  • Loading branch information
mahara committed Jan 11, 2024
1 parent 9368d70 commit 549bc51
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private void OnSampleListCollectionChanged(object? sender, NotifyCollectionChang
if (e.Action == NotifyCollectionChangedAction.Add)
{
var newItems = e.NewItems;
if (newItems != null)
if (newItems is not null)
{
foreach (SampleItem item in newItems)
{
Expand All @@ -35,7 +35,7 @@ private void OnSampleListCollectionChanged(object? sender, NotifyCollectionChang
else if (e.Action == NotifyCollectionChangedAction.Remove)
{
var oldItems = e.OldItems;
if (oldItems != null)
if (oldItems is not null)
{
foreach (SampleItem item in oldItems)
{
Expand Down
22 changes: 12 additions & 10 deletions src/NHibernate.ObservableCollections.DemoApp/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public MainWindow()

private bool TryGetSelection(out object? selectedContainer, out SampleItem? selectedItem)
{
if (SampleSetBox.SelectedItem != null)
if (SampleSetBox.SelectedItem is not null)
{
selectedContainer = _sampleSetContainer;
selectedItem = (SampleItem) SampleSetBox.SelectedItem;
return true;
}

if (SampleListBox.SelectedItem != null)
if (SampleListBox.SelectedItem is not null)
{
selectedContainer = _sampleListContainer;
selectedItem = (SampleItem) SampleListBox.SelectedItem;
Expand All @@ -39,7 +39,7 @@ private bool TryGetSelection(out object? selectedContainer, out SampleItem? sele
private void ItemSelectionChange(object s, RoutedEventArgs e)
{
var sender = (ListBox) s;
if (sender.SelectedItem == null)
if (sender.SelectedItem is null)
{
return;
}
Expand All @@ -51,8 +51,10 @@ private void ItemSelectionChange(object s, RoutedEventArgs e)

private void AddButton_Click(object sender, RoutedEventArgs e)
{
var newItem = new SampleItem();
newItem.Name = editItemBox.Text; // add new item from value in text box
var newItem = new SampleItem
{
Name = editItemBox.Text // add new item from value in text box
};
if (_sampleSetContainer.SampleSet.Add(newItem))
{
newItem.ParentSetContainer = _sampleSetContainer;
Expand All @@ -65,7 +67,7 @@ private void AddButton_Click(object sender, RoutedEventArgs e)

private void CopyToButton_Click(object sender, RoutedEventArgs e)
{
if (SampleSetBox.SelectedItem == null)
if (SampleSetBox.SelectedItem is null)
{
return;
}
Expand All @@ -88,7 +90,7 @@ private void DeleteButton_Click(object sender, RoutedEventArgs e)
if (selectedContainer is SampleSetContainer)
{
// then delete item from set
if (selectedItem != null && _sampleSetContainer.SampleSet.Remove(selectedItem))
if (selectedItem is not null && _sampleSetContainer.SampleSet.Remove(selectedItem))
{
selectedItem.ParentSetContainer = null;

Expand All @@ -107,9 +109,9 @@ private void DeleteButton_Click(object sender, RoutedEventArgs e)
else
{
// delete item from list
if (selectedItem != null && _sampleListContainer.SampleList.Remove(selectedItem))
if (selectedItem is not null && _sampleListContainer.SampleList.Remove(selectedItem))
{
if (selectedContainer != null)
if (selectedContainer is not null)
{
using var dbMgr = new NHibernateDatabaseManager();

Expand Down Expand Up @@ -145,7 +147,7 @@ private void UpdateButton_Click(object sender, RoutedEventArgs e)
return;
}

if (selectedItem != null)
if (selectedItem is not null)
{
selectedItem.Name = editItemBox.Text; // update selected item from value in text box

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

<PropertyGroup>
<TargetFrameworks>$(WinExeTargetFrameworks)</TargetFrameworks>
<!--<TargetFrameworks>net8.0-windows</TargetFrameworks>-->
<!--<TargetFrameworks>net48</TargetFrameworks>-->

<UseWPF>true</UseWPF>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace NHibernate.ObservableCollections.Helpers.BidirectionalAssociations
/// <author>Adrian Alexander</author>
/// <remarks>
/// REFERENCES:
/// - https://happynomad121.blogspot.com/2008/05/revisiting-bidirectional-assoc-helpers.html
/// - <see href="https://happynomad121.blogspot.com/2008/05/revisiting-bidirectional-assoc-helpers.html" />
/// </remarks>
public sealed class ManyToManyAssociationSync<T>
{
Expand Down Expand Up @@ -78,13 +78,10 @@ public void UpdateOtherSide(object sender, NotifyCollectionChangedEventArgs e)

private ICollection<T>? GetOtherSidesCollection(object otherSide)
{
if (_otherSideProperty == null)
{
_otherSideProperty = otherSide.GetType().GetProperty(_otherSidePropertyName);
}
_otherSideProperty ??= otherSide.GetType().GetProperty(_otherSidePropertyName);

var otherSideProperty = _otherSideProperty;
if (otherSideProperty != null)
if (otherSideProperty is not null)
{
return (ICollection<T>?) otherSideProperty.GetValue(otherSide, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace NHibernate.ObservableCollections.Helpers.BidirectionalAssociations
/// <author>Adrian Alexander</author>
/// <remarks>
/// REFERENCES:
/// - https://happynomad121.blogspot.com/2008/05/revisiting-bidirectional-assoc-helpers.html
/// - <see href="https://happynomad121.blogspot.com/2008/05/revisiting-bidirectional-assoc-helpers.html" />
/// </remarks>
public sealed class OneToManyAssociationSync
{
Expand All @@ -31,7 +31,7 @@ public OneToManyAssociationSync(object thisOneSide, string manyToOnePropertyName
/// </summary>
public static void UpdateOneSide<T>(T thisManySide, object oldOneSide, object newOneSide, string oneToManyPropertyName)
{
if (oldOneSide != null && oldOneSide != newOneSide)
if (oldOneSide is not null && oldOneSide != newOneSide)
{
var oldCollection = ReflectionUtil.NavigateToManySide<T>(oldOneSide, oneToManyPropertyName);
if (oldCollection.Contains(thisManySide))
Expand All @@ -48,7 +48,7 @@ public static void UpdateOneSide<T>(T thisManySide, object oldOneSide, object ne
}
}

if (newOneSide != null)
if (newOneSide is not null)
{
var newCollection =
ReflectionUtil.NavigateToManySide<T>(newOneSide, oneToManyPropertyName);
Expand Down Expand Up @@ -77,7 +77,7 @@ public void UpdateManySide(object? sender, NotifyCollectionChangedEventArgs e)
// addingToManySide: the item that was just added to this one-side's collection
foreach (var addingToManySide in e.NewItems!)
{
if (addingToManySide != null && NavigateManyToOne(addingToManySide) != _thisOneSide)
if (addingToManySide is not null && NavigateManyToOne(addingToManySide) != _thisOneSide)
{
SetManyToOne(addingToManySide, _thisOneSide);
}
Expand All @@ -98,7 +98,7 @@ public void UpdateManySide(object? sender, NotifyCollectionChangedEventArgs e)

private PropertyInfo GetManyToOneProperty(System.Type manySideType)
{
if (_manyToOneProperty == null)
if (_manyToOneProperty is null)
{
var pi = manySideType.GetProperty(_manyToOnePropertyName)!;
_manyToOneProperty = pi.DeclaringType!.GetProperty(_manyToOnePropertyName)!;
Expand Down
22 changes: 11 additions & 11 deletions src/NHibernate.ObservableCollections.Helpers/CollectionsUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,38 @@ public static bool ContainsAny<T>(ICollection<T> collection, ICollection<T> valu
/// </summary>
/// <typeparam name="T">The collections' item type</typeparam>
/// <returns>True if both collections contain the same elements (in any order)</returns>
public static bool Equals<T>(ICollection<T> one, ICollection<T> two)
public static bool Equals<T>(ICollection<T> collectionA, ICollection<T> collectionB)
{
if (one == null || two == null)
if (collectionA is null || collectionB is null)
{
// special case
return one == two;
return collectionA == collectionB;
}

if (ReferenceEquals(one, two))
if (ReferenceEquals(collectionA, collectionB))
{
// same objects
return true;
}

if (one.Count != two.Count)
if (collectionA.Count != collectionB.Count)
{
return false;
}

IList<T> listOne = new List<T>(one);
IList<T> listTwo = new List<T>(two);
var listA = new List<T>(collectionA);
var listB = new List<T>(collectionB);

// make sure that every object in one is also in two
for (var i = 0; i < listOne.Count; i++)
for (var i = 0; i < listA.Count; i++)
{
var obj = listOne[i];
if (!listTwo.Contains(obj))
var obj = listA[i];
if (!listB.Contains(obj))
{
return false;
}

listTwo.Remove(obj);
listB.Remove(obj);
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<PropertyGroup>
<TargetFrameworks>$(WinLibTargetFrameworks)</TargetFrameworks>

<ImplicitUsings>disable</ImplicitUsings>
<UseWPF>true</UseWPF>
</PropertyGroup>

Expand Down
30 changes: 15 additions & 15 deletions src/NHibernate.ObservableCollections.Helpers/ReflectionUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ namespace NHibernate.ObservableCollections.Helpers

public static class ReflectionUtil
{
private static MethodInfo? isInitialized;
private static MethodInfo? isInitializedMethod;

public static bool IsInitialized<T>(ICollection<T> newCollection)
public static bool IsInitialized<T>(ICollection<T> collection)
{
if (isInitialized == null)
if (isInitializedMethod is null)
{
var t = System.Type.GetType("NHibernate.NHibernateUtil, NHibernate");
if (t != null)
var type = System.Type.GetType("NHibernate.NHibernateUtil, NHibernate");
if (type is not null)
{
isInitialized = t.GetMethod("IsInitialized", BindingFlags.Static | BindingFlags.Public);
isInitializedMethod = type.GetMethod("IsInitialized", BindingFlags.Static | BindingFlags.Public);
}
}

if (isInitialized != null)
if (isInitializedMethod is not null)
{
// true if the NHibernate assembly is present
return (bool) isInitialized.Invoke(
return (bool) isInitializedMethod.Invoke(
null,
new object[]
{
newCollection
collection
})!;
}

Expand All @@ -33,16 +33,16 @@ public static bool IsInitialized<T>(ICollection<T> newCollection)

public static ICollection<T> NavigateToManySide<T>(object start, string propertyName)
{
var bf = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
var pi = start.GetType().GetProperty(propertyName, bf)!
.DeclaringType!.GetProperty(propertyName, bf)!;
return (ICollection<T>) pi.GetValue(start, null)!;
var flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
var property = start.GetType().GetProperty(propertyName, flags)!
.DeclaringType!.GetProperty(propertyName, flags)!;
return (ICollection<T>) property.GetValue(start, null)!;
}

public static object NavigateToOneSide(object start, string propertyName)
{
var pi = start.GetType().GetProperty(propertyName)!;
return pi.GetValue(start, null)!;
var property = start.GetType().GetProperty(propertyName)!;
return property.GetValue(start, null)!;
}
}
}
18 changes: 9 additions & 9 deletions src/NHibernate.ObservableCollections/ObservableCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ protected virtual void InsertItemsRange(int index, IEnumerable<T> collection)
throw new ArgumentOutOfRangeException(nameof(index));
}

if (collection == null)
if (collection is null)
{
throw new ArgumentNullException(nameof(collection));
}
Expand Down Expand Up @@ -275,7 +275,7 @@ protected virtual void InsertItemsRange(int index, IEnumerable<T> collection)

protected virtual void RemoveItemsRange(IEnumerable<T> collection)
{
if (collection == null)
if (collection is null)
{
throw new ArgumentNullException(nameof(collection));
}
Expand Down Expand Up @@ -320,7 +320,7 @@ protected virtual void RemoveItemsRange(IEnumerable<T> collection)

base.RemoveItem(index);

if (lastIndex == index && lastCluster != null)
if (lastIndex == index && lastCluster is not null)
{
lastCluster.Add(item);
}
Expand Down Expand Up @@ -386,15 +386,15 @@ protected virtual IDisposable DeferEvents()
/// </remarks>
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (_deferredEventsCollection != null)
if (_deferredEventsCollection is not null)
{
_deferredEventsCollection.Add(e);

return;
}

var handler = CollectionChanged;
if (handler != null)
if (handler is not null)
{
// Not calling BlockReentrancy() here to avoid the SimpleMonitor allocation.
_blockReentrancyCount++;
Expand Down Expand Up @@ -485,7 +485,7 @@ private void OnSerializing(StreamingContext context)
[OnDeserialized]
private void OnDeserialized(StreamingContext context)
{
if (_monitor != null)
if (_monitor is not null)
{
_blockReentrancyCount = _monitor._busyCount;
_monitor._collection = this;
Expand Down Expand Up @@ -550,7 +550,7 @@ private sealed class SimpleMonitor : IDisposable

public SimpleMonitor(ObservableCollection<T> collection)
{
Debug.Assert(collection != null);
Debug.Assert(collection is not null);

_collection = collection!;
}
Expand All @@ -567,8 +567,8 @@ private sealed class DeferredEventsCollection : List<NotifyCollectionChangedEven

public DeferredEventsCollection(ObservableCollection<T> collection)
{
Debug.Assert(collection != null);
Debug.Assert(collection!._deferredEventsCollection == null);
Debug.Assert(collection is not null);
Debug.Assert(collection!._deferredEventsCollection is null);

_collection = collection;
_collection._deferredEventsCollection = this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public PersistentObservableBag(ISessionImplementor session) :
public PersistentObservableBag(ISessionImplementor session, ICollection<T> collection) :
base(session, collection)
{
if (collection != null)
if (collection is not null)
{
((INotifyCollectionChanged) collection).CollectionChanged += OnCollectionChanged;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public PersistentObservableCollection(ISessionImplementor session) :
public PersistentObservableCollection(ISessionImplementor session, IList<T> collection) :
base(session, collection)
{
if (collection != null)
if (collection is not null)
{
((INotifyCollectionChanged) collection).CollectionChanged += OnCollectionChanged;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public PersistentObservableSet(ISessionImplementor session) :
public PersistentObservableSet(ISessionImplementor session, ISet<T> collection) :
base(session, collection)
{
if (collection != null)
if (collection is not null)
{
((INotifyCollectionChanged) collection).CollectionChanged += OnCollectionChanged;
}
Expand Down

0 comments on commit 549bc51

Please sign in to comment.