Complete example in Orleans / Serialization / Surrogates
- Dominant language
- No language data
- Stars
- 4.8k
- Forks
- 6.1k
- Avg merge
- 15h 21m
- Merged PRs (30d)
- 370
Description
### 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
```c#
// Foreign types
public record Foo(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(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 : IConverter, FooSurrogate> where T : Bar
{
public Foo ConvertFromSurrogate(in FooSurrogate surrogate) => new(surrogate.Name, surrogate.Payload);
public FooSurrogate ConvertToSurrogate(in Foo value) => new(value.Name, value.Payload);
}
public sealed class BarConverter : IConverter
{
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
{
public Bar ConvertFromSurrogate(in BazSurrogate surrogate) => new(value.Hi);
public BarSurrogate ConvertToSurrogate(in Baz value) => new (value.Hi);
}
public sealed class BarYConverter : IConverter
{
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);
}
```
---
#### Document Details
⚠ *Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.*
* ID: 1ad80183-a94c-1344-7289-40e97a964118
* Version Independent ID: e0e88587-aeeb-b95a-65b3-d62db40cf0b9
* Platform ID: cea5c9e0-8887-9e61-c735-27ed81d6cf37
* Content: [Serialization in Orleans - .NET](https://learn.microsoft.com/en-us/dotnet/orleans/host/configuration-guide/serialization?pivots=orleans-7-0#surrogates-for-serializing-foreign-types)
* Content Source: [docs/orleans/host/configuration-guide/serialization.md](https://github.com/dotnet/docs/blob/main/docs/orleans/host/configuration-guide/serialization.md)
* Service: **dotnet-orleans**
* GitHub Login: @IEvangelist
* Microsoft Alias: **dapine**
Contributor guide
Assessment
This issue has not been assessed yet.