2

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?

  • If you're using the word "Factory" to refer to an implementation of the Factory OOP design pattern, you might want to rename your class. Object oriented factory pattern isn't what you're doing. See Robert Harvey's answer. – RibaldEddie Dec 25 '13 at 18:50
  • @RibaldEddie: Yes, that was my intention. Does it not qualify because it only produces objects of one type? I originally had 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 made Post objects should be a PostFactory, 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 create Post objects"? – Patrick Collins Dec 26 '13 at 5:07
  • More or less, that's right. But I do wonder what's up with your system. Why is creating an object requiring creating a database record? Or when you say "create a post object" do you mean something like "save a post object or persist a post?" – RibaldEddie Dec 26 '13 at 5:16
  • @RibaldEddie: it's the opposite. 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. – Patrick Collins Dec 26 '13 at 5:25
  • Are there any special steps your code needs to do to the Post object between the API call and returning the populated Post? If not, then just put the code in the repository. If so then rename your Factory to be a Builder. Look up the builder pattern. – RibaldEddie Dec 26 '13 at 5:28
12

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.

1

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.

  • 1
    See the update I made to my answer. – Robert Harvey Dec 25 '13 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.