ORM architecture

ORM architecture

DWKit uses a special ORM engine that allows getting data represented as dynamic objects or typed objects inherited from DbObject, where T refers to the type of object. To request and update information in the database, you need metainformation about what tables you should request, what the types of columns are, and how to build joins. You can get this information from three sources:

  • Using the Data Model, which you can change and synchronize in the Admin application. In this case, you should request a data object by the name of the DataModel object.
  • Using the binding of data model to the form controls, which can be edited in the Admin application. In this case, you should request a data object by the name of the form.
  • Using information from the attributes that lay out the properties of the class inherited from the DbObject

In the first two cases, getting a model is enabled by the static class MetadataToModelConverter, in the second case by the DbObject<T>.Model property. Below is an example of getting the EntityModel from the Data Model:

EntityModel userModel = MetadataToModelConverter.GetEntityModelByModelAsync("SecurityUser").Result;

In this case, the EntityModel enables access to the SecurityUser table in the database. An example of getting the EntityModel from the form:

EntityModel userModel = MetadataToModelConverter.GetEntityModelByFormlAsync("SecurityUserForm").Result;

In this case, the EntityModel enables access to the table specified in the binding of data model to the form controls, for example, the SecurityUser table. Here's an example of getting the EntityModel from the DbObject<T>:

  public class SecurityUser : DbObject<SecurityUser>
  {
        [DbObjectModel(IsKey = true)]
        public Guid Id
        {
            get => _entity.Id;
            set => _entity.Id = value;
        }

        [DbObjectModel]
        public string Name
        {
            get => _entity.Name;
            set => _entity.Name = value;
        }

        [DbObjectModel]
        public string Email
        {
            get => _entity.Email;
            set => _entity.Email = value;
        }
  }
  EntityModel securityUserModel = SecurityUser.Model;

In this case, the EntityModel enables access to the SecurityUser table in the database. The name of the table and the name of the columns correspond to the name of the class and the names of its properties.

Thus, using the EntityModel, you can request or update data. Usually, such operations are performed via the static object DynamicRepository or via the EntityModel. For example:

var userModel = MetadataToModelConverter.GetEntityModelByModelAsync("SecurityUser").Result;
var user = userModel.GetAsync(Filter.And.Equal(userId, "Id")).Result.FirstOrDefault();

or

var user = SecurityUser.SelectByKey(userId).Result;

The first approach is used to organize general business logic. For example, DWKit uses it to organize datasources for forms. The second approach is used to organize complicated and unique algorithms of business logic.

When requesting data, you can specify the request parameters:

  • Filter object - filter (the WHERE sentence) in the request, for example, var filter = Filter.And.Equal(userId, "Id") - filter by column (property) Id.
  • Order object - sorting (the ORDER BY sentence) in the request
  • Paging object - specificaion of a data page in the request.

Yoy can also change or update data via the DynamicRepository object or via the EntityModel. Upon data selection, you get objects of two types:

  • In case you used the EntityModel to request data, you will get a list or a unique object, such as the DynamicEntity. There are two ways of getting values of properties from them. For example, via the Email property.
var email = (string)user["Email"];

or via the conversion to dynamic:

var dynamicUser = user as dunamic;
string email = user.Email;
  • In case you used an inheritor of the DbObject<T> to request data, the data will be strongly typed. For example, you can get a User-like object.