mjtamlyn / django-adapters

Constructible, introspectable serializers and forms
BSD 3-Clause "New" or "Revised" License
69 stars 7 forks source link

Question about the adapters pattern #39

Closed jpic closed 6 years ago

jpic commented 6 years ago

Considering my current understanding of the adapters pattern - waiting to stand corrected - we're going to define adapter mapping trees.

Will it exclusively go in one way, ie:

from django.db.models import Person
# add returns a clone of the payload, so we're instanciating 2 payloads here
# because the FormView adapter's post_add() will add(Form adapter, clone=False)
# if it's not already there ? 
p = Payload.factory(instance=Person()).add('django.views.ModelFormView')
# executing a step returns a clone of the payload, but we don't care:
# we have an adapter mapping on data, a request, and we want a response
assert p.steps.response(request=request).response

The tutorial demonstrates how the above would be possible, but it might look like this (not tested code, obviously clumsy):

Or, will it allow to build a nested adapter map, and then be able to generate a model class with another adapter ?

class Hobby(adapters.Payload):
    name = Payload(adapters=[StringAdapter()])

class Person(adapters.Payload):
    hobbies = Payload(map=[HobbyAdapter()])
    class Meta:
        adapters = [OnlyAllowHobbiesToBe('archery', 'django', 'music')]

p = Person().add('django.db.models.Model')
# custom step by django model adapter, optinal, sets payload.model if not already present
p.steps.modelize().model

Another possibility is to make everything an adapter, which can have adapters who know about their parent, in which case steps also are adapters, just they orchestrate the adapters which are in a mapping structure, and defining a step is just defining a method which may depend on methods priorly executed.

Sorry if this doesn't make any sense please correct me ;)

If that makes sense to you then you probably understand why i consider this million $ worth, in terms of refactoring, and code reusability.

jpic commented 6 years ago

Closing this for now it's not supported