N
N
neronru2016-06-30 18:46:34
go
neronru, 2016-06-30 18:46:34

How to properly organize model-model communication in MVC?

In general, suppose that the value of a property of one model depends on the value of a property of another. As an example, consider the following GO code, but in principle there is no difference in the programming language.

type A struct {
 Id uint64
 Param1 uint
}
type B struct {
Id uint64
IdA uint64
Param2 uint
}
func (b *B) SetParam2(a A) {
b.Param2 =  3*a.Param1
}

In general, the question is whether such code is correct from the point of view of the MVC pattern?
And it turns out that the initialization of structure A for further use in B must be done in the controller? Or can it be done in model B?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikita, 2016-06-30
@neronru

The controller provides the transfer of external data to the model. If the initialization of structure A requires the transfer of external data to it, then this can be done in the controller, but I think it is not at all critical where the initialization will take place.
I would do not SetParam2, but

type B struct {
  A
  Id uint64
  IdA uint64
  //Param2 uint 
}

func (b B) Param2() uint {
  return A.Param1 * 3;
}
but this, of course, depends on the task.
For complete clarity, the easiest way (in the sense without thinking about how it should be) is to create a 3rd structure C that works with models A and B. Initialization of C in the controller, initialization of A and B in model C.

A
Anatoly, 2016-06-30
@taliban

In such cases, a third entity is introduced and called differently (service, library, etc.) and it already interacts between models. In general, ideally, it's cool when the models are self-sufficient, and do not depend on anyone. It is essentially a dataset with minimal logic.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question