|
| 1 | +using System; |
| 2 | +using System.Linq.Expressions; |
| 3 | +using NHibernate.Criterion; |
| 4 | +using NHibernate.Impl; |
| 5 | +using NHibernate.SqlCommand; |
| 6 | + |
| 7 | +namespace NHibernate |
| 8 | +{ |
| 9 | + public static class EntityJoinExtensions |
| 10 | + { |
| 11 | + #region QueryOver extensions |
| 12 | + |
| 13 | + public static TThis JoinEntityAlias<TThis, TEntity>(this TThis queryOver, Expression<Func<TEntity>> alias, ICriterion withClause, JoinType joinType = JoinType.InnerJoin, string entityName = null) where TThis : IQueryOver |
| 14 | + { |
| 15 | + CreateEntityCriteria(queryOver.UnderlyingCriteria, alias, withClause, joinType, entityName); |
| 16 | + return queryOver; |
| 17 | + } |
| 18 | + |
| 19 | + public static TThis JoinEntityAlias<TThis, TEntity>(this TThis queryOver, Expression<Func<TEntity>> alias, Expression<Func<bool>> withClause, JoinType joinType = JoinType.InnerJoin, string entityName = null) where TThis : IQueryOver |
| 20 | + { |
| 21 | + return JoinEntityAlias(queryOver, alias, Restrictions.Where(withClause), joinType, entityName); |
| 22 | + } |
| 23 | + |
| 24 | + public static IQueryOver<TRoot, TEntity> JoinEntityQueryOver<TRoot, TEntity>(this IQueryOver<TRoot> queryOver, Expression<Func<TEntity>> alias, Expression<Func<bool>> withClause, JoinType joinType = JoinType.InnerJoin, string entityName = null) |
| 25 | + { |
| 26 | + return JoinEntityQueryOver(queryOver, alias, Restrictions.Where(withClause), joinType, entityName); |
| 27 | + } |
| 28 | + |
| 29 | + public static IQueryOver<TRoot, TEntity> JoinEntityQueryOver<TRoot, TEntity>(this IQueryOver<TRoot> queryOver, Expression<Func<TEntity>> alias, ICriterion withClause, JoinType joinType = JoinType.InnerJoin, string entityName = null) |
| 30 | + { |
| 31 | + var q = CastOrThrow<ISupportEntityJoinQueryOver<TRoot>>(queryOver); |
| 32 | + return q.JoinEntityQueryOver(alias, withClause, joinType, entityName); |
| 33 | + } |
| 34 | + |
| 35 | + #endregion QueryOver extensions |
| 36 | + |
| 37 | + #region Criteria extensions |
| 38 | + |
| 39 | + public static ICriteria CreateEntityAlias(this ICriteria criteria, string alias, ICriterion withClause, JoinType joinType, string entityName) |
| 40 | + { |
| 41 | + CreateEntityCriteria(criteria, alias, withClause, joinType, entityName); |
| 42 | + return criteria; |
| 43 | + } |
| 44 | + |
| 45 | + public static ICriteria CreateEntityAlias<U>(this ICriteria criteria, Expression<Func<U>> alias, ICriterion withClause, JoinType joinType = JoinType.InnerJoin, string entityName = null) |
| 46 | + { |
| 47 | + CreateEntityCriteria(criteria, alias, withClause, joinType, entityName); |
| 48 | + return criteria; |
| 49 | + } |
| 50 | + |
| 51 | + public static ICriteria CreateEntityCriteria(this ICriteria criteria, string alias, ICriterion withClause, JoinType joinType, string entityName) |
| 52 | + { |
| 53 | + var c = CastOrThrow<ISupportEntityJoinCriteria>(criteria); |
| 54 | + return c.CreateEntityCriteria(alias, withClause, joinType, entityName); |
| 55 | + } |
| 56 | + |
| 57 | + public static ICriteria CreateEntityCriteria<U>(this ICriteria criteria, Expression<Func<U>> alias, ICriterion withClause, JoinType joinType = JoinType.InnerJoin, string entityName = null) |
| 58 | + { |
| 59 | + return CreateEntityCriteria(criteria, ExpressionProcessor.FindMemberExpression(alias.Body), withClause, joinType, entityName ?? typeof(U).FullName); |
| 60 | + } |
| 61 | + |
| 62 | + #endregion Criteria extensions |
| 63 | + |
| 64 | + private static T CastOrThrow<T>(object obj) where T : class |
| 65 | + { |
| 66 | + return obj as T |
| 67 | + ?? throw new ArgumentException($"{obj.GetType().FullName} requires to implement {typeof(T).FullName} interface to support explicit entity joins."); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments