Clean Architecture with ASP.NET Core 8 | .NET Conf 2023

  Переглядів 179,033

dotnet

dotnet

День тому

Clean Architecture (aka Onion, Hexagonal, Ports-and-Adapters) organizes your code in a way that limits its dependencies on infrastructure concerns. This results in much more testable, maintainable code and is ideal for Domain-Driven Design as well as microservices. Learn how to apply it to your ASP.NET Core apps!
Chapters:
00:00 Let's get started
00:06 What is Clean Architecture
01:25 When to use Clean Architecture
03:25 Two approaches to layered architecture
05:57 Demo
www.nuget.org/packages/Ardali...
07:25 Clean Architecture Rules
08:51 What belongs in the Core project
13:48 Demo
16:23 What about CQRS
16:45 What belongs in teh Use Cases project
17:01 Demo
18:35 What belongs in the Infrastructure project
20:02 Demo
21:29 What belongs in the Web project
22:05 Demo
23:57 The Shared Kernel
24:17 Demo
25:52 Resources
25:57 Q&A
Clean Architecture Template
github.com/ardalis/CleanArchi...
eShopOnWeb Reference Application
github.com/dotnet-architectur...
Architect Modern Apps w/ASP.NET Core eBook
dotnet.microsoft.com/learn/we...
More from Steve
ardalis.com
Download .NET 8 - aka.ms/get-dotnet-8
Hack Together - aka.ms/hacktogether/dotnet
Learn collection - aka.ms/learn-dotnet-8
.NET 8 Survey - aka.ms/dotnet-survey
Local Events - aka.ms/dotnetconf-local-event
Hero blog - aka.ms/dotnet8blog
Blog: aka.ms/dotnet/blog
Twitter: aka.ms/dotnet/twitter
TikTok: aka.ms/dotnet/tiktok
Mastodon: aka.ms/dotnet/mastodon
LinkedIn: aka.ms/dotnet/linkedin
Facebook: aka.ms/dotnet/facebook
Docs: learn.microsoft.com/dotnet
Forums: aka.ms/dotnet/forums
🙋‍♀️Q&A: aka.ms/dotnet-qa
👨‍🎓Microsoft Learn: aka.ms/learndotnet
#dotnet

