Answer the question
In order to leave comments, you need to log in
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
}
Answer the question
In order to leave comments, you need to log in
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. 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 questionAsk a Question
731 491 924 answers to any question