Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete example in Orleans / Serialization / Surrogates #44720

Open
egil opened this issue Feb 7, 2025 · 0 comments
Open

Complete example in Orleans / Serialization / Surrogates #44720

egil opened this issue Feb 7, 2025 · 0 comments
Labels
⌚ Not Triaged Not triaged

Comments

@egil
Copy link

egil commented Feb 7, 2025

Describe the issue or suggestion

@ReubenBond helped set up this sample that demonstrates a more complex example of creating surrogates in Orleans serialization.

The docs could benefit from including a variant of this example in https://learn.microsoft.com/en-us/dotnet/orleans/host/configuration-guide/serialization?pivots=orleans-7-0#surrogates-for-serializing-foreign-types

// Foreign types
public record Foo<T>(string Name, T Payload) where T : Bar;
public record Bar(Baz Baz, string Something);
public record Baz(string Hi);
public record BarY(string Content, Baz Baz, string Something) : Bar(Baz, Something);

// Surrogates
[GenerateSerializer, Immutable]
public readonly record struct FooSurrogate<T>(string Name, T Payload) where T : Bar;

[GenerateSerializer, Immutable]
public readonly record struct BarSurrogate(Baz Baz, string Something);

[GenerateSerializer, Immutable]
public readonly record struct BazSurrogate(string Hi);

[GenerateSerializer, Immutable]
public readonly record struct BarYSurrogate(string Content, Baz Baz, string Something);

// Converters
public sealed class FooConverter<T> : IConverter<Foo<T>, FooSurrogate<T>> where T : Bar
{
    public Foo<T> ConvertFromSurrogate(in FooSurrogate<T> surrogate) => new(surrogate.Name, surrogate.Payload);
    public FooSurrogate<T> ConvertToSurrogate(in Foo<T> value) => new(value.Name, value.Payload);
}

public sealed class BarConverter : IConverter<Bar, BarSurrogate>
{
    public Bar ConvertFromSurrogate(in BarSurrogate surrogate) => new(surrogate.Baz, surrogate.Something);
    public BarSurrogate ConvertToSurrogate(in Bar value) => new (value.Baz, value.Something);
}

public sealed class BazConverter : IConverter<Baz, BazSurrogate>
{
    public Bar ConvertFromSurrogate(in BazSurrogate surrogate) => new(value.Hi);
    public BarSurrogate ConvertToSurrogate(in Baz value) => new (value.Hi);
}

public sealed class BarYConverter : IConverter<BarY, BarYSurrogate>
{
    public BarY ConvertFromSurrogate(in BarYSurrogate surrogate) => new(surrogate.Content, surrogate.Baz, surrogate.Something);
    public BarYSurrogate ConvertToSurrogate(in BarY value) => new (value.Content, value.Baz, value.Something);
}
@dotnet-policy-service dotnet-policy-service bot added the ⌚ Not Triaged Not triaged label Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⌚ Not Triaged Not triaged
Projects
None yet
Development

No branches or pull requests

1 participant