Can we create the business rule in our model?

Asked

Viewed 601 times

2

What best practice?

Create the rule in the model itself or create another class

Ex:

public class Item
{
    public void Faturar()
    {
        //fatura item
    }
}

ou

public class ItemBLL
{
     public void Faturar(Item )
     {

     }
}

1 answer

2


Yes, we can create business rules in the Model. Model represents the logical behavior of the data in the application. It represents the business logic of the application.

I believe that the answer about creating the rule in one class or another depends on what kind of business rule, because depending on the rule it may or may not be created within the object.

Example 1: The business rule below is a specific rule of the Item object. For this reason it is implemented in the object itself.

public class Item
{     
    public string NomeDoItem { get; set; }

    public void NomeDoItemDeveIniciarComLetra()
    {
        //Código que valida o nome do Item iniciado com letra
    }
}

Example 2: Already to generate an Invoice, the business rule may be different depending on the Item passed or even another object. For this reason this business rule may be implemented in another class that will make this "service" to generate the invoice according to the Item.

public class ServicoFatura
{
    public void GerarFatura(Item item)
    {
        //Código que recebe um Item e gera a fatura 
    }

    public void ExcluirItemDaFatura(Item item)
    {
        //Código que recebe um Item
        //Verifica se o item possui Faturas geradas com valor maior que $1.000
        //Caso positivo retorna uma mensagem e não excluí o Item 
        //Além disso verifica se o Item possui suas faturas pagas
        if(item.PossuiFaturasGeradas() && item.FaturasGeradasPagas())
        {
            //...
        }
        //...
    }
}

Analyze the method Excluiritemdafatura, if these rules were implemented in the Item object, in the future change o valor da fatura of this rule from $1,000 to $2,000, a change in the Item would be required. Similarly if it were necessary to change the rule of what would be a fatura gerada e paga, we would also have to change the Item. With this we keep changing the Item due to changes related to the invoice. This would be very bad: Item would end up not having a single responsibility and beyond its rules, would be carrying Invoice related rules.

However, if we think about the terms highlighted (exemplifying what would be our business rules) it is possible to identify that they are related to the Item, but they are not specific rules of the Item: the value is the invoice, as well as the rule if it was paid and can be excluded. Then we can remove this "overload" from the Item, leaving it only with its specific responsibilities and separate in the Servicofatura class the specific rules generating and deleting an item from an invoice.

  • I understood, so creating this class "Service" a class of layer BLL

  • @Rod That’s right

Browser other questions tagged

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