# Post-virtus models using Dry::{Struct,Types}

**URL:** <https://discourse.dry-rb.org/t/post-virtus-models-using-dry-struct-types/277>\
**Category:** Uncategorized\
**Created:** [May 26, 2017, 11:18am UTC](https://discourse.dry-rb.org/t/post-virtus-models-using-dry-struct-types/277 "2017-05-26T11:18:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbuehlmann](https://avatars.discourse-cdn.com/v4/letter/t/df788c/32.png) [@tbuehlmann](https://discourse.dry-rb.org/u/tbuehlmann)\
**Post date:** [May 26, 2017, 11:18am UTC](https://discourse.dry-rb.org/t/post-virtus-models-using-dry-struct-types/277/1 "2017-05-26T11:18:05Z")

</div>

Hey there,

I’ve been using virtus for writing models that are populated by consuming an JSON API in the past and it’s been quite a good experience. As its README states, dry-types, dry-struct and dry-validation “should be considered as virtus’ successors, with better separation of concerns and better features”.

I’m giving Dry::Struct and Dry::Types a shot right now and I’m a bit baffled that Dry::Struct doesn’t include attribute setters. I already read that a Dry::Struct should be treated as immutable and that it is not designed to have its state mutated over time ([https://github.com/dry-rb/dry-types/issues/106](https://github.com/dry-rb/dry-types/issues/106)).

What’s your opinion on that, considering that virtus indeed has attribute setters?

I have to admin that I’m used to Active Record and its all-in-one-approach, so virtus having attribute setters is kind of familiar. Let’s say I’d want to write an JSON API client, when having to update a model, how would you do it? Always use the repository pattern, provide the new attributes as a hash and work with the result like this?

```
project_repository = ProjectRepository.new
project = project_repository.find(42)
updated_project = project_repository.update(project.id, {name: 'A New Name'})

```

Would love to read your thoughts!

Best regards, Tobias

---

<div class="post-metadata">

**Author:** ![flash-gordon](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/flash-gordon/32/494_2.png) [@flash-gordon](https://discourse.dry-rb.org/u/flash-gordon)\
**Post date:** [May 28, 2017, 10:17am UTC](https://discourse.dry-rb.org/t/post-virtus-models-using-dry-struct-types/277/2 "2017-05-28T10:17:09Z")

</div>

Hi,

Well, yes, I don’t mutate values in Ruby and use ROM for persistence like you showed. Since version 0.3 dry-struct has a shortcut for building new structs based on existing ones, it called … `new`, you can use it like this `project.new(name: 'A New Name') #=> struct with a new :name value`

---

<div class="post-metadata">

**Author:** ![tbuehlmann](https://avatars.discourse-cdn.com/v4/letter/t/df788c/32.png) [@tbuehlmann](https://discourse.dry-rb.org/u/tbuehlmann)\
**Post date:** [May 28, 2017, 6:59pm UTC](https://discourse.dry-rb.org/t/post-virtus-models-using-dry-struct-types/277/3 "2017-05-28T18:59:52Z")

</div>

Hey,

so having immutable objects here is a rather personal preference, right? I can live with that, thanks for the input!

About the new method: Yeah, I saw it while digging the library’s code, good to know!

---

<div class="post-metadata">

**Author:** ![flash-gordon](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/flash-gordon/32/494_2.png) [@flash-gordon](https://discourse.dry-rb.org/u/flash-gordon)\
**Post date:** [May 30, 2017, 4:12pm UTC](https://discourse.dry-rb.org/t/post-virtus-models-using-dry-struct-types/277/4 "2017-05-30T16:12:48Z")

</div>

> [@tbuehlmann](#):
>
> so having immutable objects here is a rather personal preference, right?

Well, yes. But we build the stack that can operate without mutability at all.
