Which application layer is aimed at processing the most extensive and complex business rules?

Asked

Viewed 563 times

9

I’ve been reading some questions and answers about the MVC standard and I saw that they say that the treatment of business rules in the layer is correct Controller, such as: Check if a record already exists in the database before persisting it.

However, I still have some doubts, and one of them refers to more complex treatments/business rules, which require more classes to maintain a good structure, such as tax assessment and other processing that make up more complex classes.

This kind of situation I believe deserve another layer of application and I would like to Saer which layer is this?

I believe it is not a class of model layer because it is not data validation, but processes.

Is this line of reasoning correct? If so, what would be the appropriate layer to condition such deeper process classes?

3 answers

7


It would not indicate creating business methods in the MVC controller. It is very common for you to need to expose these methods through a web service, or to use the same business rules in a desktop application, for example. The ideal would be to create a specific layer to deal with business rules, this way your layer can be reused in other projects of your solution. Imagine you develop within the controller some rule that can be used in other controllers. It wouldn’t be very stylish for one controller to access the other.

  • Hello @Gus, thank you for your reply. And what could be the names of that layer with business rules?

  • 2

    @James where I work we usually put as follows. Company name_product layer_you can call Regranegocio or Negocio. The idea is to put a name that represents what you are proposing. A suggestive name.

  • 1

    Business (Business) really seems to be the most appropriate. I’ll just wait a little longer to see if more answers emerge. Thank you for your response!

  • 1

    I also have a preference for this kind of nomenclature, however is that your solution, if not already, may have a mobile application, which logic would receive a layer name App.

  • @Gus, it’s out of scope, but I’d like to know if you’re familiar with software engineering, and if you could point me to a book with good teaching and content for a software study for someone who doesn’t know anything about it yet? Grateful!

2

I believe it is not a class of the model layer because it is not a validation of data, but of processes. This line of reasoning is correct?

It depends. In question you said the following:

I’ve been reading some questions and answers about the MVC standard and I saw that they say that the treatment of business rules in the Controller layer is correct, such as: Check if a record already exists in the database before persisting it.

I admit I said something like that, but in the answers I stated that the database is managed by the Entity Framework, which would be the Framework that implements a complete repository.

In this case, what the Controller does is invoke the Entity Framework methods only, and does not perform the full treatment of a validation cycle.

The function of a Controller is basically to harmonize the data flows between the various components of an application. In the specific case of a validation, this place is the Model.

How can this be done?

On validation of processes

When logic is recurring (i.e., when it is used in more than one Controller) the correct approach is using a design standard called Helper.

What is a Helper?

Helper is a class, usually static, or an extension method (which is always static, therefore) that contains a recurring logic to several Controllers. He can be summoned at any time by any Controller, can use a data context (Entity Framework) or a repository (not-Entity Framework).

In it you can put what you call "process validation", with a complex sequence of steps, without necessarily putting this logic in a Controller.

Taking advantage

@Gus said the following in his reply:

It is very common for you to need to expose these methods through a web service, or to use the same business rules in a desktop application, for example.

This is true, but the idea is that in MVC6 the programmer writes a Controller for all presentation layers such as Desktop or Web Service.

There is, in fact, a limitation of a Controller MVC be almost strictly for a web project, but this should be simplified in vNext.

(By the date of this reply, vNext had not yet been officially released)

-1

In my company we develop with . NET Webforms and MVC, we split into 3 layers (3 projects within a solution): the layout of the site (design and treatment of some parameters), the BLL (Business, the rule of business with the assembly and validation of the CRUD and complex processes of an entity) and the DAL (Data access layer, which accesses the database) this last layer is called only by BLL. Advantages: organization and reuse of code, ex: I can make a Webservice access my BLL.

Browser other questions tagged

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