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 )
{
}
}
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 )
{
}
}
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 which type of business rule, because depending on the rule it can 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 Past Item or even on another object. For this reason this business rule can be implemented in another class that will do this "service" of generating 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 DeleteItemError method, if these rules were implemented in the Item object, in the future if you needed to change% with% of that rule from $ 1,000 to $ 2,000, you would need to change the Item. Similarly if it were necessary to change the rule than it would be a o valor da fatura
, we would also have to change the Item. With this we are changing the Item due to changes related to invoice. This would be very bad: The Item would end up not having a single responsibility and beyond its rules, would be loading rules related to Invoice.
However, if we think about the terms highlighted (exemplifying what our business rules would be), it is possible to identify that they are related to the Item, but are not specific rules of the Item: the value is of the invoice, as well as the rule if it has been paid and can be deleted. So we can remove this "overload" from the Item, this being only with your specific responsibilities and separate in the Service class the specific rules of generating and deleting an item from an invoice.