mardi 20 novembre 2018

Which pattern to use to add functionality to dango models

I have several models in my Django project. Most of these models are quite simple, but some of they are the entry point to complex data structures.

For example, Reservation model has several detail models through foreign key relationships (ReservationHotel, ReservationTransfer, ReservationTour, ReservationRountrip, ReservationService), and each one of these details has foreign relationship with product models (Hotel, Transfer, Tour, Roundtrip, Services).

I know a good practice is to keep models isolated from other models, so they must not know the detail about them. Due to this, I implement controllers to manage complex data structures like the one described before.

I want these controller to implement design patterns, and I believe the proper one for Reservations should be the Proxy pattern, based in its description in wikipedia:

What problems can the Proxy design pattern solve?

The access to an object should be controlled .

Additional functionality should be provided when accessing an object.

When accessing sensitive objects, for example, it should be possible to check that clients have the needed access rights.

What solution does the Proxy design pattern describe?

Define a separate Proxy object that can be used as substitute for another object (Subject) and

implements additional functionality to control the access to this subject.

In my case, I want to create a proxy for model Reservation that would handle also the information for its foreign references, so Reservation model would keep its encapsulation but the proxy would manage all the information related to a Reservartion instance.

Is that a proper use of a Proxy pattern?

Aucun commentaire:

Enregistrer un commentaire