Skip to content

Instantly share code, notes, and snippets.

@ferminhg
Created May 22, 2020 09:59
Show Gist options
  • Select an option

  • Save ferminhg/da2302dc25e0dca3d2aa87c1819809f2 to your computer and use it in GitHub Desktop.

Select an option

Save ferminhg/da2302dc25e0dca3d2aa87c1819809f2 to your computer and use it in GitHub Desktop.
#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