0
我閱讀有關NHibernate的aliastobean,在我的項目的東西,而我這篇文章劃線: http://blog.andrewawhitaker.com/blog/2014/06/19/queryover-series-part-4-transforming/NHibernate的定製AliasToBean
我發現有趣的是自定義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>();
這似乎都不錯,我知道他們在這裏做什麼,但是是不是更簡單,只是一個普通的變壓器做選擇列表中?
像這樣:
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情況更復雜一點,但仍然,你總是可以做到只用普通變壓器的別名。
因此,任何人都知道什麼是重點?
也許你應該直接通過在博客上發表評論來問博客作者,如果他想在這裏回答,最終會指給他。 –