As the title suggests, I am having small problems when implementing a 3-structure model (Domain object, dataperper and service).
In the past, when someone registered on my site, I just did
$user->register($firstName, $lastName, $emailAddress, $username...);
and this method will be executed with a step like this
1. Check if the form sent was valid. 2. Check if all the required fields were filled. 3. Check the if the lengths of strings were valid and the range of integers etc. 4. Check if the input is in the correct format (regex). 5. Check if the username is already taken and if the email address already exists in the database 6. etc. etc.
Everything works fine, but I'm trying to get away from this because I want my code to be more reusable and verifiable.
Now, with this 3-line model, the Domain Object and Data Map must communicate through the Service to isolate them from each other, so here is my idea of โโa user service
class UserService { public function register($firstName, $lastName, $email...) { $userDO= $this->domainObjectFactory->build('User'); $mapper = $this->dataMapperFactory->build('User');
And then, to actually run, that I would do it from my controller, so
$userService = $this->serviceFactory->get('user'); $result = $userService->register($_POST['firstName']....);
The logic (if and else) should go in the register() method in my UserService class correctly? Because if they fall into the domain object, when I am in the stage of need for the database to perform some checks, for example, if the username already exists, how can I access the database? I really don't know, since the domain object should not know anything about the data source.
There should be access to the database for small queries, for example, checking for a username or email address and many other small queries that need to be completed.
I have a lot of domain objects / objects that have to do a lot of small queries, and in the past my model had access to the database from any method and could execute those queries, but this doesn't seem to be allowed with this 3, and I'm dying to find out how to do it right, because there must be a way.
I flew until I found out that the Model is a layer, divided into 3 structures.
Any help or push in the right direction would be very helpful, especially good examples of real life. There seems to be no such problems on the Internet for my particular problem.
Thanks.