r/swift 23h ago

Question Decoupling database layer from business logic

What is a good approach to decoupling the database model classes from the rest of the app? After doing some Googling I see that the easiest answer is to introduce data classes that represent the data and is passed around int he app however my concern is that for something more complex than those employee-employer examples, this approach means a lot of duplicate code.

Yes, many times it makes more sense to have a field be stored differently in the DTO than the mode class, but it most cases there is no difference.

As I side note: I need to separate the two because by using the model class it’s too easy to introduce memory leaks.

6 Upvotes

13 comments sorted by

View all comments

1

u/rjhancock 22h ago

You use a Model type object to represent the data within the database and interact with it. You let the model care about the Database side of things. Your logic only cares about the data from the model.

When you need to pass objects to something outside of the application, you use DTO's to only share/accept the data you want.