【问题标题】:Separation between data and logic for sqlalchemy modelssqlalchemy 模型的数据和逻辑分离
【发布时间】:2018-07-09 01:42:42
【问题描述】:

在下面的示例中,我应该将 get_friend 方法保留在我的类中还是应该将它移到外面,因为我想将数据结构与业务逻辑分开? 最佳做法是什么? 不要因为班级小而有偏见,这只是一个例子,但班级可能会更大。

from sqlalchemy import Column, Integer, String, Float, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

class User(Base):
    id = Column("id", Integer, primary_key=True)
    friends = relationship("Friends")

    def get_friend(self, friend_name):
        for friend in self.friends:
            if friend.name == friend_name:
                return friend
        return None

【问题讨论】:

    标签: python python-3.x sqlalchemy data-access-layer business-logic-layer


    【解决方案1】:

    我从没用过 python,只用 DAL 和 BLL 来回答这个问题。

    User 是你的持久模型,如果我没记错的话。理想情况下,它应该只处理持久性问题。它不应该处理域逻辑;该部分应移至业务逻辑层或您的服务类。

    好吧,对此没有任何硬性规定;但有“良好做法”/“建议”/“范式”/“模式”。这些是根据大型社区的更广泛经验定义的。

    因此,您最好考虑将该方法移至您的 BLL。

    【讨论】:

      猜你喜欢
      • 2015-05-28
      • 2012-08-02
      • 1970-01-01
      • 1970-01-01
      • 2015-08-18
      • 2013-06-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多