How to structure a layered architecture ASP.Net Core project in a (almost) clean way

Поділитися
Вставка
  • Опубліковано 14 чер 2024
  • Sometimes the hardest thing is to get started! That's why in this video we describe how to structure a layered architecture ASP.Net Core project from scratch. During this video we start from a blank solution and aim to refactor the Asp.Net Core Web API template towards a layered architecture in a (almost) clean way. When creating each layer, we also go through the thought process of what belongs to each layer. in the end, we wire everything up together, taking advantage of the Asp.Net Core dependency injection container and the power of abstractions through interfaces.
    Join this channel to get access to perks:
    / @codewrinkles
    1. Why should we start from a blank solution? 00:00
    2. Creating Visual Studio projects for each layer 03:59
    3. Creating the domain layer 04:21
    4. What should we put in the domain layer? 06:30
    5. Creating the Data Access Layer (DAL) 09:52
    6. What should we put in the DAL? 10:30
    7. Creating the service layer 12:06
    8. What should we put in the service layer? 13:27
    9. Creating the presentation layer (ASP.Net Core Web API in this case) 15:56
    10. Refactoring the domain layer 17:56
    11. Implementing the DAL 27:15
    12. Implementing the service layer 30:04
    13. Wiring everything up with the ASP.Net Core dependency injection container 36:47
    14. Summary 42:45
    Playlist containing the mentioned EF Core videos: • Using EF Core in Blazo...
    #aspnetcore #softwareArchitecture #dotnet
  • Наука та технологія

