Drongo_III Posted November 3, 2021 Share Posted November 3, 2021 (edited) I'm working on a new project and need to create DTOs for use in views (this is a Laravel project so MVC). The DTO will get created from subsets of values from 2 other models (Price, Quote). Obviously, it wouldn't be ideal to build the DTO from those models in multiple places so I want to seat that logic in a specific class. I figure an approach is to use a factory - i.e. as a place to capture the creation of the DTO. But when I read into Factory Method and Abstract Factory and Simple Factory this use case doesn't sound like it fits. So my questions are: Is a factory the right pattern? If so, which flavour of factory would you suggest? Is this the way you would approach it? I appeal to the more experienced minds here for some insight. I work alone so it's hard sometimes to figure out the right approach. thanks, Drongo Edited November 3, 2021 by Drongo_III 1 Quote Link to comment https://forums.phpfreaks.com/topic/314162-to-factory-or-not-to-factory/ Share on other sites More sharing options...
requinix Posted November 3, 2021 Share Posted November 3, 2021 Need more details. Or try from another angle. What kind of code do you want to write in order to get the data you need? Quote Link to comment https://forums.phpfreaks.com/topic/314162-to-factory-or-not-to-factory/#findComment-1591737 Share on other sites More sharing options...
Solution Drongo_III Posted November 4, 2021 Author Solution Share Posted November 4, 2021 (edited) 13 hours ago, requinix said: Need more details. Or try from another angle. What kind of code do you want to write in order to get the data you need? I actually dusted off my copy of Patterns of Enterprise Application Architecture and there was a chapter on DTOs. In that chapter it recommends using a 'DTO Assembler' which is an type of Mapper - i.e. an intermediary to keep the DTO ignorant of the Models that supply it's data. And this seems to solve the very problem I'm trying to solve, I just didn't have a name for it and incorrectly thought 'factory must be what i need'. Edited November 4, 2021 by Drongo_III Quote Link to comment https://forums.phpfreaks.com/topic/314162-to-factory-or-not-to-factory/#findComment-1591745 Share on other sites More sharing options...
gizmola Posted November 4, 2021 Share Posted November 4, 2021 For anyone who might stumble upon this question in the future: Data Transfer Object (DTO) For the purposes of this discussion, there are multiple Laravel Eloquent Models Article on DTO in a Symfony4 REST api Patterns of Enterprise Application Architecture by Martin Fowler Quote Link to comment https://forums.phpfreaks.com/topic/314162-to-factory-or-not-to-factory/#findComment-1591762 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.