.NET 7 Update: Required Properties in 10 Minutes or Less

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 ธ.ค. 2024

ความคิดเห็น • 69

  • @fbodirector7464
    @fbodirector7464 2 ปีที่แล้ว +21

    I just wanted to let you know I am loving the 10 Minute series.

  • @m5s10
    @m5s10 2 ปีที่แล้ว +37

    In case you didn't know, you don't need to type constructors manually like you did at ~2:15, instead of ctor (parameterless constructor), you can use ctorf (constructor with parameters for fields) or for your case ctorp (constructor with parameters for properties.

    • @IAmTimCorey
      @IAmTimCorey  2 ปีที่แล้ว +9

      Fun. I didn't know about that one. I've used the quick action refactoring to do the same, but I'll check out the ctorf.

    • @jayflaggs
      @jayflaggs 2 ปีที่แล้ว +3

      Nice! I use the "ctrl + ." shortcut and ctor snippet but didn't know about ctorf!

    • @jonnyjazzz
      @jonnyjazzz 2 ปีที่แล้ว +3

      I didn't know about ctorf either! Learn something every day. Thanks!

    • @jayflaggs
      @jayflaggs 2 ปีที่แล้ว +1

      I tried it and it didn't work out of the box. VS2022 community version.

  • @janne_kekalainen
    @janne_kekalainen 2 ปีที่แล้ว +1

    This is definitely one of those i've waited for a while, no longer do I need a nugget package to do this.

  • @ChadiHD112
    @ChadiHD112 2 ปีที่แล้ว +1

    Tim never disappoints, thanks for these 🙏🙏

  • @Irshu
    @Irshu 2 ปีที่แล้ว

    thanks Tim! This a feature we didnt we needed until now.

  • @codefoxtrot
    @codefoxtrot 2 ปีที่แล้ว +2

    I'm still not on board the nullable train. I understand its purpose and how to use it, but if feels like an extra dimension to think about when I'm trying to accomplish the real objective. Especially due to the existence of the null forgiving operator, where I now need to placate the compiler when it thinks something could be null but can't. It's all markup and ceremony distracting me from the greater mission. This required feature, adding new ceremony to the constructor scenarios feels more of the same. But I thank you Tim, because now I'll at least recognize it, if others end up adopting it!

    • @IAmTimCorey
      @IAmTimCorey  2 ปีที่แล้ว +1

      The nullability design-time checks are designed to help improve your code quality. If you have a string that you use, but you don't validate that it is not null first, that could throw an exception. This system shows you all of the places items could be null, but where you are treating them as not null. Those are all potential exceptions in the future. That's what it is designed to do.

  • @gugildak12
    @gugildak12 2 ปีที่แล้ว

    Thanks Tim. I think they have refocused improved security, privacy and validation in this dotNet version 7. I was watching your web api videos.

  • @yasminarab1353
    @yasminarab1353 2 ปีที่แล้ว +2

    Thx, but i have question,
    What is the different between the required key word and the [Required] attribut in the DataAnnotation?
    It would be great to know the differents.

    • @IAmTimCorey
      @IAmTimCorey  2 ปีที่แล้ว +3

      The Required data annotation is for forms, so when you have a form bound to a model, the model can tell the form that a given field must be filled in.

    • @natan_amorim_moraes
      @natan_amorim_moraes 2 ปีที่แล้ว +1

      it's also used for Swagger to know what is required when building an API, if I am not mistaken

    • @lordshmee
      @lordshmee ปีที่แล้ว

      Most importantly, the required keyword makes code violations into compile-time errors instead of runtime errors, which is awesome for pre-squashing bugs.

  • @natan_amorim_moraes
    @natan_amorim_moraes 2 ปีที่แล้ว +1

    Great Video!, i have a small API, and I will probably upgrade to dotnet 7 just for this feature alone! some of my constructor were very big and sometimes i forgot to include some properties in the constructor, this seems to me, way easier to avoid mistakes like that and to not have to write a lot of code, now i just need to test to see if my NuGet Packages are compatible with .net 7

  • @zahirkhan778
    @zahirkhan778 2 ปีที่แล้ว

    This was a great explanation

  • @yasminarab1353
    @yasminarab1353 2 ปีที่แล้ว +1

    Can you plz make tutorial about Reactive programming, and what is Observable, observer, Subject and SubjectBehaivor. it is hard to understand the code that is in other tutorial.
    Thx again.

    • @IAmTimCorey
      @IAmTimCorey  2 ปีที่แล้ว

      Thanks for the suggestion. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/

  • @raziuddinmohd
    @raziuddinmohd 2 ปีที่แล้ว

    Thanks for the nice explanation..

  • @expertreviews1112
    @expertreviews1112 2 ปีที่แล้ว

    Thanks for a very good video!

  • @chris5947
    @chris5947 2 ปีที่แล้ว

    Thank you!

  • @jonnyjazzz
    @jonnyjazzz 2 ปีที่แล้ว

    Thanks for this. The other day, I started up an empty project to prototype something, noticed .net7 and figured I'd try it, and ran into this nullable warning throughout. Just ignored it, but good to know how to properly work with it!
    Edit** Do you think this was designed so that we no longer need to check for nulls? Or what does this benefit? We still need to check for (or convert) nulls should data be coming from a store or API, for example.

    • @IAmTimCorey
      @IAmTimCorey  2 ปีที่แล้ว

      The benefit of the nullable warnings is to help you make explicit choices about nulls in your applications. When you turn it on in an existing application, you will see all of the places where the data could be null, but you aren't accounting for it. Addressing those issues gives you better code quality.

  • @den3v
    @den3v 2 ปีที่แล้ว +1

    Great videos

  • @bieleckipawel1
    @bieleckipawel1 2 ปีที่แล้ว

    Please someone tell me this works in Blazor Components

    • @bieleckipawel1
      @bieleckipawel1 2 ปีที่แล้ว

      Oh no it doesn’t 😢 You can still write and editor will not tell you that in MyComponent there is [Parameter] public required int MyParameter {get; set;}. You will find out on runtime. What a wasted opportunity.

  • @caseyspaulding
    @caseyspaulding 2 ปีที่แล้ว

    These are great!

  • @DaminGamerMC
    @DaminGamerMC 2 ปีที่แล้ว +1

    This should work with EFCore to not need the [Required] Attribute anymore

    • @IAmTimCorey
      @IAmTimCorey  2 ปีที่แล้ว +1

      No, those are different things. The Required attribute is still necessary.

  • @UmanPC
    @UmanPC 2 ปีที่แล้ว

    Tnx Tim

  • @borsuk7667
    @borsuk7667 2 ปีที่แล้ว

    I think will be good if for records required will be default
    So we create record by ctor or by object initializer

    • @IAmTimCorey
      @IAmTimCorey  2 ปีที่แล้ว

      That would be a fundamental change to how records work, which would break existing code, though.

  • @zackdemarco8747
    @zackdemarco8747 2 ปีที่แล้ว

    Very informative, however in a use case for using this with a REST api service, there are times where for numeric values or even GUIDs where a user may not provide the information ( In essence an underposting attack) how does that required feature work... does this do the same thing as it did before where it supplies whatever the default value is (so for an int it would default it to 0, which in some scenarios could be a valid value so we wouldnt want it to default to that.) I do feel like some of the issues with underposting could be made easier for developers to tackle but I am aware of at least one way to prevent the issues from happening. This is just about whether it makes that easier or not. This might also be more of an appropriate question against whether MVC's model binding process is trying to be too clever.

    • @zackdemarco8747
      @zackdemarco8747 2 ปีที่แล้ว

      Its interesting how many comments have had replies and yet nothing on this one.

    • @IAmTimCorey
      @IAmTimCorey  2 ปีที่แล้ว

      This isn't related to that. Required on properties and fields is for when a class is initialized in C#. What you are referring to is when a model is populated externally. For that, you would use the Required attribute on the property. In that case, what happens is that the value is either not posted to the API or in the case of an MVC form, it can be posted but the model state will be invalid. You check for the model state first in both of these cases before using the model's information. If they posted nothing to a required field, the model state is invalid and nothing should be trusted in that model's data.

  • @HollandHiking
    @HollandHiking 2 ปีที่แล้ว

    You also can assign a default value to the autoproperty. I really do not see a clear need for the required keyword.

    • @IAmTimCorey
      @IAmTimCorey  2 ปีที่แล้ว +1

      What should the default value of EmployeeId be? Some fields need to be populated with real values, not just starter values.

  • @iankirkpatrick2022
    @iankirkpatrick2022 2 ปีที่แล้ว +2

    Just an opinion on this: I don't know what the point of this feature in C# is... there's already a way to do this: require them in constructor arguments. It just bloats the language syntax and increases the learning curve for beginners in my opinion.

    • @janne_kekalainen
      @janne_kekalainen 2 ปีที่แล้ว +1

      That's the point, to not have to have a monster constructor. I would rather use an object initializer with required properties than a constructor with (many) parameters, especially when the number of parameters is large i.e. 5+. In the end it's personal preference.

    • @IAmTimCorey
      @IAmTimCorey  2 ปีที่แล้ว +1

      Not everything can use a constructor. ORMs typically use empty constructors to populate a model. Also, creating constructors simply to populate properties isn't a great solution in every case. What if you had six properties and only two were required. How do you set that up? A constructor that takes the two, but then the other four are optional? Or just take the two and let the caller mix the constructor call with object initializers? It makes for a messy setup. This is an option now, which allows for a choice of what is best for a given project rather than being forced into one way of doing things.

  • @RandalGJunior
    @RandalGJunior 2 ปีที่แล้ว

    Is there a way to deserialize to a required property?
    When I use JsonSerializer.Deserialise() it throws a exception on the required property.

  • @benjaminshinar9509
    @benjaminshinar9509 ปีที่แล้ว

    this feels like a serious mess.
    1. this prevents using "partial" constructors to set some of the values and object initialization for the others, since ctors must be "all or nothing" in terms of which properties they set.
    2. why doesn't the ctor know that it sets the properties? it seems like a weird miss that would happen when the declaration and definition are separated.
    3. question - if you had removed the "nullable" option from the project definition, wouldn't you get this behavior for free because the member are not initialized?

    • @IAmTimCorey
      @IAmTimCorey  ปีที่แล้ว

      1. Trying to set property values in two different constructors in two different locations using partial classes seems wrong. I can't figure out when that would be a wise solution.
      2. That's a tooling fix that will probably come soon.
      3. Nullable only gives you a warning. It is a recommendation rather than a requirement. Plus, nullable won't know that you are setting the values via the curly-brace option, so it will yell at us even if we are doing it "right".

  • @tymczasowanazwa
    @tymczasowanazwa 2 ปีที่แล้ว +1

    I really appriepriate your content, but this one could be 2 minutes or less :D

  • @stephentrouse2069
    @stephentrouse2069 2 ปีที่แล้ว

    You could have done this in one minute.

    • @IAmTimCorey
      @IAmTimCorey  2 ปีที่แล้ว +2

      And you could have gone to the developer's blog and read about it yourself. It feels like you are just complaining about a free resource.

    • @IAmTimCorey
      @IAmTimCorey  2 ปีที่แล้ว +2

      Here's my take on this: the only thing you communicated was that it could have been different. That isn't "nice positive feedback". It is a drive-by attack on how I chose to structure the video. It assumes that I failed to properly evaluate what should be in the video. It assumes that I did not consider cutting out content. There are a lot of negative assumptions in your statement. While I wasn't angry, I wasn't going to just sit there and try to pick the good out of the complaint.
      To answer the heart of your issue, I disagree with you. I felt (and still do) that setting up the need for required and how they work would allow people to understand what gap this fills. I did this to reduce confusion.

  • @ahmad122122
    @ahmad122122 8 หลายเดือนก่อน

    ERROR When used required >>>: Missing compiler required member system.runtime.compilerServices.CompilerFeatureRequiredAttribute !!! Any Solution

    • @IAmTimCorey
      @IAmTimCorey  8 หลายเดือนก่อน

      Are you trying to use this in a .NET Framework or .NET 5/6 project?

    • @ahmad122122
      @ahmad122122 8 หลายเดือนก่อน

      ​ .NET 4.8 WindowsForm App Last Version i have and play good with My Maui project Anyway, thank you for bringing it to my attention

    • @IAmTimCorey
      @IAmTimCorey  8 หลายเดือนก่อน

      Yep, that's the issue. This is only for .NET 7 and beyond.

  • @opticalmouse2
    @opticalmouse2 2 ปีที่แล้ว +1

    Why do you always use Blazor in your teachings?!
    In case you're living under a rock Blazor isn't that widely used.
    Why don't you use something like React for frontend?

    • @IAmTimCorey
      @IAmTimCorey  2 ปีที่แล้ว +5

      In this video, I use a Console application, not Blazor. As for why I use Blazor, it is because it is a great framework that uses C#. I do not, in fact, live under a rock. I do know that React is popular. What you don't seem to understand is that companies do not suddenly all pick a new framework right when it comes out. It takes time. Blazor is gaining in popularity. It also might not be as popular as a pure JavaScript framework because it is based upon C#. It was designed for C# developers who want a great web framework without switching languages. That leads me to my last point, which is that I cover C# development. Blazor is a C# framework. React is not. I don't cover it because doing so would mean covering a TON of additional things in preparation for covering React. People tend to think that the "best" framework is the one that is most popular. That's simply not true. In order to pick the best framework, it needs to be the best framework for your specific project. Otherwise, you are making a big mistake (which, as a consultant for a number of years, I had to pick up the pieces for numerous times).

    • @opticalmouse2
      @opticalmouse2 2 ปีที่แล้ว

      @@IAmTimCorey I wish Blazor will be more popular than Javascript...but I don't think it will ever happen. I think Blazor will remain a niche thing. React was released in 2013 so it's not new.