КОМЕНТАРІ • 89

  • @Mohamed-pu7so
    @Mohamed-pu7so 27 днів тому +1

    You're explanation is insanely awesome, no one talks about the n-layered architecture like you! they often put their domain models inside the Data Access layer!!! Not like you separated the domain model into a specific layer and made it below the Data Access layer so BAL and DAL both can use it. Thank you, sir.

  • @saadatmane9101
    @saadatmane9101 2 роки тому +9

    Very helpful for me. I will start using this method in my future development, it's clear and modern architecture.

    • @Codewrinkles
      @Codewrinkles  2 роки тому +2

      In the mean time, there are also videos on a pure clean architecture. You might also want to check that out.

  • @matburtcher8359
    @matburtcher8359 Рік тому

    Thank you so much for this video - it really clarified a lot for me. I arrived here out of frustration with my own "Fat Controllers" on a Web App MVC project: I knew my logic should go somewhere else but didn't know where to begin. This is the answer! Really appreciate you sharing your experience.

  • @dimags500
    @dimags500 2 роки тому +3

    Pure gold ! very informative and helpful , Thanks

    • @Codewrinkles
      @Codewrinkles  2 роки тому +1

      I'm glad you find this content useful.

  • @aliesmaeili7433
    @aliesmaeili7433 5 місяців тому

    Thank you for this straightforward, practical and simple course. I'm new in asp and it's helped me to learn Clean and Layer Architecture both for my new project.

    • @rasheed9947
      @rasheed9947 5 місяців тому

      did u start your new projects ? im gonna start my project with this Architecture we can discuss

  • @CheenaSingh
    @CheenaSingh 2 роки тому +2

    Very useful, I think I am going to have to watch this a few times and then code and example for it to stick. Many Thanks.

  • @hurlok
    @hurlok Рік тому

    Very good explanation, thank you!

  • @yasinbastug6753
    @yasinbastug6753 2 роки тому +2

    thank you so much, cleared lots of questions from my mind

  • @mohammedkarem9709
    @mohammedkarem9709 Рік тому

    This is a great tutorial. I really appreciate your effort.

  • @jeroenbakker2350
    @jeroenbakker2350 2 роки тому

    Very informative!

  • @giorgididebulidze8394
    @giorgididebulidze8394 Рік тому

    very helpful, THANKS man

  • @tankhangvan6272
    @tankhangvan6272 5 місяців тому

    Thank you for your video. First time i'm approaching this kind of information, cuz re-skill from embedded devloper to web developer.

  • @mahmoudzeort7078
    @mahmoudzeort7078 Рік тому

    Great explanation ! thank you .

  • @kpadhnan
    @kpadhnan Рік тому

    Thank you so much for this. Very useful

  • @CarrigansGuitarClub
    @CarrigansGuitarClub Рік тому

    Really really good explanation - smart guy!

  • @Zeuchon
    @Zeuchon 6 місяців тому

    Very good content there ! - thanks for making this

    • @Codewrinkles
      @Codewrinkles  6 місяців тому

      Glad you enjoy my content. Thanks for watching

  • @mohdaman7958
    @mohdaman7958 2 роки тому +2

    I am very thankful to you, It is very helpful for me. Could we apply Data first approach using Entity framework with PostgreSql DB for same structure?, And it is giving me a circular dependency error. Thanks

  • @nadikakpathirana
    @nadikakpathirana 6 місяців тому

    Thank You...

  • @sayurirathnayake291
    @sayurirathnayake291 Рік тому

    thank you! ❤

  • @saqibali7066
    @saqibali7066 3 місяці тому

    Thanks. Loved the video for every minute . Just curious if can have code access ? Thanks again

  • @davidsanderson625
    @davidsanderson625 2 роки тому +1

    Hi - Thank you for a very useful introduction and example of this architecture. I have seen comments elsewhere that the Web or API Layer should not have a dependency on the DAL. Is there any alternative way to Inject the Repository in Startup (or elsewhere) without having an explicit reference to the DAL project?

    • @Codewrinkles
      @Codewrinkles  2 роки тому +3

      If your only dependency is in the Startup class or in Program class (if .NET6), then that's totally acceptable by all common sense and best practices. Your dependency is only that you add them to your DI container. An alternative would be using 3rd party DI library and place it in a "common" project, something similar to a shared kernel in DDD terms. But that would be an overhead that would not justify just a compile time dependency. I'm not sure where you have seen or read this, but it's the first time that I can say that it's total nonsense and an advice you should not follow.

  • @lamnguyenhuu7400
    @lamnguyenhuu7400 2 роки тому

    Thank you so much :)

    • @Codewrinkles
      @Codewrinkles  2 роки тому +2

      You're welcome. A video on structuring a clean architecture project will also come these days.

    • @lamnguyenhuu7400
      @lamnguyenhuu7400 2 роки тому

      @@Codewrinkles love to know that

  • @mohammadsj
    @mohammadsj Рік тому

    Is it okay to use multiple classes for Repository? I mean if a domain is too large and have couple of sub-domains, Is that okay to use different implementation of Repository for different sub-domains?

  • @philipabraham278
    @philipabraham278 Рік тому

    Great video!
    I have been looking into the service layer pattern for a bit. Would you say that this standard layer architecture somewhat adheres to that pattern?

    • @Codewrinkles
      @Codewrinkles  Рік тому

      Yes, it's kind of standard. There are a lot of projects out there that run just fine and use services. However, my preference goes towards a more decoupled approach using CQRS,

  • @nightgamer8953
    @nightgamer8953 10 місяців тому +1

    Hello Dan, First of all Thank you for the video. I have few questions ,
    1) In which layer should DTO Models be used?
    2) How do we handle complex JOINS on multiple entities? If we do Joins, should we return IQueryable for GetAll Methods from Repository? ( Case - Db First Approach without Scaffolding )
    3) Where are Exceptions thrown? If we use Global Exception Handling, what happens? Is it a good idea to throw exceptions in the Service Layer?
    4) Where should Custom Exceptions Classes go?
    5) Where should the Request Model Validators Extension be located? (FluentValidation)
    Please assist me because I am about to use this Layer for our upcoming project (smaller one)
    Please advise if this is a good architecture to use.

    • @dimasik3108
      @dimasik3108 7 місяців тому

      Did you find answers for these questions? If so could you pleace provide them because I faced the same problem but can't find anything that would help to solve it

  • @user-ls9vi8io5y
    @user-ls9vi8io5y 9 місяців тому

    hi good video,
    how to insert update the api data into my database where we need to handle it.
    do my controller need to access dal layer to save the entities or the service after all the business logic are moved to service

  • @rasheed9947
    @rasheed9947 5 місяців тому

    in this architecture Where would we put the Dtos? or where ot map it

  • @EshantKapoor87
    @EshantKapoor87 Місяць тому

    Veryuseful, can you help or guide to add Dapper ORM into it

  • @moatasemkremed5395
    @moatasemkremed5395 2 роки тому +1

    Where can we apply identity operations such as adding user permissions and rules?
    Can we add it to a reusable repository??
    Its connection string, especially in Blazor projects?

    • @Codewrinkles
      @Codewrinkles  2 роки тому +2

      Identity operations are for sure part of the domain model. The model classes you need for that belong therefore to the domain layer. The repositories for it belong to the DAL and Idnetity Services belong to the services layer. For Blazor Server there's really no difference. For the client, you should never work with connection strings on the client. Information should be exposed via the API and authentication and authorization will have to follow OpenID Connect and OAuth 2.0 standards.

  • @bayyajribi9465
    @bayyajribi9465 Рік тому

    This was very interesting, thank you for explaining. I have a question though, can we use the same architecture with ASP.NET Core Web Application MVC ? I'm trying to build a Web Application and I need it to be well structured.

    • @Codewrinkles
      @Codewrinkles  Рік тому

      Sure you can. It really doesn't change anything. You'll just have views and viewmodels on your Presentation layer. But otherwise, nothing changes.

  • @crazydreamer2324
    @crazydreamer2324 Рік тому

    Thank you so much
    I have an one doubt, where will be our dbcontext in your scenario? In DAL or in domain?

  • @gameraya1370
    @gameraya1370 2 роки тому

    Thank you for the video, i have a question, how i can implement a WeatheropenApi or other external Api to this architecture.

    • @Codewrinkles
      @Codewrinkles  2 роки тому

      I'm really not sure what you are looking for. On this channel you have a lot of implementations with this pattern. Go to the ASP.NET Core for absolute beginner playlist, and there we implement an API following this architectural pattern. All the playlists that contain live coding sessions implement the different architecture or the other. Why does it need to be a weather API?

  • @vaeluck01
    @vaeluck01 Рік тому

    Thanks so much, your explanation cleared my confusion of many days. In the case that I need to interact with the file system or email or Sms, where does not functionalities go? Do I create another layer; may be Common Layer or add the functionalities to the Service layer?

    • @Codewrinkles
      @Codewrinkles  Рік тому +1

      Those would be regular services in your service layer. You would have a FileService, an EmailService, an SmsService and so on. No need to create additional projects/layers for that unless your existing project gets overwhelming.

    • @vaeluck01
      @vaeluck01 Рік тому

      @@Codewrinkles Thanks a lot. Gracias!

  • @mohamedal-qadeery6530
    @mohamedal-qadeery6530 2 роки тому

    Thanks for the video .. now if i want to use a js framework like Angular its going to be in api layer right ?

    • @Codewrinkles
      @Codewrinkles  2 роки тому +2

      No, that would be a total different layer that actually doesn''t have to do anything with the API. You can think about this as a totally different application. I know that in the standard VS template for ASP.NET Core with Angular, the API and Angular is kind of in the same project. But that's totally wrong from my point of view because it introduces a lot of misunderstanding.

    • @mohamedal-qadeery6530
      @mohamedal-qadeery6530 2 роки тому

      @@Codewrinkles Thank you for replaying .. yea thats why i got confused

  • @EshantKapoor87
    @EshantKapoor87 Місяць тому

    Any link for @Dapper ORM.

  • @CarrigansGuitarClub
    @CarrigansGuitarClub Рік тому

    Where should my EF database models go - in the DAL or the Domain?

    • @Codewrinkles
      @Codewrinkles  Рік тому

      If you are using dedicated models for EF, then for sure in the data access layer. But usually when we work wit code first we simply re-use our domain models also for EF. As the data access layer will already have a dependency on the domain layer, this should not be any problem.

    • @iamjosedev
      @iamjosedev Рік тому

      @@Codewrinkles Hi, what about the EF dbcontext? Inside the models folder of the Domain layer or in the DAL layer? Thanks.

  • @dotnetdevni
    @dotnetdevni Рік тому

    Where should your db context live in this type of project ?

    • @Codewrinkles
      @Codewrinkles  Рік тому

      The DbContext should always be where you have your data access logic. Usualy that's a dedicated DAL/persistence project.

  • @sibaprasaddalai3775
    @sibaprasaddalai3775 2 роки тому

    It was very very informative.. 👍 ?As i have heard in the market there are onion, Domain driven , CLEAN architecture but not aware how these works, does this architecture in the video has any name?

    • @Codewrinkles
      @Codewrinkles  2 роки тому +2

      It's the standard layered architecture. You can also hear about it under n-layer or n-tier architecture.

    • @sibaprasaddalai3775
      @sibaprasaddalai3775 2 роки тому

      @@Codewrinkles Thank u so much for ur quick reply. Loved ur explaination and gonna follow this architecture . 👍

    • @Codewrinkles
      @Codewrinkles  2 роки тому +4

      @@sibaprasaddalai3775 My plan is to cover most of the architectures and how we set them up in our VS solution. Hope I'll be able to publish one on clean architecture very soon.

    • @trickytechnology6749
      @trickytechnology6749 Рік тому

      @@Codewrinkles going to subscribe you.... And will be going to follow your each and every video.

  • @mohamedal-qadeery6530
    @mohamedal-qadeery6530 2 роки тому

    at 46:11 shouldn't the service layer have dependency on the data access layer too ?

    • @Codewrinkles
      @Codewrinkles  2 роки тому +1

      It depends. If you are following 100% clean architecture, then it shouldn't. If you go mostly for a CQRS pattern, then I personally don't see any problem working directly with the DbContext in each handler. That would introduce a dependency, but in this very isolated approach where each action in the application is handled by exactly 1 handler this is not a big problem anymore.

    • @mohamedal-qadeery6530
      @mohamedal-qadeery6530 2 роки тому

      @@Codewrinkles i followed your beginner playlist but in the service layer i had a dependency to the Dal layer because we were accessing Datacontext in ResveationService class ,, is it wrong ? and how i can solve it or is there another way to do it ?

    • @Codewrinkles
      @Codewrinkles  2 роки тому +2

      @@mohamedal-qadeery6530 To be honest I don't recall exactly how it was set up during the tutorial and maybe there were some pedagogical reasons why I did that, but usually in the Service you would Inject an IReservationRepository and you would inject the DbContext in the repository. This way, the Service layer would not have a direct dependency on the DAL. It would use just interfaces defined in the domain/core layer.

    • @mohamedal-qadeery6530
      @mohamedal-qadeery6530 2 роки тому

      @@Codewrinkles Thank you so much its clear now :)

  • @popeandrei6274
    @popeandrei6274 11 місяців тому

    Hi! Shouldn't you use your own models on the web? If you were to use IMapper, where should it be placed? using it on the web, each page/controller will repeat the same logic. Going over that, for POST requests you will directly use the entity from Domain due to the argument of that type from Service. I am facing this problem, I am looking for a way to separate it as clean as possible

    • @Codewrinkles
      @Codewrinkles  11 місяців тому +1

      In my view, you should never use your domain models as API contracts.

  • @Uzair_Anwar2299
    @Uzair_Anwar2299 2 роки тому +1

    What about in case of reverse engineering. In case of reverse engineering I am not able to create domain and context class in different class libraries. Please give me solution for this.

    • @Codewrinkles
      @Codewrinkles  2 роки тому

      Sorry, but I don't think I understand your problem. Can you please elaborate and provide a concrete example of what you mean?

    • @MahmoudSaed98
      @MahmoudSaed98 7 місяців тому

      Did you find a solution? @@Codewrinkles

    • @MahmoudSaed98
      @MahmoudSaed98 7 місяців тому

      Did you find a solution?

  • @bswill5077
    @bswill5077 Рік тому

    Thank you sir but can you please move your head on the screen so we can see the other files on the right side bottom. You can just use your voice while doing a tutorial. Great job on the tutorial tho :) thank you

  • @subratadey3943
    @subratadey3943 Рік тому

    Code you please provide me the source code?

    • @Codewrinkles
      @Codewrinkles  Рік тому

      I'm thinking on ways to make the source code available in the near future. Please stay tuned.

  • @dotnetdevni
    @dotnetdevni Рік тому

    Why are you calling interfaces abstractions is that not confusing due to the abstract class type

    • @Codewrinkles
      @Codewrinkles  Рік тому +1

      Both interfaces and abstract classes are abstractions. I think that's something all developers should get accustomed with.

  • @albxdotcom
    @albxdotcom 6 місяців тому

    In your opinion, in this architecture, would you include Models which are not persisted to the database in your Domain layer as well? For example a WeatherSummary class which had less properties than the WeatherForecast and may be a return type from your Service Layer.

    • @Codewrinkles
      @Codewrinkles  6 місяців тому +1

      I think that there is a very common misconception that there should be some kind of mapping between what is persisted and what should be in the domain model. When I'm writing my domain, I don't care about persistence. I just care about modeling business behavior. What gets then persisted to the database is a whole different story. So, the answer is, yes, if WeatherSummary is part of business behavior, I would still put it in the domain. But from the name of this suggested class it seems to me that it is more related to the presentation of the data to a consumer and hence I would probably consider placing it in the presentation/API layer as some kind of DTO or ViewModel. My service layer would return the domain objects. The presentation layer is then responsible to aggregate them based on what needs to be shown on the UI or returned from the API.

    • @albxdotcom
      @albxdotcom 6 місяців тому

      @@Codewrinkles Thank you for the response, this makes sense. So in your opinion, you could return both domain models and other classes from the Service layer. Some of these classes would be persisted to the database, while others may simply be models used for transferring data in between the layers.