I'm working (slowly) on a small Flask project. One of my models is a class called Post
, which is a lightweight namedtuple
. Post
objects are created by a PostFactory
, because creating a Post
involves hitting a database. The PostFactory
holds the necessary database connection, so the Post
objects can be serialized.
What part of the model/view/controller division does a factory object naturally fit in? Is it part of the models, since it's tightly coupled with Post
objects? Or is it a controller, since it manipulates models?
2 Answers 2
It's probably a proper part of a repository, which goes in your model.
Any machinery that helps you look up domain objects is part of the model. The controller will most likely call a method in your repository, which in turn calls the factory to get the Post
object. Or, you can expose the factory method from your repository, and allow the controller to call it directly.
Always favor fatter models, and thinner controllers.
The name MVC is highly semantic. Model is your data, View is your UI/Interface, and Controller being the operations you perform on the data. In your example, your assumption is correct in that it would be considered part of the controller as it is manipulating the data.
-
1See the update I made to my answer.Robert Harvey– Robert Harvey2013年12月25日 18:41:57 +00:00Commented Dec 25, 2013 at 18:41
Explore related questions
See similar questions with these tags.
Post
objects get passed a connection to the database and then pull the necessary info, but it seemed messy because posts have nothing to do with database connections. I thought that the thing that madePost
objects should be aPostFactory
, do you think that could be confusing because it's too limited? Is there a more reasonable name for "the class whose instances are used to createPost
objects"?Post
objects contain data that's been scraped from another site. They get passed an ID to look up, then pull down information about it from an API.