# Wrapping ManagedObjectContext

**URL:** <https://forums.swift.org/t/wrapping-managedobjectcontext/41495>\
**Category:** Swift Composable Architecture\
**Tags:** environment, coredata\
**Created:** [October 27, 2020, 6:42am UTC](https://forums.swift.org/t/wrapping-managedobjectcontext/41495 "2020-10-27T06:42:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![eimantas](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/eimantas/32/13145_2.png) [@eimantas](https://forums.swift.org/u/eimantas)\
**Post date:** [October 27, 2020, 6:42am UTC](https://forums.swift.org/t/wrapping-managedobjectcontext/41495/1 "2020-10-27T06:42:15Z")

</div>

I am making a CRUD application.

I have an environment that has a repository object that has MOC. I wonder if I am correct in my assumption that when returning objects from CD store I should convert them to simple DTOs in the `Effect` and return them to the reducer as simple structs (as to hide the dependency from CD). And should I do the reverse when accepting user input from views by accepting DTO into the `Effect` that later then converts it to instance of `NSManagedObject`. Same question applies to the object updates and deletes by object ids.

So basically I wonder if it's an accepted practice of converting DTOs to implementation-dependent objects (CoreData, SQLite, serialized json, etc.) in the Effects **OR** can effects expose the implementation-dependent types to the reducer and the state?

---

<div class="post-metadata">

**Author:** ![roanutil](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/roanutil/32/12060_2.png) [@roanutil](https://forums.swift.org/u/roanutil)\
**Post date:** [November 4, 2020, 4:39pm UTC](https://forums.swift.org/t/wrapping-managedobjectcontext/41495/2 "2020-11-04T16:39:59Z")

</div>

If your repository object is working on a queue other than main, you'll want to use a struct model in TCA and contain NSManagedObjects to the repository. If your NSManagedObjects are equatable and you plan to only use the main queue, there shouldn't be a technical reason to require struct models in Effects.

But I would say it's far preferable to keep NSManagedObjects out of the Reducer, State and Actions. Do all that CRUD work on a serial background queue and use a struct model in all the TCA stuff.
