Implementing the Result Pattern In ASP.NET API

Поделиться
HTML-код
  • Опубликовано: 26 ноя 2024

Комментарии • 36

  • @razvann7334
    @razvann7334 Месяц назад +1

    Felicitari pentru explicatia concisa!

  • @Masteroxify
    @Masteroxify Месяц назад +3

    Recently I implemented very very similar pattern and it works perfectly! I can confirm that it is awesome pattern.

  • @majormartintibor
    @majormartintibor Месяц назад +1

    Very nice video. I love your take on technical debt. Technical debt could be an etire video on its own :)
    I know you have a video on "When To Throw Exceptions?", still need to watch that. My take on it is to think about what the business would do. I try to give an exampel, might be a bad one though: when you give someone a warehouse address to fetch some products from there and there is no warehhouse at the address you would probably check if the address was correct. If the given products are out of stock you order new ones and ask the customers to check back later etc. These error cases are within your business domain to handle. So you handle them. In your workflow you have a happy path, but you have error pathes as well that you are prepared for or implement after it happened once.
    However if at the given warehouse address you see warehouse is burning than that is not within your business domain. That is an exception, since it is not expected to ever happen, it is an exceptional event, you wouldn't have an employee witht he job to handle burning warehouses.

    • @Codewrinkles
      @Codewrinkles  Месяц назад +1

      I think your examples make totally sense and I agree to them. In the video you mentioned I gace conceptually similar examples, you'll see.

  • @anonymoos
    @anonymoos Месяц назад

    Excellent explainer! Already have a few places I will implement this and with that make the code easier to follow.

    • @Codewrinkles
      @Codewrinkles  Месяц назад

      Glad it was helpful! And hope you are doing well. Haven't seen you on Discord for a while :)

  • @nove1398
    @nove1398 Месяц назад

    I prefer this approach personally, my needs have been pretty basic and this would suffice

  • @topdownprogramming
    @topdownprogramming Месяц назад

    Thank you, I use these kind of error handling, but this one is far better than what I use. Especially that map things (ok result and bad request) at controller result.

    • @Codewrinkles
      @Codewrinkles  Месяц назад

      Glad you found something useful in the video

  • @Tamer_Ali
    @Tamer_Ali Месяц назад

    Thanks a lot Dan for the awesome video
    Is it better Error class to be record struct?

  • @nirajchandrajoshi
    @nirajchandrajoshi Месяц назад

    Well explained ❤ Thank You

  • @pilotboba
    @pilotboba Месяц назад +1

    I might add some implicit converters as well to simplify returning just the error type or just the value type.
    // Implicit conversion from T to Result
    public static implicit operator Result(T value) => Success(value);
    // Implicit conversion from string to Result
    public static implicit operator Result(Error error) => Failure(error);

    • @Codewrinkles
      @Codewrinkles  Месяц назад

      I generally tend to dislike the use of implicit operators because it adds a lot of "magic". As teams evolve, new devs will usually take some real chunk of time to understand what's happening. But, beyond personal preferences, this is a use case where the implicit operator could be used. I agree to that.

    • @pilotboba
      @pilotboba Месяц назад +1

      @@Codewrinkles I'm ok with a little bit of magic to make the code more readable in the handlers/endpoints. They just need to return the T and it will be converted accordingly.

    • @Eirenarch
      @Eirenarch Месяц назад

      @@Codewrinkles implicit is great here at the very least for the success case. Complaining it is magic is like complaining async methods wrap your return type in a Task for you

  • @davidpccode
    @davidpccode Месяц назад

    Excelent video my friend. Keep going

    • @Codewrinkles
      @Codewrinkles  Месяц назад

      Glad you liked it. Feel free to share it with others. Thanks

  • @alorenzoad
    @alorenzoad Месяц назад

    How does this impact the pipelines in mediatr, when your request becomes Result instead of a non-generic type?

  • @xanhxanh5097
    @xanhxanh5097 Месяц назад

    thank you for the video!

  • @Time21
    @Time21 Месяц назад

    Very well explained. |
    I do agree somehow about the technical debt on adding packages to your solution, but what about implementing your own 2FA?
    would this also be a technical debt or we just have to reinvent the wheel?

    • @Codewrinkles
      @Codewrinkles  Месяц назад

      Well I would probably NOT want to implement my iwn low level sms sending service. I would probably use Twilio or something.

  • @adrien8768
    @adrien8768 Месяц назад

    Return a successful result without throwing an exception, and ensure that you execute code only if it is valid. For example, in the addUser function: Method A returns the user if valid, while Method B takes the user and saves it. If Method A throws an error because the user is not valid, Method B should not be called. Using a result pattern, Method A returns null, and you must test in Method B to check if the user is not null, and so on.

  • @amrdeveloper8226
    @amrdeveloper8226 Месяц назад

    niiiiiiiiiiiiiice

  • @emanuelrodriguez3155
    @emanuelrodriguez3155 Месяц назад

    Why are you using VS ? What about rider?

    • @Codewrinkles
      @Codewrinkles  Месяц назад +1

      Well, I came to the conclusion that altough I like Rider much better the benefits of using it don't actually justify the monthly 16$.

    • @emanuelrodriguez3155
      @emanuelrodriguez3155 Месяц назад

      @@Codewrinkles best conclusion ever

  • @ldeerosel
    @ldeerosel Месяц назад

    I think you are confisingn Map with Match. Map execute a mapping from the type T to the type TOut

    • @Codewrinkles
      @Codewrinkles  Месяц назад

      The way I understand it, map functions usually applies a projection to a value. I think conceptually both map and match would be a good fit. But I might be wrong.

  • @EricKing
    @EricKing Месяц назад +1

    "Why import a library for simple functionality you can write yourself in five minutes?" Clearly you're not a JavaScript programmer lol. I use something similar in all my projects.

    • @Codewrinkles
      @Codewrinkles  Месяц назад

      Yeah, when you add expressjs you already add around 2300 libraries :)). Unfortunately, I had to work also on Node projects in the past

  • @harrisonwell1719
    @harrisonwell1719 Месяц назад

    code?

    • @Codewrinkles
      @Codewrinkles  Месяц назад

      As I mentioned in the video, source code is available for Codewrinkles members on Ambassador tier or higher. Check the link in the description to become a member and the check the Membership tab for info on how to get the source code

    • @harrisonwell1719
      @harrisonwell1719 Месяц назад

      @@Codewrinkles ok np