0

In MVC, MVVM, or MVP I typically have models that either put a heavy emphasis on using other models in conjunction to get the full set of data needed or that basically require joins to other tables (models). When I run into this I usually just extend the model's functionality like normal but will include a join to another table in new method I'm creating. What's tricky for me to sometimes decide is which model I should put this functionality/join in since it makes it dependent on something not specific to that model. Is there a better way of dealing with relationships when using patterns that utilize data models?

Here is some pseudo code to help illustrate what I mean with some active record mixed in:

public static void getAll() {
    this.db("sometable").join("anothertable", "column", "=", "anothercolumn").select();
}

Now if the model was for sometable I'm creating a dependency of sorts by joining to another table in the method above. Now the model for sometable also deals with anothertable which would have its own separate model.

Aaron
  • 10,386
  • 13
  • 37
  • 53
  • 1
    Do you have a bit of pseudo code to illustrate the question? It's a bit difficult to understand the scenario completely... – Marc Nov 12 '14 at 07:54
  • Sure, I've just added some. – Aaron Nov 12 '14 at 12:54
  • The select is redundant; it's only necessary if mapping the type of the join to another type. –  Nov 12 '14 at 14:12
  • It's pseudo code. It's not meant to work and I wrote it based off a mix of languages and frameworks I've used in the past. I'm not asking about how to deal with the code but more about how to deal with the general situation. – Aaron Nov 12 '14 at 14:15

1 Answers1

1

'Model' is an abstract term. It doesn't need to be one single model.

I look at MVP as a presenter, a view and whatever else I need to get the job done. This might be one class, an enumeration or a number of repositories. As long as I keep my presenter's dependencies low, I'm happy.

Could you consider making your controller or presenter dependent on something 'above' your models that can get related things as, and when, you need them?

David Osborne
  • 6,436
  • 1
  • 21
  • 35
  • Your suggestion for making my model or presenter dependent on something above my models is a good one. I'm thinking I could use service objects which would provide an interface of sorts to my models and the required execution flow. – Aaron Nov 12 '14 at 23:13
  • Worth looking at: http://stackoverflow.com/questions/9031916/in-mvc-mvp-and-mvvm-the-model-could-it-be-a-collection-of-entities?rq=1 – David Osborne Nov 14 '14 at 15:54