Hi,

Suppose I have the following ORM class:

class User(Base):

    __tablename__= 'users'

    first_name = Column(String(64), nullable=False)
    last_name = Column(String(64), nullable=False)
    email = Column(String(128), nullable=False)

In our project we now need the full name of a User, as well as her long 
email address (and other data derived from the ORM’s mapped/stored data). 
There are two ways to implement this:

   1. Add @property to the User object.
   2. Add helper functions to a utility module.

The discussion is now going forth and back between these two approaches.

One view is to use @property for the User class:

    @property
    def name(self):
        return self.first_name + " " + self.last_name

    @property
    def long_email(self):
        return "{0} <{1}>".format(self.name, self.email)

because semantically these properties “belong” to a user, keep other scopes 
clean by avoiding unnecessary imports, and they are stateless and 
read-only, and do not affect the underlying data. (Even if they did, the 
ORM should then handle these state changes consistently: e.g. assigning a 
full name to a User object.)

The other view is to use helper functions:

from orm.model import User

def get_name(user: User) -> str:
    return user.first_name + " " + user.last_name

def get_email(user: User) -> str:
    return "{0} <{1}>".format(self.name, self.email)

because @property add responsibility to the User object and an ORM should 
not *create* new data based from its model.

So… what are the opinions of the wider audience here? 🙃

Cheers,
Jens

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to