开发者

MVC - is it okay for business models to know each other?

Simple question: in MVC, is it okay for one model to interact with another? For example, with an Auth model...can it i开发者_如何学编程nteract with the User model - or is there a better way to go about it?

Should the middle man be the controller?


Different Model classes interacting is just fine. Most complex objects aren't completely standalone. As with any good OO code, the classes should use public methods and not expose their implementation to each other, but beyond that, it's all good.


They can certainly know about each other, and utilize each other's functions. Often, best practices will keep this one-way, but not always. Like dj_segfault said, this interaction should be through public methods. Be sure to read up on Dependency injection; changing the internal workings of one class should not break the other, as long as all public methods still behave the same.


It really depends on the specifics of what you are trying to accomplish. On the surface, one model can interact with another model if it needs to. Just make sure that the reason why the model has to interact with another model is well thought out, and that the model to model interaction would'nt be better served by model to communication interaction instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