Skip to content

Commit 365064d

Browse files
Copilotjaviercn
andcommitted
Implement explicit interface implementation in IPersistentComponentStateSerializer<T> and make base interface public
Co-authored-by: javiercn <[email protected]>
1 parent cbf9fa9 commit 365064d

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

src/Components/Components/src/IPersistentComponentStateSerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Microsoft.AspNetCore.Components;
77

8-
internal interface IPersistentComponentStateSerializer
8+
public interface IPersistentComponentStateSerializer
99
{
1010
Task PersistAsync(Type type, object value, IBufferWriter<byte> writer);
1111
object Restore(Type type, ReadOnlySequence<byte> data);

src/Components/Components/src/IPersistentComponentStateSerializerOfT.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Components;
99
/// Provides custom serialization logic for persistent component state values of type <typeparamref name="T"/>.
1010
/// </summary>
1111
/// <typeparam name="T">The type of the value to serialize.</typeparam>
12-
public interface IPersistentComponentStateSerializer<T>
12+
public interface IPersistentComponentStateSerializer<T> : IPersistentComponentStateSerializer
1313
{
1414
/// <summary>
1515
/// Serializes the provided <paramref name="value"/> and writes it to the <paramref name="writer"/>.
@@ -26,4 +26,16 @@ public interface IPersistentComponentStateSerializer<T>
2626
/// <param name="data">The serialized data to deserialize.</param>
2727
/// <returns>The deserialized value.</returns>
2828
T Restore(ReadOnlySequence<byte> data);
29+
30+
/// <summary>
31+
/// Explicit interface implementation for non-generic serialization.
32+
/// </summary>
33+
Task IPersistentComponentStateSerializer.PersistAsync(Type type, object value, IBufferWriter<byte> writer)
34+
=> PersistAsync((T)value, writer);
35+
36+
/// <summary>
37+
/// Explicit interface implementation for non-generic deserialization.
38+
/// </summary>
39+
object IPersistentComponentStateSerializer.Restore(Type type, ReadOnlySequence<byte> data)
40+
=> Restore(data)!;
2941
}

src/Components/Components/src/PersistentStateValueProvider.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,8 @@ private static PropertyGetter ResolvePropertyGetter(Type type, string propertyNa
109109
var serializerType = typeof(IPersistentComponentStateSerializer<>).MakeGenericType(type);
110110
var serializer = serviceProvider.GetService(serializerType);
111111

112-
if (serializer != null)
113-
{
114-
// Create an adapter that implements the internal interface
115-
var adapterType = typeof(SerializerAdapter<>).MakeGenericType(type);
116-
return (IPersistentComponentStateSerializer?)Activator.CreateInstance(adapterType, serializer);
117-
}
118-
119-
return null;
112+
// The generic interface now inherits from the internal interface, so we can cast directly
113+
return serializer as IPersistentComponentStateSerializer;
120114
}
121115

122116
[UnconditionalSuppressMessage(
@@ -374,13 +368,4 @@ internal bool TryTake<TValue>(string key, IPersistentComponentStateSerializer<TV
374368
return false;
375369
}
376370
}
377-
378-
private sealed class SerializerAdapter<T>(IPersistentComponentStateSerializer<T> serializer) : IPersistentComponentStateSerializer
379-
{
380-
Task IPersistentComponentStateSerializer.PersistAsync(Type type, object value, IBufferWriter<byte> writer)
381-
=> serializer.PersistAsync((T)value, writer);
382-
383-
object IPersistentComponentStateSerializer.Restore(Type type, ReadOnlySequence<byte> data)
384-
=> serializer.Restore(data)!;
385-
}
386371
}

src/Components/Components/src/PublicAPI.Unshipped.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ Microsoft.AspNetCore.Components.Infrastructure.RegisterPersistentComponentStateS
1616
Microsoft.AspNetCore.Components.PersistentStateAttribute
1717
Microsoft.AspNetCore.Components.PersistentStateAttribute.PersistentStateAttribute() -> void
1818
Microsoft.AspNetCore.Components.Infrastructure.PersistentStateProviderServiceCollectionExtensions
19+
Microsoft.AspNetCore.Components.IPersistentComponentStateSerializer
20+
Microsoft.AspNetCore.Components.IPersistentComponentStateSerializer.PersistAsync(System.Type! type, object! value, System.Buffers.IBufferWriter<byte>! writer) -> System.Threading.Tasks.Task!
21+
Microsoft.AspNetCore.Components.IPersistentComponentStateSerializer.Restore(System.Type! type, System.Buffers.ReadOnlySequence<byte> data) -> object!
1922
Microsoft.AspNetCore.Components.IPersistentComponentStateSerializer<T>
2023
Microsoft.AspNetCore.Components.IPersistentComponentStateSerializer<T>.PersistAsync(T value, System.Buffers.IBufferWriter<byte>! writer) -> System.Threading.Tasks.Task!
2124
Microsoft.AspNetCore.Components.IPersistentComponentStateSerializer<T>.Restore(System.Buffers.ReadOnlySequence<byte> data) -> T

0 commit comments

Comments
 (0)