Skip to content

Commit

Permalink
simplify writable client property
Browse files Browse the repository at this point in the history
  • Loading branch information
abhipsaMisra committed Aug 30, 2021
1 parent 08b913c commit 5cdb7f7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 28 deletions.
18 changes: 4 additions & 14 deletions iothub/device/src/ClientPropertyCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,19 +237,9 @@ public virtual bool TryGetValue<T>(string componentName, string propertyName, ou
{
// Case 2:
// Check if the retrieved value is a writable property update request
if (dictionaryElement is WritableClientProperty writableClientProperty)
if (dictionaryElement is WritableClientProperty<T> writableClientProperty)
{
object writableClientPropertyValue = writableClientProperty.Value;

// If the object is of type T or can be cast to type T, go ahead and return it.
if (ObjectCastHelpers.TryCast(writableClientPropertyValue, out propertyValue))
{
return true;
}

// If the cannot be cast to <T> directly we need to try to convert it using the serializer.
// If it can be successfully converted, go ahead and return it.
propertyValue = Convention.PayloadSerializer.ConvertFromObject<T>(writableClientPropertyValue);
propertyValue = writableClientProperty.Value;
return true;
}
}
Expand Down Expand Up @@ -373,7 +363,7 @@ internal static ClientPropertyCollection WritablePropertyUpdateRequestsFromTwinC
}
else
{
individualPropertyValue = new WritableClientProperty
individualPropertyValue = new WritableClientProperty<object>
{
Convention = payloadConvention,
Value = payloadConvention.PayloadSerializer.DeserializeToType<object>(JsonConvert.SerializeObject(componentProperty.Value)),
Expand All @@ -386,7 +376,7 @@ internal static ClientPropertyCollection WritablePropertyUpdateRequestsFromTwinC
}
else
{
var writableProperty = new WritableClientProperty
var writableProperty = new WritableClientProperty<object>
{
Convention = payloadConvention,
Value = payloadConvention.PayloadSerializer.DeserializeToType<object>(Newtonsoft.Json.JsonConvert.SerializeObject(propertyValueAsObject)),
Expand Down
14 changes: 2 additions & 12 deletions iothub/device/src/PayloadCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,9 @@ public bool TryGetValue<T>(string key, out T value)
{
// Case 2:
// Check if the retrieved value is a writable property update request
if (retrievedPropertyValue is WritableClientProperty writableClientProperty)
if (retrievedPropertyValue is WritableClientProperty<T> writableClientProperty)
{
object writableClientPropertyValue = writableClientProperty.Value;

// If the object is of type T or can be cast to type T, go ahead and return it.
if (ObjectCastHelpers.TryCast(writableClientPropertyValue, out value))
{
return true;
}

// If the cannot be cast to <T> directly we need to try to convert it using the serializer.
// If it can be successfully converted, go ahead and return it.
value = Convention.PayloadSerializer.ConvertFromObject<T>(writableClientPropertyValue);
value = writableClientProperty.Value;
return true;
}
}
Expand Down
10 changes: 8 additions & 2 deletions iothub/device/src/WritableClientProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ namespace Microsoft.Azure.Devices.Client
/// This type contains a convenience method to format the reported property as per IoT Plug and Play convention.
/// For more details see <see href="https://docs.microsoft.com/azure/iot-develop/concepts-convention#writable-properties"/>.
/// </remarks>
public class WritableClientProperty
public class WritableClientProperty<T>
{
private T _value;

/// <summary>
/// The value of the writable property update request.
/// </summary>
public object Value { get; internal set; }
public T Value
{
get => Convention.PayloadSerializer.ConvertFromObject<T>(_value);
internal set => _value = value;
}

/// <summary>
/// The version number associated with the writable property update request.
Expand Down

0 comments on commit 5cdb7f7

Please sign in to comment.