ASP.NET Core Controllers – Keep them Clean with these 3 Tips

preview_player
Показать описание
💡Fat controllers are a nightmare. The problem is that most controllers are not that fat in the beginning. Then code starts to add up and if there's no clear vision or structure on how to keep your controllers clean, everything goes south. That's why in this video I talk about the top 3 ways to keep your #aspnetcore controllers clean.

✅Join this channel to get source code access and other perks:

Mentioned videos:

Contents
1. Intro: 00:00
2. Problem statement: 01:27
3. Extract to a base controller: 07:02
4. Extract to action filters: 13:35
5. Extract to extension methods: 17:31
6. Exception handling as cross-cutting concern: 21:45
7. Considerations about logging: 28:23
8. Conclusions: 31:56
Рекомендации по теме
Комментарии
Автор

I was just wondering how to avoid the ugly "try-catch" stuff from all the controllers and services. Great solution, thanks! :)

sokoo
Автор

This is a a superb video, Dan. I learned a ton of things here. Thank you very much!

rogerpence
Автор

Thus is one of the video, I ve been searching for. Best explanations and new idea.. let's implement in my project.

mohanrajraja
Автор

Some excellent information Dan - great video and contents again!

CarrigansGuitarClub
Автор

Thank you for the video.
1. why did you choose to make your own extension to get data from header instead using FromHeader in action parameter
2. why did you choose to use exceptionfilter instead of using middleware?
Thank you.

pavelromashuk
Автор

But how can we test this controler with mock ILogger, or any other mock dependency, if we dont have constructor injected dependencies?

noss
Автор

Please what IDE is this you are using? Looks very cool

IfeanyiNnamdiOkagbue
Автор

Great video! You can move some of the code left (after refactoring) to a service class and the controller will be even leaner. Also, the abstraction level goes up a bit in the controller. Like you said, all depends on how you want to architect the solution, however what you show here I would say are best practices at an advanced level based on what I have seen in some projects.

victormoreno
Автор

A question about the action filters, let's say we need to add some code before the return method like re-populate ViewBag then either return View("action") or RedirectToAction like below:

if (!ModelState.IsValid)
{
code to execute...
return
}
can something like that be done using action filters?

Thanks for the great tutorial

cyrildouglas
Автор

Thanks Mr.Dan for this awesome video 👍
Adding [Authorize] attribute to every controller is a headache special if the app has more than role e.g: Admins, Managers & Users. I was thinking to create a stand alone base controller for each role with their authorize attribute e.g:[Authorize (Roles="Users")] and make the controllers that the users should access inherit from UsersBaseController for example.
Is there a better approach?

Tamer_Ali
join shbcf.ru