Make Your ASP.NET Core API Controllers Incredibly Simple

preview_player
Показать описание

Having thin controllers in an ASP.NET Core API is a good practice. It helps to keep any logic away from your ASP.NET Core controllers, increasing maintainability and simplifying your endpoint. I'll show you an interesting approach to making your ASP.NET Core API controllers incredibly simple with a few functional programming concepts.

Join my weekly .NET newsletter:

Read my Blog here:

Subscribe for more:

Chapters
0:00 Benefits of "Thin" Controllers
1:34 Rewriting the controller endpoint
2:24 Defining the Bind function
4:19 Defining the Match function
7:05 Fluent controller endpoints
Рекомендации по теме
Комментарии
Автор

Definitely clean, but harder to understand for average developers. I would prefer somewhat balanced simplicity.

nayanchoudhary
Автор

While this is nice looking and certainly functional, i think it's not a major improvement over the controller code you showed at the start of this video. Imo, the original version does comunicate intent way better than the new version does (specifically the bind portion is something that someone looking at this code for the first time will have to investigate before they are able to understand what is actually happening). Having the match function to decide on a handler to call given a certain result type ( success vs. failure) is nice, but i would personally leave it at that and keep the rest of the code as it was.

Anyways: Thanks for showing of the more functional approch, definitely an intersting way to do it !

ewkdev
Автор

We have been discussing this very thing at work and I really like your solution here. I’ve a feeling we will be using exactly this in our controllers from now on. Thanks for the video, it’s excellent!

morganskinner
Автор

Looks nice and fancy afterwards, but by wrapping everything in static extension methods I would argue it is harder to read, debug and unit test. The original way was clean, simple, effective and easily tested.

gntkmdy
Автор

I would prefer to have the old version for 3 reasons: easy to read, easy to debug, looks native.

VladyslavHorbachov
Автор

Man I'm so glad I took a functional programming course last semester at my Uni, monads are so awesome!

itzWLF
Автор

Amazing content! You’re always surprise me with very useful content, thanks!

topx
Автор

Man your contents are just amazing.keep it up. good luck!

javidibra
Автор

You've created a Result monad. This comes out of the box in most functional languages. It's such a round-about way of doing it in C# that I usually just don't bother writing all of the "extra code to make it more F#'y".

The Result class makes it possible to make a distinction between an expected (validation / domain) error and an actual unexpected exception (e.g. the database is down or we accidently introduced a bug).

shadowsir
Автор

Milan Nice Explanation.
One Suggestion, like you're using already created project for Video.
I think it's hard for viewers to understand when you start coding in already created classes etc.
I think it will be good if you demo it in sample small project.
it will be easy to understand. :)

mohsinazamafridi
Автор

Great job Milan. Would be great to see a GraphQL video soon

cyrilw
Автор

Nice video! Btw, what's the name of you VS theme?

Scifer
Автор

I prefer fluent way as well, but performance has to be taken into consideration if it’s a priority 🚀

vitek
Автор

Hi Milan, nice and efficient work, thanks for sharing it! I see you are using CancellationToken parameter in your Post Methods. I heard that we should not be using CancellationToken in our Post Methods because of in case of cancel there may be an inconsistency in the data as a result of the process being interrupted. What do you think about this?

hamitkarahan
Автор

@MilanJovanovicTech, that is a super clean and easy to read design, thanks for your great ideas and inspirations. Don't pay to much attention to ones who say it's complex and unreadable. Even for average mind as mine it is quite straightforward (even given I have started to learn c# dotnet a few month ago).
I could only think of better name for Bind, like Execute, but probably you have some convention as I see you always do in dotnet)

arthurfedotiew
Автор

Please make a video on how to implement identity server with role based auth + how to check the role conditionally in a common/shared method

dfytq
Автор

Great idea for refactoring! Could you make a introduction video on monads and discriminated unions in C#?

branislavpetrovic
Автор

Love this with the ErrorOr library, have something very similar in place currently. In my opinion throwing Exceptions for errors which are expected (we know about) is logically incoherent since they are not exceptional in nature. What's your take on Errors vs Exceptions?

MB-Kajtech
Автор

Can you make a series on functional programming important concepts Milan ?

ramytawfik
Автор

How do you not have to write authentication scheme on the controller function? Which one is picked as default? If you only have one active, will it be picked as default?

SKglitches