Separation of concerns between business layer, data layer and presentation layer without losing information

I’m developing an api in Fast API using sqlalchemy to manage my ORM classes and operations with the database.

I’m dealing with some design decisions to have my classes as little coupled as possible without losing information.

For example, this is one of my entities called Topic
this class defined in models.py and is supposed to be use by the TopicRepository


class Topic(Base):
    __tablename__ = "topics"

    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String, unique=True)
    category_id = Column(
        Integer, ForeignKey("categories.id", ondelete="CASCADE"), nullable=False
    )

    category = relationship("Category", back_populates="topic", lazy="subquery")

I have some “core” classes where this are supposed to live in the business layer and is used by the TopicService, for example

class Topic:
    def __init__(self, id: int, title: str, cost: int = 0, capacity=0, category=None):        
        self._id = id
        self._title = title
        self._category = category
        self._capacity = capacity

   # properties
   # methods for Topic

The presentation layer which is the TopicRouter uses these classes for making the json responses for different kinds of topic request.
schemas.py

class SimpleTopic(BaseModel):
    """Represents a simple topic with just a name"""
    name: str

    model_config = ConfigDict(from_attributes=True)


class TopicRequest(SimpleTopic):
    category: str


class TopicResponse(SimpleTopic):
    id: int
    category: SimpleCategory = Field(validation_alias="category")


class TopicList(RootModel):
    root: List[TopicResponse]

    def __iter__(self):
        return iter(self.root)
 

So, my doubts here are:

  1. Is this kind of separation, ok?
  2. Should the TopicService know the classes from schemas.py, models.py and topic.py in order to mapped from one to the other?
  3. ORM objects should not be used as domain classes so in that case, do I need to duplicate each class?

Thanks! 🙂

2

Usually in this type of scenario you have 3 types of objects:

  • Data Objects
  • Business Objects
  • DTOs (Data Transfer Objects)

From these layers there will be mappers that transform data. Consider a case to get data. The data layer will return objects from the persistence store, a mapper will convert them to business objects, then run any business logic, and then send back resultant DTOs to the client by mapping business objects to DTOs. The reverse can also but true that is data coming in versus data coming out. The mappers themselves do the work of mapping the object to its resultant object. So, each layer only deals with the data it knows about, therefore there is good separation. Usually there is a controller, service, repository layer involved as well. This isn’t the only way to do things but may give you a sense of what you’re trying to do. Usually, DTOs have no logic other than setters/getter and maybe data validation to ensure proper data is passed to the service layer, service level objects may have some built in functionality or that may be delegated to service(s), and data objects typically massage service level objects into the current persistent layer schema. This is typically your ORM.

Data should never be lost because the mappers ensure data is passed correctly between the layers. Mapping itself is isolated as the layers of the application delegate that responsibility to the mappers.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật