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

2

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

If what I understand is correct, the frameworks make the self-connection just to make it easier, so when we use the Model the settings of the database are loaded automatically by the framework when the Model is "built" by connecting to the database, or is it just to automate.

My question is, can I use a webservice in of the 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 it differently and what would this way be?

    
asked by anonymous 02.05.2015 / 04:36

1 answer

1

I think this would be a very risky architectural decision.

Consider the following situation. Your model transits through all layers of your application and you can quickly call these web services methods anywhere. Is that really what you want? Do web services get called from anywhere? Imagine that your object is in the Data Access Object (DAO) layer, or sometimes called the Repository. Would you like web services to be called there?

Another situation, imagine you create proxies of your models to be used in client applications. As they are proxies, what would happen if these methods of web services are called?

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

    
02.05.2015 / 04:46