简体   繁体   中英

Nhibernate custom AliasToBean

I'm reading about aliastobean in nhibernate, for something in my project, and I have crossed with this article : http://blog.andrewawhitaker.com/blog/2014/06/19/queryover-series-part-4-transforming/

What I found interesting is the custom AliasToBean trasformer, here is how it works:

public class AliasToBeanWithCallbackTransformer<T> : IResultTransformer
{
    private readonly AliasToBeanResultTransformer aliasToBeanTransformer;
    private readonly Action<T> callback;

    public AliasToBeanWithCallbackTransformer(Action<T> callback)
    {
        this.aliasToBeanTransformer = new AliasToBeanResultTransformer(typeof(T));
        this.callback = callback;
    }

    public IList TransformList(IList collection)
    {
        return this.aliasToBeanTransformer.TransformList(collection);
    }

    public object TransformTuple(object[] tuple, string[] aliases)
    {
        object result = this.aliasToBeanTransformer.TransformTuple(tuple, aliases);

        // Call the callback before returning the result.
        callback((T)result);

        return result;
    }
}

The DTO class:

public class ProductReviewDTO
{
    public int ProductReviewID { get; set; }

    public int Rating { get; set; }

    public string Comments { get; set; }

    public DateTime DateRetrieved { get; set; }
}

And the usage:

DateTime dateRetrieved = DateTime.Now;

IList<ProductReviewDTO> highestReviews =
    session.QueryOver<ProductReview>()
        .SelectList(list => list
            .Select(pr => pr.Comments).WithAlias(() => result.Comments)
            .Select(pr => pr.Id).WithAlias(() => result.ProductReviewID)
            .Select(pr => pr.Rating).WithAlias(() => result.Rating)
        )
        // Assign "DateRetrieved correctly:
        .TransformUsing(new AliasToBeanWithCallbackTransformer<ProductReviewDTO>(
            hp => hp.DateRetrieved = dateRetrieved))
        .Take(10)
        .List<ProductReviewDTO>();

Which seems all good and I understand what they did here, but isn't it much more simpler to just do it within the select list with a regular Transformer?

Like this:

DateTime dateRetrieved = DateTime.Now;

IList<ProductReviewDTO> highestReviews =
    session.QueryOver<ProductReview>()
        .SelectList(list => list
            .Select(pr => pr.Comments).WithAlias(() => result.Comments)
            .Select(pr => pr.Id).WithAlias(() => result.ProductReviewID)
            .Select(pr => pr.Rating).WithAlias(() => result.Rating)
            .Select(() => dateRetrieved).WithAlias(() => result.DateRetrieved)
        )
        // Assign "DateRetrieved correctly:
        .TransformUsing(Transformers.AliasToBean<ProductReviewDTO>())
        .Take(10)
        .List<ProductReviewDTO>();

Or maybe I'm missing the concept of this, maybe this is better for more complicate aliastobean case, but still, you can always do the alias with just the regular transformers.

So anyone knows whats the point?

Having it in the Select clause will write it to the sql-query which is additional overhead and complicates the query in the log and analyser. The data transfered will also increase. I doubt it would matter in your example. Another more efficient solution would be a simple foreach because it avoids creating an anonymous object and does not call a Delegate.

DateTime dateRetrieved = DateTime.Now;

IList<ProductReviewDTO> highestReviews =
    session.QueryOver<ProductReview>()
        .SelectList(list => list
            .Select(pr => pr.Comments).WithAlias(() => result.Comments)
            .Select(pr => pr.Id).WithAlias(() => result.ProductReviewID)
            .Select(pr => pr.Rating).WithAlias(() => result.Rating)
        )
        .TransformUsing(Transformers.AliasToBean<ProductReviewDTO>())
        .Take(10)
        .List<ProductReviewDTO>();

foreach (var review in highestReviews)
    review.DateRetrieved = dateRetrieved;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM