Skip to content

Commit

Permalink
Merge pull request #30 from BoBoBaSs84/feature/remove-boxing-unboxing
Browse files Browse the repository at this point in the history
feat: remove boxing unboxing
  • Loading branch information
BoBoBaSs84 authored Nov 12, 2023
2 parents 814363a + ba6e376 commit b3f0cad
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<VersionMajor>1</VersionMajor>
<VersionMinor>5</VersionMinor>
<VersionMinor>6</VersionMinor>
<VersionPatch>0</VersionPatch>
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
<VersionSuffix Condition="$(Configuration.Equals('Debug'))">Development</VersionSuffix>
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public sealed class TestClass : NotifyPropertyBase

If the property is changed, the `PropertyChangingEventHandler` is triggered before the value changes and the `PropertyChangedEventHandler` is triggered after the value has changed.

- The `PropertyChangingEventHandler` contains via the `CollectionChangingEventArgs` the name of the property that is changing and the old value.
- The `PropertyChangedEventHandler` contains via the `PropertyChangedEventArgs` the name of the property that has changed and the new value.
- The `PropertyChangingEventHandler` contains via the `CollectionChangingEventArgs` the name of the property that is changing and when casted to `CollectionChangingEventArgs<T>` the old value.
- The `PropertyChangedEventHandler` contains via the `PropertyChangedEventArgs` the name of the property that has changed and when casted to `PropertyChangedEventArgs<T>` the new value.

Further implementation possibilities can be achieved using the `INotifyPropertyChanged` and `INotifyPropertyChanging` interfaces.

Expand Down
27 changes: 19 additions & 8 deletions src/BB84.Notifications/Components/PropertyChangedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,39 @@ namespace BB84.Notifications.Components;
/// <summary>
/// The property changed event args class.
/// </summary>
public sealed class PropertyChangedEventArgs : EventArgs
public class PropertyChangedEventArgs : EventArgs
{
/// <summary>
/// Initializes a instance of the property changed event args class.
/// </summary>
/// <param name="name">The name of the property that is changed.</param>
/// <param name="value">The value of the property that is changed.</param>
public PropertyChangedEventArgs(string name, object? value = null)
{
Name = name;
Value = value;
}
public PropertyChangedEventArgs(string name)
=> Name = name;

/// <summary>
/// The name of the property that is changed.
/// </summary>
public string Name { get; }
}

/// <summary>
/// The property changed event args class.
/// </summary>
/// <typeparam name="T">The type to work with.</typeparam>
public sealed class PropertyChangedEventArgs<T> : PropertyChangedEventArgs
{
/// <summary>
/// Initializes a instance of the property changed event args class.
/// </summary>
/// <param name="name">The name of the property that is changed.</param>
/// <param name="value">The value of the property that is changed.</param>
public PropertyChangedEventArgs(string name, T value) : base(name)
=> Value = value;

/// <summary>
/// The value of the property that is changed.
/// </summary>
public object? Value { get; }
public T Value { get; }
}

/// <summary>
Expand Down
27 changes: 19 additions & 8 deletions src/BB84.Notifications/Components/PropertyChangingEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,39 @@ namespace BB84.Notifications.Components;
/// <summary>
/// The property changing event args class.
/// </summary>
public sealed class PropertyChangingEventArgs : EventArgs
public class PropertyChangingEventArgs : EventArgs
{
/// <summary>
/// Initializes a instance of the property changing event args class.
/// </summary>
/// <param name="name">The name of the property that is changing.</param>
/// <param name="value">The value of the property that is changing.</param>
public PropertyChangingEventArgs(string name, object? value = null)
{
Name = name;
Value = value;
}
public PropertyChangingEventArgs(string name)
=> Name = name;

/// <summary>
/// The name of the property that is changing.
/// </summary>
public string Name { get; }
}

/// <summary>
/// The property changing event args class.
/// </summary>
/// <typeparam name="T">The type to work with.</typeparam>
public sealed class PropertyChangingEventArgs<T> : PropertyChangingEventArgs
{
/// <summary>
/// Initializes a instance of the property changing event args class.
/// </summary>
/// <param name="name">The name of the property that is changing.</param>
/// <param name="value">The value of the property that is changing.</param>
public PropertyChangingEventArgs(string name, T value) : base(name)
=> Value = value;

/// <summary>
/// The value of the property that is changing.
/// </summary>
public object? Value { get; }
public T Value { get; }
}

/// <summary>
Expand Down
35 changes: 22 additions & 13 deletions src/BB84.Notifications/NotifyPropertyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public abstract class NotifyPropertyBase : INotifyPropertyBase
/// <summary>
/// Sets a new value for a property and notifies about the change.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T">The type to work with.</typeparam>
/// <param name="fieldValue">The referenced field.</param>
/// <param name="newValue">The new value for the property.</param>
/// <param name="propertyName">The name of the calling property.</param>
Expand All @@ -45,24 +45,33 @@ protected void SetProperty<T>(ref T fieldValue, T newValue, [CallerMemberName] s
/// <summary>
/// Raises the <see cref="PropertyChanged"/> event.
/// </summary>
/// <remarks>
/// The calling member's name will be used as the parameter.
/// </remarks>
/// <param name="propertyName">The name of the calling property.</param>
private void RaisePropertyChanged(string propertyName)
=> PropertyChanged?.Invoke(this, new(propertyName));

/// <summary>
/// Raises the <see cref="PropertyChanged"/> event.
/// </summary>
/// <typeparam name="T">The type to work with.</typeparam>
/// <param name="propertyName">The name of the calling property.</param>
/// <param name="value">The value of the calling property.</param>
private void RaisePropertyChanged(string propertyName, object? value = null)
=> PropertyChanged?.Invoke(this, new(propertyName, value));
private void RaisePropertyChanged<T>(string propertyName, T value)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs<T>(propertyName, value));

