MVC - Is it okay for a Model to be composed of a f

2019-05-07 10:43发布

I have two Controllers: one for viewing a single player, and one for viewing a team. I currently have it where the team is composed of a bunch of Player Models.

I'm new to MVC and out of everything I've read, I haven't seen much on Models being composed of other Models. Is there an alternative way to approach this situation, or does this seem like a pretty standard implementation?

5条回答
仙女界的扛把子
2楼-- · 2019-05-07 11:24

Of course! That's object oriented programming!

查看更多
成全新的幸福
3楼-- · 2019-05-07 11:30

A Team is naturally a Collection of Players with some additional data (name, manager, etc). I think this should be okay.

查看更多
Ridiculous、
4楼-- · 2019-05-07 11:32

Yes, a thousand times yes! This is how the Dependency Injection Frameworks (such as Robotlegs and Swiz) work. They have an Injector which acts as a combination Model and Factory to provide just the bits needed by a View or Command (for the most part, they also don't really have the idea of a large, monolithic Controller, either).

查看更多
家丑人穷心不美
5楼-- · 2019-05-07 11:34

Model is not a "thing" or a "class". It is a layer.

And model layer is made up from multiple elements. Two of most important types are Domain Objects [1] [2] and Data Access Object ( usually implemented as DataMappers [1] [2] pattern). The third type of structures would be services, but i'm gonna try to keep it simple.

The domain objects is where the business logic is withing the model layer. And not only it is possible to have domain object containing other domain objects, it is usually the best way to to go. Here is a small example:

$group = $this->modelFactory->buildObject('group');
$mapper = $this->modelFactory->buildMapper('group');

$group->setName('wheel');
$mapper->fetch($group);

$user = $this->modelFactory->buildObject('user');
$user->setName('foobar');
$user->setHomeDir('/home/user');
$user->setShell('/bin/csh');

$group->addUser( $user );
$mapper->store($group);

You also might benefit from reading this topic.

查看更多
戒情不戒烟
6楼-- · 2019-05-07 11:38

Given your question, you're referring to the model as a single entity (e.g. the user model). If you take the only the model and forget about the application, the model is literally what the word states, a conceptual model.

A domain model in problem solving and software engineering can be thought of as a conceptual model of a domain of interest (often referred to as a problem domain) which describes the various entities, their attributes, roles and relationships, plus the constraints that govern the integrity of the model elements comprising that problem domain.

http://en.wikipedia.org/wiki/Domain_model

A mental model captures ideas in a problem domain, while a conceptual model represents 'concepts' (entities) and relationships between them. A Conceptual model in the field of computer science is also known as a domain model.

http://en.wikipedia.org/wiki/Conceptual_model_(computer_science)

Answering your question: Your domain model will be composed of entities with associations between then, in other words, one single entity can have a composition containing a collection of other entities (associations or aggregations).

A Domain Model creates a web of interconnected objects, where each object represents some meaningful individual, whether as large as a corporation or as small as a single line on an order form.

http://martinfowler.com/eaaCatalog/domainModel.html

In order to it turn into a layer of something else, you need to jump from the OOA (Object-oriented Analyzes) to the OOD (object-oriented Design).

Object-oriented analysis (OOA) applies object-modeling techniques to analyze the functional requirements for a system. Object-oriented design (OOD) elaborates the analysis models to produce implementation specifications. OOA focuses on what the system does, OOD on how the system does it.

http://en.wikipedia.org/wiki/Object-oriented_analysis_and_design

So, keep in mind the two meanings of the "model" keyword. It can be the mental model of the problem domain or a layer of your application describing how the mental model will be technically implemented.

查看更多
登录 后发表回答