Skip to content

Potential improvement to AliasToBeanResultTransformer #2700

Closed
@Kermittt

Description

@Kermittt

I often find myself performing a QueryOver to collect data into some interim model, then re-processing the entire result set to reformat the data into the actual results I want to expose. In many cases this also includes future queries which are merged into the main result set as I re-process them. To improve this process, I have extended the AliasToBeanResultTransformer to accept a list of transform functions, which can be used to manipulate the values as they are being copied into the models. In many cases this completely avoids the need to re-process the data after the initial transformation. This has been particularly useful for us with MySQL where we often perform subqueries which return JSON strings, then use the additional transformations to deserialize that data directly into the result model.

To implement this, I inherited from AliasToBeanResultTransformer, added a new parameter to the constructor, and overrode TransformTuple to check for, and execute the transformers for each alias, if one was supplied. This required me to access a number of private variables and methods in the AliasToBeanResultTransformer, so I currently have a copy of that class, with the relevant members modified to be protected instead of private. (They are _resultClass, _beanConstructor and SetProperty.)
Once I had completed this work I realised that since the new parameter is optional, this modification could potentially be implemented directly in the existing AliasToBeanResultTransformer with zero impact on existing code

My question is, if I were to submit this as a PR, is it something that would be considered in scope for the NHibernate core, and if so, which implementation would be preferred?
I could either submit this as...

  • A direct modification of AliasToBeanResultTransformer with an optional parameter on the constructor.
  • Or, as a modification to AliasToBeanResultTransformer to expose the necessary private members, and a new derived transformer which accepts the additional parameter. (Ours is currently called AliasToBeanValueResultTransformer, but I'm not thrilled with that name.)

To give a little visibility into how this actually works in code...

This is the new constructor...

public AliasToBeanValueResultTransformer(Type resultClass, params (string Alias, Func<object, object, object> Transform)[] valueTransformers)

...and a simple usage example...
(This one is deserializing a list of strings, but of course the transformation can potentially do anything you like with the target alias value, or other values on the model which have already been populated.)

.TransformUsing(CustomTransformers.AliasToBeanValue<Model>(
    (m => m.Comments, (v, _) => ((string)v).FromJson(new List<string>())),
 )).ListAsync<Model>(ct);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions