Комментарии:
Hi Zoran, great video!
Why class shouldn't derive from another class like in your example with name?
NonEmptyString record struct is nice, but in C# you can still use "default" keyword which would ignore parameterless constructor
ОтветитьCan you make a video when to use record structs and when to use record classes?
ОтветитьDidn't even think about deriving records, didn't know about equality.
Glad to dump structs.
Always learn something from these videos.
Devs should watch this even if they think they understand records.
ОтветитьYou can also simply use readonly record struct to make its properties immutable by default.
ОтветитьEveryone should watch out when using the Primary Constructors feature. There is currently no way to mark the backing fields that it creates as readonly. Not a good feature to use in conjunction with Dependency Injection.
Ответитьthanks for the subtitles in english are very useful 🙂
Ответитьnever heard that i should use record structs over normal structs
do you have any sources going deeper into that?
i'm dumb, since i'm using Blazor with JsInterop i had some objects that had to be passed between Blazor and javascript, I used a plain class with the IEquatable, when i could have just used record, i even knew about them. Ahh thanks for the video.
ОтветитьTo be sure, I 100% understood what you were describing about records in the opening of this video. But I knew approximately 0% of the syntax you showed after, so it’s a good thing I did not skip the rest of this video per your advice.
ОтветитьThis is the best demo on records. It couldn't get better than this one. <3
ОтветитьCan you give some real world examples on using records. I am struggling to think of where I can use them in the business application I am working on.
ОтветитьGood stuff. I was looking for the video to discuss with dotnet team in the format: let's meet after a week and discuss it. Your topics so far were in the shelve labeled "this could be too difficult for junios and mids", but this one is flexible. Juniors and seniors will find topics to discuss. Thank you.
ОтветитьLove it. Used records a few times but always confused me what they really are tbh. Other than a quick way to create a small class (which is what I saw it as earlier). Now I'm kind of considering migrating a lot of classes to records.. Great video!
Ответить'make invalid state unrepresentable' - that is an excellent line
ОтветитьGreat video!
ОтветитьI wondering if records can be used as entity models working with EF
ОтветитьIn a previous video, you recommended against using collections in records due to the issues with deep immutability. Based on this video, it seems you would not have the same concern if the record used frozen collections; is that accurate?
ОтветитьIn the record you can define required immutable properties like so: public required string FirstName { get; init; }
ОтветитьThe later `record struct` makes the properties mutable by default. Does this seem like a backward trend?
ОтветитьDiscord link is not working anymore :(
ОтветитьYour syntax is so absolutely alien to me always, but I find your videos very valuable.
Things like var (firstName1, _) = samuelL; do not even look like C# to me. What is the variable here called?
Too awesome!
ОтветитьThis was a succinct deep-dive into records. Could you kindly make one on pattern matching, too? There are countless ways to use them and it doesn't come naturally to think in pattern-matching style after years of using verbose conditions.
ОтветитьWill you do F# somedays?
ОтветитьC# invented record from Pascal
ОтветитьBesides learning records, if anyone wants to learn effective communication, watch this! <3
ОтветитьIt seems most objects I deal with have an Id. Is a record still suitable? It doesn't matter the equality of the other properties, only Id. And what about objects with many properties? Is a record slower?
ОтветитьWould that be an candidate for record struct? If it does make sense how would a record struct look like?
public sealed class DateTimeRange
{
public DateTime Start { get; private set; }
public DateTime End { get; private set; }
public DateTimeRange(DateTime start, DateTime end)
{
EnsureStartIsBeforeEnd(Start, End);
Start = start;
End = end;
}
static void EnsureStartIsBeforeEnd(DateTime start, DateTime end)
{
if (start > end)
{
throw new DateTimeRangeException("The endtime must not be before the starttime");
}
}
}
Damn!!! I love this, I generally use it in DTOs scenario
ОтветитьEnough material for at least 10 videos :) Nice!
ОтветитьWhat a wonderful guide! I love your concise and clear communication
ОтветитьI would love to use records more but I have one doubt. Even if I just store values that are immutable, I often initialise SOME (but not all) values during creation and update OTHER later. Records don't seem to be in favour in that situation. Or maybe I can do RecordType x = x with {...} to create updated and in fact replace the old object? [I am rather a beginner;)]
ОтветитьExcellent, full and deep explanation, thanks a lot 🎉
Ответитьthank you! excellent explanations
ОтветитьGreat vid and love your accent and storytelling!
ОтветитьYou Speak too bad and your accent is a disaster that left the video at minute 2
ОтветитьGreat content, thanks Zoran.
ОтветитьThe best 13 min ever. Amazing explanation
ОтветитьIt was very good explanations. Thank you
ОтветитьMaybe I'm old school, but I don't like black magic. There's too much implicit behavior to reason about.
It's a bit like reflection. Very powerful feature, but you'll never know how a change will impact the calling scope.
I really like the looks of records but how do you manage using them as value objects when inserting data to a database where an identity is required?
ОтветитьRecords have its place but I'm disappointed to see the community once again (the first time with minimal APIs) trying to re-invent classes. I mean people trying to use class features using records... For example: trying to have properties within records and even worse logic (any logic, validations, overrides etc).
ОтветитьReally useful video! Thanks, Zoran.
However, there's one significant problem with your NonEmptyString implementation. The way you implemented the validation on it does not prevent someone from making a non-zero length array of NonEmptyStrings, using the "with { Value = null }" expression, or even worse, simply forgetting to initialize a NonEmptyString field. In all these cases your parameterless constructor that throws will not be called. Say, for example, you have a Person class with NonEmptyString Name property and you forget to initialize it - the Value of the Person's Name will end up being null. This behavior defeats the purpose of having the NonEmptyString type in the first place.
If you want to enforce validation of your members so they don't end up with default values, you must use the standard reference-type record. Even then, you must be careful with auto-implemented init properties which can be set to whatever using the "with" expression.
Would you also use record struct to model money types in retail domains like ListPrice, CostPrice, Total, GrossProfit etc….?
ОтветитьInteresting video Sir, Is there any way to use DataAnnotations using record types as DTO ? if so, would that affect the immutability of that object ?
Ответить