首页 新闻 搜索 专区 学院

NHibernate Criteria封装的问题

1
悬赏园豆:60 [已关闭问题] 关闭于 2011-11-12 16:04

如何封装关联查询?例如  帖子(Posts)  帖子状态交叉(Post_PostState [PostID,StateID])   帖子状态类型(PostState [Flag属性,用来枚举的] )

其中 帖子(Posts)和 帖子状态类型(PostState)做了many to many 映射如果我有如下查询:

IList<CriteriaParameter> _query = new List<CriteriaParameter>();

int[] values = {5,10,15};

_query.Add(new CriteriaParameter("PostStates", "Flag", values, CriteriaOperator.NotIn));

_bllPost.FindByPage(PageIndex, PageSize, out RecordCount, _query);

应该怎么在CriteriaHelper类中封装类似这样的关联查询new CriteriaParameter(tablename,propertyname,value,criteriaOperator)呢?

CriteriaParameter类

    public class CriteriaParameter
{
private string _propertyName;
private string _tableName;
private object _value1;
private object _value2;
private object[] _values;
private CriteriaOperator _criteriaOperator;

public CriteriaParameter(string propertyName, object value1, CriteriaOperator criteriaOperator)
: this(propertyName, value1, null, criteriaOperator) { }

public CriteriaParameter(string propertyName, CriteriaOperator criteriaOperator)
: this(propertyName, null, null, criteriaOperator) { }

private CriteriaParameter(string propertyName, object value1, object value2, CriteriaOperator criteriaOperator)
{
this._propertyName = propertyName;
this._value1 = value1;
this._value2 = value2;
this._criteriaOperator = criteriaOperator;
}

///<summary>
/// 关联CriteriaOperator.In or NotIn测试
///</summary>
///<param name="propertyName"></param>
///<param name="values"></param>
public CriteriaParameter(string tableName, string propertyName, object[] values, CriteriaOperator criteriaOperator)
{
this._propertyName = propertyName;
this._values = values;
this._tableName = tableName;
this._criteriaOperator = criteriaOperator;
}

public string TableName
{
get { return _tableName; }
set { _tableName = value; }
}

public string PropertyName
{
get { return _propertyName; }
set { _propertyName = value; }
}
public object Value1
{
get { return _value1; }
set { _value1 = value; }
}
public object Value2
{
get { return _value2; }
set { _value2 = value; }
}
public object[] Values
{
get { return _values; }
set { _values = value; }
}

public CriteriaOperator CriteriaOperator
{
get { return _criteriaOperator; }
set { _criteriaOperator = value; }
}
}

public enum CriteriaOperator
{
Eq = 1,
Gt = 2,
Lt = 3,
Between = 4,
Like = 5,
In = 6,
NotIn = 16,
On = 7,
IsNotNullOrEmpty = 8,
IsNullOrEmpty = 9,
Or = 10,
Le = 11,
Ge = 12,
NotEq = 13,
OrderByDesc = 14,
OrderByAsc = 15
}

 

CriteriaHelper 类

public class CriteriaHelper
{
private static ICriteria AddCriteria(CriteriaParameter criteriaParameter, CriteriaOperator criteriaOperator, ICriteria criteria)
{
//CriteriaOperator.IsNullOrEmpty
if (criteriaOperator == CriteriaOperator.IsNullOrEmpty)
{
if (criteriaParameter.Value1 == null)
{
criteria.Add(NHibernate.Criterion.Expression.IsNull(criteriaParameter.PropertyName));
}
else
{
if (string.IsNullOrWhiteSpace(criteriaParameter.Value1 as string))
{
criteria.Add(NHibernate.Criterion.Expression.IsEmpty(criteriaParameter.PropertyName));
}
}
}
//CriteriaOperator.IsNotNullOrEmpty
if (criteriaOperator == CriteriaOperator.IsNotNullOrEmpty)
{
if (criteriaParameter.Value1 != null)
{
if (!string.IsNullOrWhiteSpace(criteriaParameter.Value1 as string))
{
criteria.Add(NHibernate.Criterion.Expression.IsNotEmpty(criteriaParameter.PropertyName));
}
}
else
{
criteria.Add(NHibernate.Criterion.Expression.IsNotNull(criteriaParameter.PropertyName));
}
}
return criteria;
}

///<summary>
/// 初始化ICriteria对象
///</summary>
public static ICriteria InitCriteria(ICriteria criteria, IList<CriteriaParameter> query)
{
foreach (var item in query)
{
switch (item.CriteriaOperator)
{
case CriteriaOperator.Eq:
criteria.Add(NHibernate.Criterion.Expression.Eq(item.PropertyName, item.Value1));
break;
case CriteriaOperator.Like:
criteria.Add(NHibernate.Criterion.Expression.Like(item.PropertyName,
String.Format("{0}%", item.Value1)));
break;
case CriteriaOperator.Gt:
criteria.Add(NHibernate.Criterion.Expression.Gt(item.PropertyName, item.Value1));
break;
case CriteriaOperator.Lt:
criteria.Add(NHibernate.Criterion.Expression.Lt(item.PropertyName, item.Value1));
break;
case CriteriaOperator.Between:
criteria.Add(NHibernate.Criterion.Expression.Between(item.PropertyName, item.Value1, item.Value2));
break;
case CriteriaOperator.In:
criteria.Add(NHibernate.Criterion.Expression.In(item.PropertyName, item.Values));
break;
case CriteriaOperator.NotIn://这里的管理怎么封装?
if (item.TableName != "")
criteria.CreateCriteria(item.TableName, JoinType.InnerJoin).Add(NHibernate.Criterion.Expression.Not(NHibernate.Criterion.Expression.In(item.PropertyName, item.Values)));
break;
case CriteriaOperator.IsNullOrEmpty:
AddCriteria(item, CriteriaOperator.IsNullOrEmpty, criteria);
break;
case CriteriaOperator.IsNotNullOrEmpty:
AddCriteria(item, CriteriaOperator.IsNullOrEmpty, criteria);
break;
case CriteriaOperator.Le:
criteria.Add(NHibernate.Criterion.Expression.Le(item.PropertyName, item.Value1));
break;
case CriteriaOperator.Ge:
criteria.Add(NHibernate.Criterion.Expression.Ge(item.PropertyName, item.Value1));
break;
case CriteriaOperator.OrderByDesc:
criteria.AddOrder(NHibernate.Criterion.Order.Desc(item.PropertyName));
break;
case CriteriaOperator.OrderByAsc:
criteria.AddOrder(NHibernate.Criterion.Order.Asc(item.PropertyName));
break;
}
}
return criteria;
}
}

 

用法测试

        ///<summary>
/// 获取分页记录
///</summary>
public IList<TEntity> FindByPage(int currentPage, int pageSize, out int recordCount, IList<CriteriaParameter> query)
{
var criteria = _session.CreateCriteria<TEntity>();
criteria = InitCriteria(criteria, query);

//利用投影得到总记录数
var criteriaCount = CriteriaTransformer.Clone(criteria);/
criteriaCount.ClearOrders();//清除Order
recordCount = criteriaCount.SetProjection(NHibernate.Criterion.Projections.RowCount()).UniqueResult<int>();

return criteria.SetFirstResult((currentPage - 1) * pageSize)
.SetMaxResults(pageSize)
.List<TEntity>();
}
Kratos Zhang的主页 Kratos Zhang | 初学一级 | 园豆:6
提问于:2011-11-12 14:48
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册