Can a Model use a Webservice for "business rules"?

Asked

Viewed 153 times

2

When using some ready-made frameworks (such as Laravel and Codeigniter), I noticed that Models are usually extended from other classes that usually connect directly to the database, so I understood this is not mandatory, as the Model should contain the "business logic" ("business rules") and not necessarily connections to databases.

If what I understand is correct, frameworks only make the connection to the auto-connection for ease, so when we use the Model the database settings are automatically loaded by the framework at the time the Model is "built" by connecting to the database, ie is just to automate.

My question is this, I can use a webservice within Model, as being "equivalent" to the database?

For example (Note that this example is completely fictitious):

class MyModel
{
    public function putItem($preco, $descricao)
    {
        $rest = Rest('ws.example.com', 'PUT /', array(
                                            'price' => $price,
                                            'description' => $descricao
                                        ));

        //Se o produto foi adicionado o HTTP status é 200 então putItem retorna TRUE
        return $response->status === 200;
    }

    public function deleteItem($id)
    {
        Rest('ws.example.com', 'DELETE /items/{id}', array(
                                            'id' => $id
                                        ));

        $response = json_decode($rest->getRespose());

        //Se o produto foi deletado o HTTP status é 200 então putItem retorna TRUE
        return $response->status === 200;
    }

    public function getItem($id)
    {
        $rest = Rest('ws.example.com', 'GET /items/{id}', array(
                                            'id' => $id
                                        ));

        $response = json_decode($rest->getRespose());
        if ($response->status === 200) {
            //Se o produto existe o HTTP status é 200 então retorna informações dele
            return $response->data;
        }

        //Se não retorna FALSE
        return NULL;
    }
}

Or should I do otherwise and what would be this way?

1 answer

1

I believe this would be a very risky architectural decision.

Think of the following situation. Your model moves through all the layers of your application and soon you can call these methods web services anywhere. Is that really what you want? To make web services can be called from anywhere? Imagine that your object is in the DAO (Data Access Object) layer, or sometimes called Repository. You would like web services to be called there?

Another situation, imagine you create proxies of your models to be used in customer applications. Since they are proxies, what would happen if these web service methods were called?

I think it would be more interesting if you delegated the call to web services at specific times and layers of your application. Its architecture would be more robust this way.

  • Thanks for the reply, sorry for the delay to answer, this question of MVC is still strange to me, yes I understood the reason of "not use" within the Model, if I understand correctly the Model is to "represent the bank" in the application, so the use of REST should be within the action directly, got it right? Please correct me if I’m wrong.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.