3

What should be the best practice in php to name an entry point method in a service when following DDD design principles.

Same as class:

class GetSinglePerson {
     ...

     public function getSinglePerson($personId)
     {
     }
}

Command pattern:

class GetSinglePerson {
     ...

     public function execute($personId)
     {
     }
}

Adapter pattern:

class GetSinglePerson {
     ...

     public function handle($personId)
     {
     }
}
peterpeterson
  • 1,315
  • 2
  • 14
  • 38
  • Do what works best in your particular setting and in your specific code base. There is no "best" way to name methods. Just be consistent. – WillardSolutions Feb 14 '18 at 17:41
  • In the first example it will act as a constructor (if there is no __construct) and execute when instantiated. – AbraCadaver Feb 14 '18 at 18:24
  • Maybe this: `class PersonService { public function getPersonById($personId) { ... } }` – odan Feb 15 '18 at 12:28

3 Answers3

4

Some remarks that won't fit in a comment ;)

  • DDD is not an architecture

  • Naming a class with a verb (GetSinglePerson) instead of a noun is uncommon in OO, DDD included.

  • Execute is command terminology - GetX is usually not a command but a query - see CQRS.

  • One-method services are uncommon, not to mention services named after the only operation they expose. Usually you would group operations together into something with a higher level name like PersonService.

guillaume31
  • 13,738
  • 1
  • 32
  • 51
  • In DDD I believe services must do one thing only. Ok I changed architecture to design principle. the GetSinglePerson is taking the repository into its constructor and it orchestrate it. – peterpeterson Feb 15 '18 at 11:02
  • "In DDD I believe services must do one thing only" - definitely not. They must be cohesive but can have multiple operations. – guillaume31 Feb 15 '18 at 11:58
0

It's really up to you. I would read over the PSR-1 Basic coding standard it has guidelines I wish we all followed. :)

As long as you are consistent, that's what counts.

This paragraph in particular sticks out to me and applies in this question/situation.

4.2. Properties

This guide intentionally avoids any recommendation regarding the use of $StudlyCaps, $camelCase, or $under_score property names.

Whatever naming convention is used SHOULD be applied consistently within a reasonable scope. That scope may be vendor-level, package-level, class-level, or method-level.

4.3. Methods

Method names MUST be declared in camelCase().

Kevin Pimentel
  • 2,056
  • 3
  • 22
  • 50
0

I would use index. If you're using something like laravel or codeigniter it should do the routing like /GetSinglePerson/{personId}

class GetSinglePerson {
     ...

     public function index($personId)
     {
     }
}
Josh Woodcock
  • 2,683
  • 1
  • 22
  • 29