📜 ⬆️ ⬇️

Linking: Page Parameters + Data Source

Based on the request parameters, download data from the source - a routine that the web developer encounters. The article describes an approach to reduce the time required to associate page parameters with a data source. The implementation of this approach for the .NET platform is described.

Consider an example:
Online store:

As a parameter for the pages, a unique identifier of the product or supplier is transmitted, respectively. Implemented the following functionality:
  1. extracting a unique identifier from the query string
  2. receiving by identifier information on the product or supplier, respectively

Similar functionality is often necessary to implement when developing pages of the site. I would like to highlight it in a separate component that could work with different types of entities (product, supplier). It should be easy to use and not significantly affect performance.
Critical properties of a component that determine its use:

What data does a component need to perform the necessary actions? Just a set of rules: the name of the parameter is the type of the entity! Indeed, knowing the name of the parameter, you can get the identity of the entity, knowing the type of the entity and the identity of the entity, you can extract the entity from the data source. For the example above, this would be:

request: <page>? poductId = <product identifier>
rule: productId - Product
by the parameter “productId” the entity identifier of the type “Product” is given

request: <page>? supplierId = <supplier identifier>
rule: supplierId - Supplier
the supplierId identifier of the suppler type entity is passed through the supplierId parameter
')

Linking to a page

The developer needs a mechanism to link the page and the component, that is, the following:

Binding can be arranged in different ways and one would like the component to use different mechanisms. Let us select in the component the part that provides interaction with the binding mechanism, and we will change it depending on the mechanism. Accordingly, all such parts correspond to a single abstraction (interface), possessing the following functionality:

This approach will simplify the addition of new binding mechanisms and their replacement


Work with collections

Often one has to deal not with separate entities, but with collections of entities when a set of identifiers is passed by parameter. Accordingly, by a set of identifiers you need to load a set of entities. Support of such functionality from the component will speed up the development process.


Work with data sources

An entity identifier is based on a data source and may have a complex structure. If you use a relational database as a source, then the identifier can be the main key of the table in which the entity is stored. The main key, in turn, can be built on several fields of the table. In order for a component to work on top of any source structure, you must provide the ability to work with complex identifiers.
The sources themselves may be different, and I would like to get a flexible mechanism to access them. You can select a part in the component that provides interaction with the data source and change it depending on the source. Accordingly, all such parts correspond to a single abstraction (interface), that is, they have the following functionality:

This approach will simplify the addition of new sources and their replacement.


Not only pages

A page is a good example of using a component. It is at the same time the source of the parameters (the name of the parameter is the identifier) ​​and also the target object to which the entities from the data source need to be passed. If we assume that the parameters are specified in some other way, then this component can be applied to an arbitrary object that needs to be transferred to the entities from the database. For example, you can store the configuration in the form: parameter name - identifier, and then use the component to transfer the necessary entities to the target object.


Implementation for .NET - Binding Framework

Binding Framework - library (.dll). It has:

Properties + Attributes

Depending on the entities that we want to load through the component, we define the page properties. The type of property may be as follows:
  1. entity type
  2. typed collection (IEnumerable <T>, T is an entity type). These properties are also marked with an attribute in which the parameter name is specified.

The same properties provide developer access to loaded entities.

A bunch of LinqToSqlClasses & WebPage

When working with collections, this link transfers the page property not a ready-made set of entities, but the IQuerable <T> collection, the T-type of the entity, which allows you to postpone the request until the moment of use, to apply additional selection criteria.

Links

Binding framework
An example of a bunch of LinqToSqlClasses & WebPage (with pictures :-)

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


All Articles