Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should be my Service method signature?

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?

like image 584
JohnT Avatar asked May 15 '11 20:05

JohnT


People also ask

How do you write a signature for a method?

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)

What are the three parts of a method signature?

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).

What are the two parts of a method signature?

Two of these components comprise the method signature: the method's name and the parameter list.

What is a method signature used for?

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.


2 Answers

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.

like image 100
Halcyon Avatar answered Nov 05 '22 18:11

Halcyon


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.

like image 2
τεκ Avatar answered Nov 05 '22 20:11

τεκ