/// <summary>
/// Raises the <see cref="PropertyChanging"/> event.
/// </summary>
/// <param name="propertyName">The name of the calling property.</param>
private void RaisePropertyChanging(string propertyName)
=> PropertyChanging?.Invoke(this, new(propertyName));

/// <summary>
/// Raises the <see cref="PropertyChanging"/> event.
/// </summary>
/// <remarks>
/// The calling member's name will be used as the parameter.
/// </remarks>
/// <param name="propertyName">The name of the calling property.</param>
/// <param name="value">The value of the calling property.</param>
private void RaisePropertyChanging(string propertyName, object? value = null)
=> PropertyChanging?.Invoke(this, new(propertyName, value));
private void RaisePropertyChanging<T>(string propertyName, T value)
=> PropertyChanging?.Invoke(this, new PropertyChangingEventArgs<T>(propertyName, value));

/// <summary>
/// Raises the <see cref="PropertyChanged"/> event for all properties that are
Expand All @@ -79,7 +88,7 @@ private void RaiseChangedAttribute(string propertyName)
propertyInfo.GetCustomAttribute(typeof(NotifyChangedAttribute), false) as NotifyChangedAttribute;

if (attribute is not null)
foreach (var property in attribute.Properties)
foreach (string property in attribute.Properties)
RaisePropertyChanged(property);
}
}
Expand All @@ -99,7 +108,7 @@ private void RaiseChangingAttribute(string propertyName)
propertyInfo.GetCustomAttribute(typeof(NotifyChangingAttribute), false) as NotifyChangingAttribute;

if (attribute is not null)
foreach (var property in attribute.Properties)
foreach (string property in attribute.Properties)
RaisePropertyChanging(property);
}
}
Expand Down
29 changes: 25 additions & 4 deletions tests/BB84.NotificationsTests/NotifyPropertyBaseTests.Changed.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
namespace BB84.NotificationsTests;
using BB84.Notifications.Components;

namespace BB84.NotificationsTests;

public sealed partial class NotifyPropertyBaseTests
{
[TestMethod]
public void Changed()
{
string changedProperty = string.Empty;
int changedValue = default;
TestClass testClass = new();
testClass.PropertyChanged += (sender, e) => { changedValue = (int)e.Value!; changedProperty = e.Name; };
testClass.PropertyChanged += (sender, e) => changedProperty = e.Name;

testClass.Property = 1;

Assert.AreEqual(nameof(testClass.Property), changedProperty);
Assert.AreEqual(1, changedValue);
Assert.AreEqual(1, testClass.Property);
}

[TestMethod]
public void ChangedWithValue()
{
string propertyName = string.Empty;
int propertyValue = default;
TestClass testClass = new();
testClass.PropertyChanged += (sender, e) =>
{
if (e is PropertyChangedEventArgs<int> iArgs)
propertyValue = iArgs.Value;

propertyName = e.Name;
};

testClass.Property = 1;

Assert.AreEqual(nameof(testClass.Property), propertyName);
Assert.AreEqual(1, propertyValue);
Assert.AreEqual(1, testClass.Property);
}
}
29 changes: 25 additions & 4 deletions tests/BB84.NotificationsTests/NotifyPropertyBaseTests.Changing.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
namespace BB84.NotificationsTests;
using BB84.Notifications.Components;

namespace BB84.NotificationsTests;

public sealed partial class NotifyPropertyBaseTests
{
[TestMethod]
public void Changing()
{
string changingProperty = string.Empty;
int changingValue = default;
TestClass testClass = new() { Property = 1 };
testClass.PropertyChanging += (sender, e) => { changingValue = (int)e.Value!; changingProperty = e.Name; };
testClass.PropertyChanging += (sender, e) => changingProperty = e.Name;

testClass.Property = 2;

Assert.AreEqual(nameof(testClass.Property), changingProperty);
Assert.AreEqual(1, changingValue);
Assert.AreNotEqual(1, testClass.Property);
}

[TestMethod]
public void ChangingWithValue()
{
string propertyName = string.Empty;
int propertyValue = default;
TestClass testClass = new() { Property = 1 };
testClass.PropertyChanging += (sender, e) =>
{
if (e is PropertyChangingEventArgs<int> intEvent)
propertyValue = intEvent.Value;

propertyName = e.Name;
};

testClass.Property = 2;

Assert.AreEqual(nameof(testClass.Property), propertyName);
Assert.AreEqual(1, propertyValue);
Assert.AreEqual(2, testClass.Property);
}
}

0 comments on commit b3f0cad

Please sign in to comment.