DEV Community

Discussion on: Where would you use the new C# 9 Record Type

Collapse
 
gsferreira profile image
Guilherme Ferreira

Hi!
Records will be really useful in cases where we are looking for immutable data. Examples:

  • DTOs
  • Events when working in event sourcing
  • Commands
  • ...

I recommend you to read this blog post (pre-records) by Jimmy Bogard when he explains the challenges of immutable classes in .net. Know, with records, it's embedded in the language.

jimmybogard.com/immutability-in-dtos/

Collapse
 
klyse profile image
klyse

Hi,
thanks for the comment! I agree records can be great for DTOs and Commands (I'm also thinking about MediatR). As Jimmy Bogard mentioned in his blog post upon until now Immutable objects could not be serialized easily. Now that we have records they are immutable and can be serialized 🎈. Here is a small example of the serialization (I couldn't believe it, I had to try it myself :D) dotnetfiddle.net/e8tSIN