I have a set of ORM models that are shared between the main business application and a couple minor side applications (such as an administrative web interface).
I don't want to put the object's business logic for the main application inside the ORM model classes because the web interface doesn't use any of it (and it would be too bloated).
That leaves me with the problem of having two classes for every real "object" (the business layer class and the ORM model class), and wondering how I should link the two. Either composition or inheritance would work, but both feel wrong. For example, I have a User class and a DBUser ORM model class. User "is not" a DBUser and a User "does not have" a DBUser.
Is there a standard solution or best practice to address this predicament? Or is this a case where there is no great answer and I just have to pick the one that makes me the least uneasy?
Here is a code example, just in case the above wasn't clear:
class DBUser(SQAlchemyBase):
__tablename__ = 'users'
user_id = Column(Integer, primary_key=True)
username = Column(String, nullable=False)
# ...
class User(object):
def __init__(self, user_id):
self.dbuser = db.query(DBUser).filter(DBUser.user_id == user_id).first()
@property
def username(self):
return self.dbuser.username
@username.setter
def username(self, username):
self.dbuser.username = username
def connect_to_server(self, server):
...
def save(self):
db.add(self.dbuser)
db.commit()
db.detach(self.dbuser)
def disconnect_from_server(self):
...
def handle_incoming_action(self, action):
...