目前我按以下方式创建标准:
gameCriteria = Restrictions.Eq("Name", videoGameName);
我想知道流利的nhibernate是否添加了允许以下内容的任何功能:
gameCriteria = Restrictions.Eq<VideoGame>(v => v.Name, videoGameName);
简而言之,我想删除魔术字符串。 (我的实际查询往往涉及许多魔术字符串,这会导致由于拼写错误等导致的错误。)
答案 0 :(得分:1)
刚刚在另一位开发人员写的存储库中找到答案。
gameCriteria = Restrictions.Eq(Projections.Property<VideoGame>(v => v.Name), videoGameName);
现在我觉得我花了很多时间来寻找这个。
答案 1 :(得分:0)
内置的NHibernate类Restrictions.cs。它提供了这种方法:
/// <summary>
/// Build an ICriterion for the given property
/// </summary>
/// <param name="expression">lambda expression identifying property</param>
/// <returns>returns LambdaRestrictionBuilder</returns>
public static LambdaRestrictionBuilder On<T>(Expression<Func<T, object>> expression)
{
ExpressionProcessor.ProjectionInfo projection =
ExpressionProcessor.FindMemberProjection(expression.Body);
return new LambdaRestrictionBuilder(projection);
}
我们可以像这样使用它:
// exact match
Restrictions.On<VideoGame>(v => v.Name == videoGameName);
或
// LIKE expression as 'videoGameName%'
Restrictions.On<VideoGame>(v => v.Name).IsLike(videoGameName, MatchMode.Start);