[英]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/ 我正在nhibernate中阅读有关aliastobean的内容,这是我的项目中遇到的问题,并且我已经与这篇文章交叉了: 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: 我发现有趣的是自定义AliasToBean变形程序,这是它的工作方式:
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: DTO类:
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? 似乎一切都很好,我了解他们在这里做了什么,但是仅使用常规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. 或者,也许我错过了这个概念,对于更复杂的aliastobean情况,这可能更好,但是仍然可以只使用常规转换器来进行别名。
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. 将其放在Select子句中会将其写入sql-query,这是额外的开销,并使日志和分析器中的查询变得复杂。 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.
另一个更有效的解决方案是简单的foreach,因为它避免创建匿名对象并且不调用委托。
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.