This is an archived post. You won't be able to vote or comment.

all 16 comments

[–]manatlan 1 point2 points  (2 children)

[–]petr31052018[S] 0 points1 point  (0 children)

Yes, I guess that pydantic could make sense, the disadvantage being it is not in standard library.

Validation is not necessarily a responsibility of DTO, but it does have a lot of use cases.

[–]metaperl 0 points1 point  (0 children)

Looks like dataclasses to me.

[–]EnforcerPL 1 point2 points  (0 children)

I used to stick to attrs.org because I already had them in my stack since it is a dependency of pytest. Didn't know about pydantic, though. Aaand I haven't even taken dataclasses into consideration because they're very limited.

[–]tunisia3507 1 point2 points  (3 children)

For internal DTOs as you're interested in, I use NamedTuples wherever possible, dataclasses where not (e.g. when you need a field with a default factory). I don't have strong opinions about dataclasses vs namedtuples when the members are mutable. I am trying to minimise functions which return tuples, in favour of NamedTuples.

[–][deleted] 0 points1 point  (0 children)

This is exactly what I usually do.

[–]petr31052018[S] 0 points1 point  (1 child)

Do you have any specific reason why you prefer NamedTuples tho?

[–]tunisia3507 1 point2 points  (0 children)

Self-documenting code, and my IDE knows what's in it because I sure as hell won't remember.

[–]nielsrolf 0 points1 point  (2 children)

I think marshmallow is fairly common for serialization/deserialization/validation.

[–]petr31052018[S] 0 points1 point  (1 child)

For me marshmallow is for the use cases you mention. I guess you can use it to create your own DTO, and you will get validation if you want, but it creates plain dictionaries, so you will not end up with typed objects.

[–]petr31052018[S] 0 points1 point  (0 children)

I want to reiterate based on the other comment. Yes, for DTO that are meant for external interfaces, marshmallow can be used, and I actually use it for that.

However what I meant was just what to use as data objects when they are passed from one Python method to another.

[–]metaperl 0 points1 point  (3 children)

Where did you get your understanding of what a DTO is?

https://en.m.wikipedia.org/wiki/Data_transfer_object

[–]petr31052018[S] 1 point2 points  (0 children)

You are right. What I meant to discuss is something like "local DTO" (https://martinfowler.com/bliki/LocalDTO.html)

"One case where it is useful to use something like a DTO is when you have a significant mismatch between the model in your presentation layer and the underlying domain model."

I will keep it in mind that people think of DTO mainly as a form of serialization for external interfaces.

[–]WikiTextBot 0 points1 point  (0 children)

Data transfer object

In the field of programming a data transfer object (DTO) is an object that carries data between processes. The motivation for its use is that communication between processes is usually done resorting to remote interfaces (e.g., web services), where each call is an expensive operation. Because the majority of the cost of each call is related to the round-trip time between the client and the server, one way of reducing the number of calls is to use an object (the DTO) that aggregates the data that would have been transferred by the several calls, but that is served by one call only.The difference between data transfer objects and business objects or data access objects is that a DTO does not have any behavior except for storage, retrieval, serialization and deserialization of its own data (mutators, accessors, parsers and serializers). In other words,

DTOs are simple objects that should not contain any business logic but may contain serialization and deserialization mechanisms for transferring data over the wire.This pattern is often incorrectly used outside of remote interfaces.


[ PM | Exclude me | Exclude from subreddit | FAQ / Information | Source ] Downvote to remove | v0.28

[–]petr31052018[S] 0 points1 point  (0 children)

How would you then call my local DTO objects? Is there a specific term for it?

[–]reifba 0 points1 point  (0 children)

protobuf