Skip to content

iv. Transformer Class

Code Ninja edited this page Sep 30, 2024 · 3 revisions

What is a Transformer

The purpose of the transformer class is to transform the data fetched by the linked query class and mapp to the configured object graph of the entity.

To define a transformer class, you need to implement BaseTransformer<TQueryResult, TEntity>

  • where TEntity is Entity implementing IEntity. eg. Customer.
  • where TQueryResult is Query Result from associated Query. It is an implementation of IQueryResult interface.

Note: It is important that the transformer should map data only to the schema path(s) pointing section(s) of the object graph.

For the example query/transformer mapping

  .Map<CustomerQuery, CustomerTransform>(For.Paths("customer"))

The customer transformer maps data only to the customer xpath mapped object graph of customer class. ie. - customer/id, customer/customercode, customer/customername

In below transformer example, CustomerTransformer is implemented to transform entity Customer with CustomerResult query result obtained from CustomerQuery execution.

internal class CustomerTransform : BaseTransformer<CustomerResult, Customer>
   {
       public override Customer Transform(CustomerResult queryResult, Customer entity)
       {
           var customer = entity ?? new Customer();
           customer.CustomerId = queryResult.Id;
           customer.CustomerName = queryResult.CustomerName;
           customer.CustomerCode = queryResult.CustomerCode;
           return customer;
       }
   }
Clone this wiki locally