jeudi 6 juillet 2017

ORM Classes and Separation of Concerns

I am using Python's peewee ORM to map my relational database, but I'm noticing that my models are starting to get bloated and I also seem to be violating separation of concerns.

For example, my User class has self explanatory methods such as

authenticate()
push_message()
upload_image()

Even though it's useful to call user.authenticate() or user.upload_image(), these methods involve using many different packages and/or third party API's and I'm wondering if it would be better to separate them from the model.

One way I can think of fixing this is by moving all these methods into another layer( and separate that layer even further for the various domains ) and use them like so:

authenticate_user( user )
push_user_message( user )
upload_user_image( user )

But this seems contradicting to the utility of OOP. Another way maybe is to combine my original idea and use an interface of sorts?

class UserAuthentication( object )
    # authentication code goes here

# These would be in separate packages and/or modules
class User( model ):
    def authenticate( self ):
        return UserAuthentication( self ).authenticate()

But this may be introducing extraneous complexity.

Aucun commentaire:

Enregistrer un commentaire