КОМЕНТАРІ: 115
@FockewulfAz
@FockewulfAz 3 місяці тому
Fantastic! God bless you!
@fboucheros
@fboucheros 5 місяців тому
Amazing talk! Love it! Architecture is not an easy topic and you made it clear and approachable. I'll do a "donet new" to continue my exploration.
@vishweshhramesh2163
@vishweshhramesh2163 2 місяці тому
Best video to understand CQRS, Clean Architecture without any clutter.
@mrohailiqbal
@mrohailiqbal 4 місяці тому
It was a good learning experience through this video. Keep it up.
@chockshoway
@chockshoway 5 місяців тому
Great video Steve. Fantastic to see Microsoft finally talking about this, domains and DDD. Hopefully we'll see some domain-centric templates rather than those entrenching anti patterns like focus on the tech choice (MVC) and pushing teams towards procedural code (top level functions).
@jan5310
@jan5310 5 місяців тому
Just want to say thank you for your very good explained clean architecture presentation! Hopefully you can extend this presentation in a more indepth series. I am sure to be checking out your template and play with it.
@acodersjourney
@acodersjourney 5 місяців тому
Your channel is a treasure trove of knowledge. Thanks!
@bmassioui
@bmassioui 5 місяців тому
Thanks, Ardalis for this amazing presentation, which made the CA very simple and easy to comprehend. From my own perspective, I'd like to see those NuGet packages' code-base in the project, which will give them full control over them to maintain and adapt them based on the needs 👍
@FainTMako
@FainTMako 5 місяців тому
Great presentation. One of my favorites from this year!
@ferreimavi1998
@ferreimavi1998 29 днів тому
Wow, just wow 👏👏👏
@DenisioRodrigues
@DenisioRodrigues 3 місяці тому
Great! Very nice!
@sanju0099
@sanju0099 19 днів тому
excellent, great Job!
@martinaaron7304
@martinaaron7304 4 місяці тому
Really great video. Learned a lot.
@ezalddenalmaghout
@ezalddenalmaghout 2 місяці тому
Steve is a gift for .net community
@philipproner6613
@philipproner6613 5 місяців тому
I don't see the clean in clean architecture. This looks like an awful huge amount of projects, files and folders for a ToDo App. The Point of seperating infrastructure and business logic seems not represented (EF Core was in the Business Layer, wasn't it?). The weird replacement for controllers with the ?reapr? framework looks just like a show off, what is the point of that? I don't see any advantage over simple API Controllers, in fact, the technique to place code that belongs to the controllers just in files nested by filename makes it unnecessarily scattered. I was really looking forward to this, but it just left me confused and I still trust the n-tier architecture even more. I think now, any architecture is clean, as long as it is consistent and scales. Pls change my mind 🙂
@RunawayYe
@RunawayYe 5 місяців тому
Even though many experts say that Clean architecture and Onion architecture are pretty much the same, their implementation of the two differs considerably. It is almost as if each of the architectures has their own "style", their own conventions in a way, which grow increasingly more complicated to the naked eye. But it doesn't have to be that way. Let me give you a personal example - I've recently refactored a few apps from n-tier to Onion, and in essence it isn't much more than moving the database/cloud interfaces from the infrastructure layer to the domain layer, and just pointing the application/services layer towards the domain layer instead of to the infrastructure layer. Boom, Onion. Now lets say I were to copy code from the service classes to cqrs-style command and query classes (which does involve a lot of boilerplate unfortunately), and then rather than inject them in the controller I implement mediatr to call them. Now everyone who looks at my solution would say "oh, that's Clean architecture". Even though the change itself is simply stylistical. My point was that it doesn't need to be as complex as presented in this video. Are there benefits to that increased complexity? I dunno. Maybe testability, cause with cqrs you just test commands and queries and not have to use much brain power on what needs testing. Maybe future extensability, cause you can just add a new command/query instead of modifying your service class (which maybe you shouldn't do as it can violate the O in SOLID). Maybe also avoiding your service class becoming huge (but on the other hand you're get so many folders and files for cqrs). I'm just thinking out loud, I'm no expert.
@alexandermackintosh1755
@alexandermackintosh1755 2 місяці тому
I heard a really good NDC talk a few week ago about how the so called 'functional core imperative shell' concept fits in nicely with this I would go have a read as that has convinced me more! The idea is something like this really encourages you to keep IO operations at the very edges of your application, and this inherently means that every bit of business logic is pretty much gonna end up as functional programming style functions that are super easy to test
@frankhoffy
@frankhoffy 5 місяців тому
Great presentation Steve. Our agency had implemented something very similar in the past where we inverted the dependency between the business layer and the data access layer, so this seems like a logical progression for us. We're currently debating the virtues of a BFF layer too. I'm wondering if that will somehow factor into clean architecture in the future. The logic to combine data from multiple APIs needs to go somewhere after all.
@calvinwilson3617
@calvinwilson3617 5 місяців тому
Love your template, I would argue that the biggest advantage of Clean Architecture aside from the dependency enforcements as you mentioned, is that you can easily swap out and configure the entire infrastructure layer. This can be extremely useful when you will have multiple clients/front ends. You can reuse the entire business logic and only create different implementations for the infrastructure layer. Most useful IMO for hosted Blazor WASM solutions although blazor itself still has a bit to go and can be frustrating to debug.
@Tvde1
@Tvde1 5 місяців тому
How often have you done this 😂😂
@Jason_Shave
@Jason_Shave 5 місяців тому
Love this topic. I can tell you’re rushing through some of this content though due to the short session time constraints. Would be great to have more time on these topics and the same goes for most of the other content too.
@Paul-uo9sv
@Paul-uo9sv 5 місяців тому
This is the best video yet on clean architecture
@rguere
@rguere 2 місяці тому
thanks so much. my project run ok..
@kell7689
@kell7689 3 місяці тому
Sounds nice, but I just can't fathom what a project with many entities would look like. If this is the complexity of a TODO app using CA, I'd imagine it would become completely overwhelming with a more complex domain. Could argue that it becomes second nature once you've trained your eye to the architecture, but I find it hard to believe that this is feasible for large systems or large teams. Feels more like a 'blue skies' design; works well in a perfect world where everyone is extremely disciplined. If anyone could point me to a large scale system that uses this architecture I'd love to see it.
@alexandrucioarec6554
@alexandrucioarec6554 3 місяці тому
I have pretty much the same view on this
@Ardalis
@Ardalis Місяць тому
It actually enforces its dependency rules, rather than simply relying on developers to remember to be disciplined. It's not new - hexagonal and ports-and-adapters and clean have been around for a couple of decades now.
@Marcin-jz8qp
@Marcin-jz8qp 5 місяців тому
Superb!
@user-nw7jo5xw9x
@user-nw7jo5xw9x 5 місяців тому
thanks for amazing presentation. Our team have ~10 micro-services on production with clean architecture.. and I personally don't recommend it.. The benefits of Clean Architecture are true, but not as big as we talk (for most case). However the downside of Clean Architecture are Bigger than we think.. Unfortunately not many peoples share the bad sides of Clean Architecture... My recommendation is Vertical Architecture. (If we could rewrite all our microservices again 😢)
@user-fv7oh9ig4q
@user-fv7oh9ig4q 4 місяці тому
imo - this is way more complex than it should be
@kronostitan11
@kronostitan11 Місяць тому
That’s kind of the point. It’s not supposed to be for very simple applications
@christianf7740
@christianf7740 5 місяців тому
Great stuff
@nobir98
@nobir98 5 місяців тому
Nice!!
@mumk
@mumk 5 місяців тому
great to see Ardalis here, I knew him from his Pluralsight course
@LuisGabrielNSimas
@LuisGabrielNSimas 5 місяців тому
2
@olivier0003
@olivier0003 5 місяців тому
Good Job :)
@slobodanmikaric2180
@slobodanmikaric2180 5 місяців тому
This is great! Thanks on ture. I have one question, as you show code that has 2 Aggregates but I am interested in how you gonna organize your domain if you have multiple bounded contexts(no microservices please)? Complexity starts there and managing infrastructure and also you need to make some encapsulation between bounded context.I like to use "Majestic Monolite" in this case and I am wondering how we can use your template in that way or do you have ideas to put together something like that in future?
@Ardalis
@Ardalis 5 місяців тому
This is where you'd start looking at a "modular monolith" approach, which yes I'm developing some content around. An alternative to microservices that allows for separation of contexts (and even teams) but without the overhead of managing a distributed application.
@sdudnic
@sdudnic 3 місяці тому
how should we link the client project here, like Angular application? I suppose a folder like Client to add to the Web solution?
@hectorcontreras9171
@hectorcontreras9171 4 місяці тому
Should I bother seeing the other videos on Clean Architecture with ASP.NET Core 6 (and 7)" before this one, or can I just start here?
@wanderingxsx
@wanderingxsx Місяць тому
I can't imagine a single case where I would take a mediator for MVP, just as I can't imagine how I would live without Nlayer in terms of expansion and testing. Why do we need this unnecessary complexity? To setup the events? This can also be done in the classic Nlayer. But we lose visibility and lose auto-click on references. Perhaps this is another "wall" that needs to be overcome, as it was with DI at the time)
@bohdan-belei
@bohdan-belei 4 місяці тому
Thank you for the insightful video! Your explanation of the concepts was excellent. I did notice a dependency on MediatR within both DeleteContributorService and ContributorDeletedHandler in your Core project. This raised a question for me regarding the Dependency Principle in Clean Architecture. Isn't it advisable to steer clear of third-party dependencies within the Core/Domain layer? I'm curious if implementing an Adapter pattern, defining interfaces in the Core and implementing them in the Infrastructure layer, could help maintain cleaner separation. Your thoughts on achieving this balance between design principles and practicality would be much appreciated!
@Ardalis
@Ardalis Місяць тому
I allow dependencies in Core (and SharedKernel) provided they don't impede unit testing or have dependencies on out of process things. So, basically if they're pure functions. MediatR and GuardClauses and FluentValidation and such all fit this and are often useful in the domain model.
@bohdan-belei
@bohdan-belei Місяць тому
Thanks for the answer, Ardalis.
@wadeaashraf2761
@wadeaashraf2761 5 місяців тому
Great job, can i ask you what is the difference between clean architecture and onion architecture and when use any of both ? Thank you for your help
@Ardalis
@Ardalis 5 місяців тому
They're essentially the same. Clean Architecture by the book includes some specific patterns but the focus on keeping dependencies out of your Core/Domain is the main point to follow.
@aiman1562
@aiman1562 Місяць тому
I would super appreciate a dark background. For those many with bad eyes
@shashankshekhar-cl7rd
@shashankshekhar-cl7rd 22 дні тому
I need to create project from scratch in dotnet core 8.0 using entity framework..can i refer this folder structure??we will be creating apis
@bmiller949
@bmiller949 2 місяці тому
I would like the see the requirements documentation that this application satisfies.
@ivanpavlovnorth
@ivanpavlovnorth 5 місяців тому
Clean architecture looks great until we do not touch an event-based communication that generated MediatR which is a very arguable approach to building software because many explicit things become implicit, code transparency (traceability) goes to zero, and the chaoticness of the solution is increased. Separating business logic into a separate independent project is a good approach to organizing code in a large solution, but only with a traditional way of code communication with service and dependency injection.
@liski12
@liski12 5 місяців тому
I know it might sound like a stupid question, but in all the clean architecture diagrams i've seen, there's never an arrow between UI and Infrastructure, and it makes me wonder if it's just to put emphasis on the fact that the dependencies are pointing to the core or is it because I don't understand something and the UI shouldn't reference the Infrastructure?
@obedasante2168
@obedasante2168 5 місяців тому
Yes the UI doesn't need to know about the infrastructure. All the UI should be interested in is data irrespective of the infrastructure. This decoupling of their dependency makes maintenance easy.
@Ardalis
@Ardalis Місяць тому
It's an optional compile-time dependency but a required runtime dependency. For simplicity I usually just include a project reference from UI to Infra but if you do it right you can avoid that and just copy the DLLs from Infra to Web/bin and load the assemblies with reflection at startup and be assured there are NO direct references within UI/Web to Infra types.
@ctwoodwa
@ctwoodwa 5 місяців тому
Has anyone looked into using the wolverine library instead of mediatr?
@Ardalis
@Ardalis 5 місяців тому
Probably trivial to swap out.
@amrswalha
@amrswalha 5 місяців тому
Hello, thanks for the presentation. Where we can access the source code?
@MaxSupercars
@MaxSupercars 5 місяців тому
You got it in video: 25:52 min.
@Paul-uo9sv
@Paul-uo9sv 5 місяців тому
​@@MaxSupercarslink?
@svorskemattias
@svorskemattias 5 місяців тому
Doesn't your specifications have ef core references? Looks like that, with that Include-method
@nikogj9495
@nikogj9495 5 місяців тому
Looks like so, so is the "UseCases" layer for cqrs which is presented as sitting between the core layer and the infrastructure layer, which I find it weird.
@svorskemattias
@svorskemattias 5 місяців тому
@@nikogj9495 specifications belong to the domain/business (he makes no distinction) layer here and he must certainly have a ef core reference there, that is being dependent on infrastructure concerns. Yes, commands and queries belong to the application layer, which sits between infrastructure and domain layer, but only if we are speaking in terms of "compile time dependencies". In runtime, the application layer will orchestrate between infrastructure and domain layers, through the repositories defined in the domain layer, but implemented in the infrastructure layer.
@nikogj9495
@nikogj9495 5 місяців тому
@@svorskemattias What I find weird is having a dependency to ef core on the domain layer. I thought this core layer was supposed to be POCO and dependent on basically nothing else
@svorskemattias
@svorskemattias 5 місяців тому
@@nikogj9495 yes. Then we agree. This domain layer is coupled to infrastructural concerns.
@Ardalis
@Ardalis 5 місяців тому
@@nikogj9495 It would be weird if it were the case.
@weicco
@weicco 3 місяці тому
This if funny since I've never had any interest in "academic" analyses of different software models. I probably don't even know half the names of the modern models. And now, watching this video, it is funny to realize there are actually names for these things I've used for decades. For instance, I've never called anything in my code "aggregates" (except in SQL). There is just bunch of code in services-folder, or whatever, doing stuff to the data and outputting it in some coherent structure. After watching this video, I probably don't remember what "aggregates" are tomorrow. And I don't think I even should because I've found that when you _don't_ follow paradigms to the letter, you have much more flexibility in your code. You just have to communicate (by code comments, documents, somehow) why you deviate from the normal flow here and there. Maybe you need to do something for performance reasons like sometimes I need to use non-normalized databases, which feels icky, but enhances performance a lot in certain situations. But it watch good watching. I think I have to subscribe to this channel.
@TheKevlar
@TheKevlar 5 місяців тому
I like the idea of eliminating Swagger and Postman this makes remote code reviews clean...
@walisonleiria
@walisonleiria 5 місяців тому
Hello! where is the Shared Kernel repository?
@zaighamgulzar1927
@zaighamgulzar1927 Місяць тому
It has been converted into a NuGet package.
@rmbl349
@rmbl349 5 місяців тому
Whats the difference to Jason Taylors Template?
@Ardalis
@Ardalis 5 місяців тому
I haven't looked recently enough to say with confidence.
@jonaszjonasz5752
@jonaszjonasz5752 3 місяці тому
Hi, why didn't you add subtitles to the film?
@sadukie
@sadukie 3 місяці тому
There are subtitles. You may need to press the CC button to enable subtitles/closed captions.
@auronedgevicks7739
@auronedgevicks7739 4 місяці тому
hindsight is 20/20. If you know your domain then great, but software usually isn't built that way. Lots of great unknowns when designing the thing to it's final form
@musigahstallone
@musigahstallone 5 місяців тому
this works like aspire dotnet is that true?
@Ardalis
@Ardalis 5 місяців тому
No, I don't think it's really related.
@brenthompson2
@brenthompson2 5 місяців тому
Tell me more about `IRepository`
@Ardalis
@Ardalis 5 місяців тому
Here's a whole presentation on the subject: ukposts.info/have/v-deo/n2aUnnmZr6dmlY0.html
@Andy-si1pl
@Andy-si1pl 4 місяці тому
Its an interface of an interface
@ttyy544
@ttyy544 4 місяці тому
AHHHHH, it`s so hard to understand this things... I have a project with clear architecture (my clear architecture, that 100 times easier, without commands and...) and want to rewrite it with something better. But this... really hard. I don`t understand...
@Ardalis
@Ardalis Місяць тому
That's why I created the solution template, so you have an easy starting point. (And so I didn't have to set it up every time for a new client's app).
@path_selector
@path_selector 5 місяців тому
hi
@PaulMarangoni
@PaulMarangoni 5 місяців тому
This presentation/video needed to be about three hours longer. You're covering way too much, too quickly. I would appreciate much smaller demo and a much smaller project.
@Ardalis
@Ardalis 5 місяців тому
I also do workshops and online training which obviously goes into greater depth. This is meant to be a quick, dense overview that hopefully delivers something for everyone with minimal fluff.
@MrShurminator
@MrShurminator 5 місяців тому
Interesting concept but definitely overengineered. Later on, adding anything new will require a LOT of plumbing added to all the projects, and it will be redundant. Speak from experience building big systems. The Clean Architecture chart needs more detail - it does not align with the presented list of projects in the template.
@Andy-si1pl
@Andy-si1pl 4 місяці тому
My immediate thought. I still see applications as N-Tier - it makes sense and about everything depending on the database - no it doesn't. You can use interfaces where needed for and use Mocks in your unit tests. Why do people always fall for the new shiny tool when it will be replaced in a couple years...?
@vonn9737
@vonn9737 5 місяців тому
Your queries/handlers and commands/handlers seem unnecessarily complicated. Why not just do this: public interface ISimpleQuery { public class Input { } public class Output { } Task QueryAsync(Input input); } public class SimpleQuery : ISimpleQuery { public async Task QueryAsync(ISimpleQuery.Input input) { throw new NotImplementedException(); } }
@Ardalis
@Ardalis 5 місяців тому
You can certainly do that. You can also simplify your Web API by giving it just one GET method that takes in "input" (via querystring, let's say) and returns "output". There are pros and cons to such dynamic approaches. YMMV. See also GraphQL.
@vonn9737
@vonn9737 5 місяців тому
@@Ardalis I would not do one generic query. I just gave an example of how it could be set up without having ICommandHandler generic. I don't think the generic is necessary.
@Ardalis
@Ardalis 5 місяців тому
@@vonn9737 Like I said, I see no reason why that wouldn't work.
@vladhusnullin1650
@vladhusnullin1650 2 місяці тому
I am so dissapointed when I see somebody coding like this 😂😂😂
@PlottingAnEscape
@PlottingAnEscape 2 місяці тому
"UseCases"... come on dude, what a complete and utter... ANNOYANCE, oh my god... Its Application, you know it, I know it, we all know it, its Application.
@fieryscorpion
@fieryscorpion 2 місяці тому
Clean arch is cancer. We should retire it already.
@user-eo5rq7qy7g
@user-eo5rq7qy7g 5 місяців тому
Complete violation of SOLID principles: massive god-like repository objects and aggregates with side effects. Yikes. In real life all this will become untraceable spaghetti code very quickly: mediator doesn’t help much, there are some devs who still use auto mapper etc. How to maintain context for e.g. serilog and metrics beyond correlation ID? Overall sounds interesting on the surface, the code outcome is unimpressive.
@77Sazaca
@77Sazaca 5 місяців тому
So what type of architecture would you use? Some example?
@MrAyuub22
@MrAyuub22 5 місяців тому
Vertical slice architecture significantly better and way less layers and folders. Better code cohesion@@77Sazaca
@imam4521
@imam4521 5 місяців тому
in all honesty, you are 100% correct, I experienced it myself, things got so complicated that i gave up on everything and just did MVC 🤣
@Ardalis
@Ardalis 5 місяців тому
Use what works for you. This works well for many teams with real world apps. If you have suggestions for things that work better, by all means share them.
@jchandra74
@jchandra74 5 місяців тому
If you don't need it (yet), then you don't need it. Don't overcomplicate your solution unnecessarily. He already mentioned it very early in the presentation of when this might work and when it might not be. If it seems that it is too complicated for you, then most likely you don't need this (yet). More than likely tho, as complexity arises, consciously or unconsciously, you will arrive here. Either by learning about it like this or refactoring to something closer to this organically. 😅
@vladhusnullin1650
@vladhusnullin1650 2 місяці тому
When you see somebody writing DTO in class name all in capitals 😂, no need to look any further 😂😂😂. Bro, start with coding guidelines... Only after that, try to reinvent bycicles like you do.
@CodeTricksDotTips
@CodeTricksDotTips 5 місяців тому
Great Session, i loved it
Full stack web UI with Blazor in .NET 8 | .NET Conf 2023
55:56
dotnet
Переглядів 158 тис.
What's New in C# 12 | .NET Conf 2023
35:28
dotnet
Переглядів 52 тис.
Eurovision Song Contest 2024: First Semi-Final (Live Stream) | Malmö 2024 🇸🇪
2:23:45
BMW просто издевается над нами! Силы на исходе…
1:34:41
ИЛЬДАР АВТО-ПОДБОР
Переглядів 3,6 млн
Crecen las redes 5G en América Latina
15:51
Computer Weekly
Переглядів 2
Vertical Slice Architecture, not Layers!
46:24
CodeOpinion
Переглядів 110 тис.
.NET 7  💥  - Intro to CQRS and MediatR with ASP.NET Core Web Api
1:06:48
Mohamad Lawand
Переглядів 18 тис.
.NET Containers advancements in .NET 8 | .NET Conf 2023
25:38
dotnet
Переглядів 18 тис.
Writing async/await from scratch in C# with Stephen Toub
1:06:02
Нужно ли чистить ПК от пыли?
0:59
CompShop Shorts
Переглядів 96 тис.
Портативная PS 5 🎮 #ps5 #expressly
0:22
ExpresSLY Shorts
Переглядів 170 тис.
Такого вы точно не видели #SonyEricsson #MPF10 #K700
0:19
BenJi Mobile Channel
Переглядів 3,2 млн