MVC - How to Transfer Data to a Service

I am a bit confused about how to follow insert and update instructions using MVC. Is it possible to create an instance of an object in your controller and transfer it to the service in order to save it or transfer data to the service and process everything else there?

Embed

In my controller , something like:

$userservice->insert("myname","mypassword"); 

In my UserService :

 function insert($username,$password){ $user = ORM::for_table('user')->create(); $user->username= $username; $user->password= $password; $user->save(); } 

Update

In my controller , something like:

 $userservice->update("myname","mypassword",1); 

In my UserService :

 function insert($username,$password,$id){ $user = ORM::for_table('user')->find($id); $user->username= $username; $user->password= $password; $user->save(); } 

Is this a good practice? Because I see many such answers, where, for example, the user is created in the controller and transferred to the repository to save it: Is the correct repository template template in PHP? But I do not like the idea of ​​creating a user in the controller ...

+7
php model-view-controller
source share
3 answers

Controllers belong to the application layer and control only activity. In your example, the action is Create- and Update for an existing or new user. These operations relate to the domain layer that contains the services. In this way, services encapsulate the domain as a gatekeeper and provide operations for resolving the domain, such as a facade.

Is it possible to create an instance of an object in your controller and transfer it to the service in order to save it or transfer data to the service and process everything else there?

The service must provide a method for passing a ValueObject . ValueObjects is better to encapsulate a lot of data (property values ​​for the user). Inside the ValueObject service, Filter and Validator must be delegated. If validation does not complete, ValueObject will be delegated to DataMapper . The DataMapper maps the ValueObject properties to the data model for the UserRepository ( service level template .

So where should the value object be created?

I would prefer the service to provide a method for this: getEntityPrototype() using the prototype template . Be careful with naming. ValueObject is an object that does not have an identifier. An object is an object with an identifier (here is the user id). For an existing user, you will have a method such as getUserById($id) , which should return UserEntity. If the User does not exist for this identifier, it must return NullObject . To create a new user, getEntityPrototype() returns UserEntity, which does not yet have an identifier, so you will call it ValueObject or better Prototype Entity. After setting properties (for example, using a FormObject) and saving this object, it is a real object. In Factory, you can install EntityPrototype for this service.

+7
source share

In this case, you should think that classes have only one responsibility .

Controller decides on the flow of action. If there is a need to register a user, then he registers him, but he should not determine how to do this , but ask the service to complete this task and get the result.

On the other hand, you should have some UserManager that updates, creates and selects users - is this the only responsibility? Kinda, yes - it is their management in a broad sense.

There is a slight problem with the names of your methods. You should have registerUser not insert , as it’s easier to say what it actually does.

+2
source share

You should pass data to the model. MVC is all about task sharing. The controller - handles the application flow, the model - contains the entire database of business logic, etc. And the view - here you decide how to show. Most of the user interface is stored here.

Thus, the controller must send model data, and the model decides what to do with the data. The advantage of coding in this way is that in the future, if you want to change something in the code that you know where to look, or if you ask the designer to redesign your site, you need to give him a VIEW part of the code. If a designer does something that caused an error, a fix that will not take so long. If you follow MVC correctly, adding, updating or supporting features will not be a problem

0
source share

All Articles