Generate plain domain models without SeaORM-specific code (Hexagonal Architecture Context) #2533
Replies: 1 comment
-
|
SeaORM codegen does not generate “pure domain” models directly. The generated entities are meant to be persistence/infrastructure models, so they will contain For hexagonal architecture I’d keep two model layers: Example: // domain/path_segment_type.rs
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PathSegmentType {
pub id: i32,
pub name: Option<SegmentTypeName>,
pub description: String,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SegmentTypeName {
Directory,
Lesson,
}Then convert from the SeaORM model in the infrastructure layer: impl From<entity::path_segment_type::Model> for domain::PathSegmentType {
fn from(model: entity::path_segment_type::Model) -> Self {
Self {
id: model.id,
name: model.name.map(Into::into),
description: model.description,
}
}
}And same idea for enums. So imo the clean approach is:
Generating plain structs automatically would be possible with a custom script/template, but I wouldn’t use generated DB models directly as domain models in hexagonal architecture. Keeping the mapping explicit is a bit more code, but it keeps the domain independent and much easier to test/change later. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I am currently working on a personal project to learn Rust. In this project, I am building a web application backend using SeaORM. I am trying to implement a hexagonal architecture, with two main folders: domain and infrastructure.
I’m not an expert in hexagonal architecture, but here is how I understand the responsibilities of these two parts:
The reason for my question is the following:
When I run sea-orm-cli generate entity, I get entities that are tightly coupled with SeaORM (with traits like DeriveEntityModel, ActiveModelBehavior, and annotations like #[sea_orm(...)]).
For example, the generated code looks like this:
What I would like is to automatically generate a corresponding domain file, without any SeaORM-specific code, looking something like this:
n other words: I’d like to generate plain Rust structures and enums matching the entities, but without SeaORM attributes or traits.
I’ve checked the documentation and couldn’t find any option or flag to achieve this.
Is there already a way to do this with SeaORM, or is there any tool/feature planned that could help with this use case?
Thank you very much for your help!
Beta Was this translation helpful? Give feedback.
All reactions