📜 ⬆️ ⬇️

Layered abstraction

In the previous article, we looked at some approaches to code generation, now I want to look at multi-level abstraction and do some analysis.

This article contains only a theory. The following article will be practical (I will try to alternate).

Layered abstraction


Multi-level abstraction - the division of an application component into several levels of abstraction so that at each level the abstraction is consistent. This is somewhat abstruse may sound. The point is to divide the component into several levels, so that we can work relatively autonomously with this level in its abstraction and not keep in mind information about other levels.

Why do they divide into levels of abstraction?


1. Fight against complexity. At each level, methods of this particular level are applied.
2. Reduced connectivity.
3. Ensuring the interchangeability of classes at all levels except the top.
')

Multi-level data abstraction


Let's go in descending levels of abstraction:
* Class is the essence of the real world
* Data provider
* Real data libraries

Example:
* User
* IUserProvider , SqlUserProvider , XmlUserProvider , ...
* SqlClient , XmlDocument , ...

In this case, we get a low connectivity: User knows about the IUserProvider interface, SqlUserProvider and XmlUserProvider run IUserProvider and use the SqlClient and XmlDocument libraries. Moreover, objects of a certain level of abstraction work only with objects of the next (lower) level of abstraction, but not vice versa, which does not allow circular links.

When do problems arise?


1. Usually in one project several multi-level abstract models. Problems arise if several abstract models must be subjected to uniform changes. Thus it is necessary to make changes to all intermediate levels of abstraction, including the top one.
2. When prototyping, the overhead of designing a multi-level abstract model may be too high and it is possible to write the time code without abstraction levels, which will have to be thrown out after the prototype is approved.
3. Abstraction from data sources can generate (and sometimes generates) non-optimal work with data sources.

What to do?


Code generation in many cases (not always) can replace multi-level abstraction. This will generate the final classes (from the top level of abstraction), containing the methods of working with the selected data source.
1. Having metadata based on, we can make changes to the code generation algorithms and modify the entire model at once.
2. If there is metadata, a prototype model can be obtained in the shortest possible time.
3. Due to the presence of generators for each data source, the model will be generated with an acceptable optimality of work with the selected data source, taking into account its specificity.

How code generation can help in complex models


Complicated applications always ask a lot of questions. According to my observations, most of them can be answered during the development process (for example, the site needs to be cached or not; which operating system will be on the server; use output buffering or not ...). If we answer these questions in advance - we can avoid the extra complexity of the program, unnecessary actions, unnecessary checks, etc. Moreover, the code generator itself can collect in the destination environment some data in advance, which it can use to optimize performance.

But this does not mean that less result code = simpler system. The code generator itself must be of sufficient quality to generate quality code.

Code Generation + Multi-Level Abstract Model


These approaches are not contradictory and can be used together. For example, in ASP.NET there is a system for storing personal user data in profiles (Profile). An abstract model with several levels of abstraction is built there, and ProfileBase lies on top . If we set the property list in the configuration, then a descendant of the ProfileBase class, ProfileCommon , will be generated, which will contain the properties we specified in the configuration. In fact, we specified metadata in the configuration.

In the next article, we will develop a certain uncomplicated code generator.

Source: https://habr.com/ru/post/23927/


All Articles