Should a value object in a public API pack also be public?

This message was imported from the Ruby/Rails Modularity Slack server. Find more info in the import thread.

Message originally sent by slack user U712MPD2QOT

If there is a method in the public API of a pack that returns a value object, should the value object also be public in that pack? I could see the argument for it that changing the value object internals affects everything that depends on that pack.

Message originally sent by slack user U716MMIE3T4

I would say yes.

If you expect the client code to directly use methods of the value object, then that value object feels like a part of the public API.

As a potential alternative, you can think of a way to limit the ability to call value object methods. You could create an intermediate public layer, that exposes a subset of the value object methods. It increases complexity, but hides internal details of the implementation. However, with the value object, I suspect that would be a bit of an overkill :wink:

I hope that’s helpful - I’m really interested in hearing other people’s opinions!

Message originally sent by slack user U70DMIK7VPU

Take a look at https://github.com/santib/ar2dto gem, you can find it useful for easily converting ActiveRecords objects to Value Objects (or DTOs) :muscle:

Message originally sent by slack user U71V6G2PDJS

Is this question particular to value objects? What if the API returns an ActiveRecord object? Or some other type?

It’s about return types in general, yes.