Nhibernate自定义AliasToBean

时间:2016-03-02 07:13:10

标签: c# .net nhibernate queryover

我正在阅读关于nhibernate中的aliastobean,对于我的项目中的某些内容,我已经越过了这篇文章: http://blog.andrewawhitaker.com/blog/2014/06/19/queryover-series-part-4-transforming/

我发现有趣的是自定义AliasToBean trasformer, 这是它的工作原理:

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;
    }
}

DTO课程:

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

    public int Rating { get; set; }

    public string Comments { get; set; }

    public DateTime DateRetrieved { get; set; }
}

用法:

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>();

这看起来一切都很好,我理解他们在这里做了什么,但是在使用常规Transformer的选择列表中做这件事要简单得多吗?

像这样:

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>();

或者我可能错过了这个概念,也许这对于更复杂的aliastobean案例更好,但是,你仍然可以使用常规变换器来做别名。

所以任何人都知道这一点吗?

1 个答案:

答案 0 :(得分:0)

将它写入Select子句会将其写入sql-query,这是额外的开销,并使日志和分析器中的查询复杂化。转移的数据也将增加。我怀疑这对你的例子很重要。另一个更有效的解决方案是简单的foreach,因为它避免了创建匿名对象而不会调用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;