I'm using a Service Layer, and until now I used a ServiceObject (which implements ArrayAccess, Iterator, Countable) but I'm wondering if it's a good ideas.
Would you do:
ArticleService::createArticle($articleData, $userId);
or
ArticleService::createArticle(ServiceObject $data);
where $data
are:
array(
'title' => 'Lorem ipsum',
'body' => 'Dolor sid amet',
'userId' => 55,
);
The ServiceObject has the benefit to provide a common Signature for every method, however sometimes it doesn't look efficient, and it is not widely used, it loses its interests.
Any feedback?
The signature of a method consists of the name of the method and the description (i.e., type, number, and position) of its parameters. Example: toUpperCase() println(String s)
The only required elements of a method signature are the method's return type, the method name, parentheses for the parameter list (which can be empty), and the method body inside curly braces (which can also be empty).
Two of these components comprise the method signature: the method's name and the parameter list.
A function signature (or type signature, or method signature) defines input and output of functions or methods. A signature can include: parameters and their types. a return value and type.
Change it to:
ArticleService::createArticle($title, $body, $user_id);
This makes it very clear what you need to create an 'Article'.
'option' arrays like your $articleData
and $data
are impossible to intellisense sensibly and I would advise against it.
Trash your idea for a generic ServiceObject
it's actually a very bad idea. Your motives are noble but this is the wrong solution.
If you need more convincing, feel free to poke.
I think the best approach here would be to have something like
$article = new Article;
$article->title = "Lorem ipsum"
$article->body = "Dolor sit amet"
$article->creator = $userID
ArticleService::createArticle($article)
Though presumably "createArticle" would not be the best name for the function any more, seeing as the Article object was already created. You would probably have something like ArticleService::publishArticle($article)
though I don't know what you are making.
What you want is to separate the construction of your data (Article
) from the usage of it (ArticleService
).
What I'm really trying to say is don't make the argument of createArticle a generic array or "ServiceObject", both of which are useless abstractions of argument lists, make it a class that really represents the relevant entity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With