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

Are GenerateSerializer and [Id] always required? #9375

Open
keithazzopardi opened this issue Mar 8, 2025 · 2 comments
Open

Are GenerateSerializer and [Id] always required? #9375

keithazzopardi opened this issue Mar 8, 2025 · 2 comments
Labels

Comments

@keithazzopardi
Copy link

I would like to pass a DTO to a service layer without dependencies on Orleans omitting attributes like [Id] or [GenerateSerializer] in the Service Layer DTO. Is there a way to apply these attribute programmatically or perhaps via inheritance as below.

/* Service DTO. No Orleans dependencies */
public class Order {
    // DTO properties
}

/* Orleans DTO */
[GenerateSerializer]
public class OrleansOrder : Order{
    
}
@ledjon-behluli
Copy link
Contributor

Only if you decide not to use the Orleans serializer, e.g: use one of the Json-based once like JSON.NET or STJ.
Regarding your example: no that won't work!

@nkosi23
Copy link

nkosi23 commented Mar 19, 2025

You can can create surrogate types if if i remember correctly:
https://learn.microsoft.com/en-us/dotnet/orleans/host/configuration-guide/serialization?pivots=orleans-7-0#surrogates-for-serializing-foreign-types

But this is overkill in your case in my opinion and would add a lot of ceremony to your code. What i've done in my case is use STJ for all types located in my core domain model namespace.

Just be aware that STJ is one order of magnitude slower than the native serializer and does not give you the same benefits (you lose powerful things such as object reference preservation & cie). See:

#9172 (comment)

In my opinion the performance hit is negligible for most apps since Orleans is still blazing blast (we are talking about nanoseconds). And in my case, since i use F# and everything is immutable over there, i didn't need the reference preservation features. And you can still use the native serializer for types used on performance critical paths.

Once this is said, unless you have hard constraints you may want to consider treating Orleans as a core dependency of your project, just like the other core libraries of .NET. because at the end of the day, Orleans is literally the execution environment of your code. Adding the nuget package is reasonable. The main reason i haven't followed that path is that there are no source generators in F# and writing the attributes manually would have been tedious.

in my opinion, using STJ by default and the native serializer only where you truly need the performance or the guaranteed will give you the best of both world.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants