码迷,mamicode.com
首页 > Web开发 > 详细

Castle.net

时间:2017-03-23 14:17:17      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:返回值   ring   lis   失败   数据   count   scalar   dal   delete   

using System;

using System.Collections.Generic;

using System.Linq;
using System.Text;
using Castle.ActiveRecord;
using NHibernate.Criterion;
using System.ComponentModel.DataAnnotations;
using NHibernate;

namespace Model
{
/// <summary>
/// 实体类基类,所有实体必须继承自EntityBase(为了被ORM框架管理)
/// </summary>
/// <typeparam name="T">子类的类型,如:Driver,Users</typeparam>
public class EntityBase<T>:ActiveRecordBase where T:class
{
[PrimaryKey(PrimaryKeyType.Identity)]//采用Castle.Net提供的算法生成ID
[Display(AutoGenerateField = false)]//在生成MVC3.0视图时不生成本属性
public virtual int ID { get; set; }//主键


public void Create(T t)
{
ActiveRecordBase.Create(t);
}

public void Update(T t)
{
ActiveRecordBase.Update(t);
}

public void DeleteAll()//删除所有数据
{
DeleteAll(typeof(T));
}

public void Delete(int id)//根据id删除实体
{
T t = Get(id);//根据id获取对象
if (t != null)//如果对象存在
{
Delete(t);//删除它
}
}

public IList<T> GetAll()//获取所有对象
{
return FindAll(typeof(T)) as IList<T>;
}

public IList<T> GetAll(IList<ICriterion> queryConditions)//根据查询条件集合获取所有对象
{
Array arr = FindAll(typeof(T));//Castle.net默认是数组
return arr as IList<T>;//将数组转换为IList集合
}

public T Get(int ID)//根据ID获取对象
{
return FindByPrimaryKey(typeof(T), ID) as T;
}

//分页区和取对象集合
public IList<T> GetPaged(IList<ICriterion> queryConditions, IList<Order> orderList, int pageIndex, int pageSize, out int count)
{
if (queryConditions == null)//如果为null则赋值为一个总数为0的集合
{
queryConditions = new List<ICriterion>();
}
if (orderList == null)//如果为null则赋值为一个总数为0的集合
{
orderList = new List<Order>();
}
count = Count(typeof(T), queryConditions.ToArray());//根据查询条件获取满足条件的对象总数
Array arr = SlicedFindAll(typeof(T), (pageIndex - 1) * pageSize, pageSize, orderList.ToArray(),queryConditions.ToArray());//根据查询条件分页获取对象集合
return arr as IList<T>;
}

/// <summary>
/// 根据查询条件分页获取实体
/// </summary>
/// <param name="queryConditions">查询条件集合</param>
/// <param name="pageIndex">当前页码,从1开始</param>
/// <param name="pageSize">页面大小</param>
/// <param name="count">返回满足查询条件</param>
/// <returns>返回满足查询条件的实体</returns>
public IList<T> GetPaged(IList<KeyValuePair<string, string>> queryConditions, int pageIndex, int pageSize, out int count)
{
//实例化一个hql查询语句对象
StringBuilder hql = new StringBuilder(@"from " + typeof(T).Name + " d");
//根据查询条件构造hql查询语句
for (int i = 0; i < queryConditions.Count; i++)
{
KeyValuePair<string, string> keyv = queryConditions[i];//获取当前序号对应的条件
if (!string.IsNullOrEmpty(keyv.Value))
{
AddHqlSatements(hql);//增加where或and语句
hql.Append("d." + keyv.Key + " = : q_" + i.ToString());
}
}

ISession session = ActiveRecordBase.holder.CreateSession(typeof(T));//获取管理DeliveryForm的session对象
IQuery query = session.CreateQuery(hql.ToString());//获取满足条件的数据
IQuery queryScalar = session.CreateQuery("select count(ID) " + hql.ToString());//获取满足条件的数据的总数
for (int i = 0; i < queryConditions.Count; i++)
{
KeyValuePair<string, string> keyv = queryConditions[i];//获取当前序号对应的条件
if (!string.IsNullOrEmpty(keyv.Value))
{
queryScalar.SetString("q_" + i, keyv.Value);//用查询条件的值去填充hql,如d.Transportor.Name="michael"
query.SetString("q_" + i, keyv.Value);
}
}
IList<object> result = queryScalar.List<object>();//执行查询条件总数的查询对象,返回一个集合(有一点怪异)
int.TryParse(result[0].ToString(), out count);//尝试将返回值的第一个值转换为整形,并将转换成功的值赋值给count,如果转换失败,count=0
query.SetFirstResult((pageIndex - 1) * pageSize);//设置获取满足条件实体的起点
query.SetMaxResults(pageSize);//设置获取满足条件实体的终点
IList<T> arr = query.List<T>();//返回当前页的数据
session.Close();//关闭session
return arr;
}

protected void AddHqlSatements(StringBuilder hql)
{
if (!hql.ToString().Contains("where"))//查询语句的开始条件是where
{
hql.Append(" where ");
}
else//当hql中有了一个where后再添加查询条件时就应该使用and了
{
hql.Append(" and ");
}
}

public IList<T> Find(IList<ICriterion> queryConditions)
{
Array arr = ActiveRecordBase.FindAll(typeof(T), queryConditions.ToArray());
return arr as IList<T>;
}
}
}

Castle.net

标签:返回值   ring   lis   失败   数据   count   scalar   dal   delete   

原文地址:http://www.cnblogs.com/cqxhl/p/6604566.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!