Created
May 22, 2020 09:59
-
-
Save ferminhg/da2302dc25e0dca3d2aa87c1819809f2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #clean architecture layers | |
| # https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html | |
| # ^^you can read read here the beneficts of use this patterns to write better code | |
| # The dependency rule (->): dependencies can only point inwards | |
| # Infrastructure -> Application -> Domain | |
| # Summary: my business logic (domain) don't know infra or apliccation layers | |
| # Use case summary | |
| # Given a slug | |
| # I search a Product Entity by slug | |
| # If not exists I will send a 404 response | |
| # If exists I generate a template with product | |
| # I reponse with http page | |
| #I added (I infra) (A Application) (D Domain) to know how acts | |
| def product_detail(request, slug): | |
| try: | |
| product = Product.objects.get(slug=slug) # (I Framework, Repository, Database) (D business logic) | |
| except Product.DoesNotExist: # (D bussines logic) | |
| raise Http404() # (A presentation layer) (I framework, message bus) | |
| return TemplateResponse(request, 'products/product_detail.html', { # (I UI) (A presenters) | |
| 'product': product, | |
| }) | |
| # Shorter version | |
| def product_detail(request, slug): | |
| return TemplateResponse(request, 'products/product_detail.html', { # (I) (A) | |
| 'product': get_object_or_404(Product.objects.all(), slug=slug), # (D) (I) (A) | |
| }) | |
| # As you see you are very couple to infra and application layers | |
| # if something in your use case will change tomorrow, you need to rewrite all | |
| # On the other hand, if one layer will changes (database, framework, business logic, json response) | |
| # it will not affect others layers and you have a scalable code and also scalable projects |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment