tags:

views:

19

answers:

4

I have to write a simple method extractArticle() that returns Article object which is extracted from Message object. I have MessageService and ArticleService classes intended to handle tasks like this. What service class is more correctly to use to incapsulate extractArticle() funcionality?

$article = MessageService::extractArticle(Message $message);

or

$article = ArticleService::extractArticleFromMessage(Message $message);

A: 

Does your ArticleService currently know about Messages? If not, I'd vote for putting it in MessageService.

In general, think about what dependencies exist between messages and articles, and how you want those dependencies to be controlled.

Owen S.
A: 

I don't think there's a lot of difference in this case as to whether defining in MessageService or ArticleService. But the work of extracting itself should be handled by a method defined in the Message class. Whichever service class you decide to use would just be a light wrapper around Message::extractArticle(). In this method of Message class is where encapsulation takes place.

Khnle
A: 

Extracting the article from the message is a function that your perform on the message, so I would say it logically belongs into the MessageService class.

Timo Geusch
A: 

MessageService and ArticleService appear to be nouns. Services normally do something, so are named for verbs.

I would expect that the article is in some kind of format in the message, so would have an ArticleParser which parses the message and creates an article.

Whether the message service or article service has the parsers for messages depends on the responsibilities of those services, which is hard to guess from the names. If the message service was a message forwarding service, and the article service had an endpoint where it received messages, I would expect the parsing to be post the endpoint. These sorts of patterns are well explained in patterns of enterprise integration - where to convert a message into a domain object, forwarding and so on.

But there isn't a 'best practice' answer. The best practice is to consider what the responsibilities of the services are, and often it's better to put transformations between services rather than within them.

Pete Kirkham