好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

NHibernate+Oracle10g搭建一个项目架构全程解析(五)

七、为表示层项目添加日志能力 1、在Web.Config里面添加如下代码: configdivs div name="log4net" type="log4net.Config.Log4NetConfigurationdivHandler,log4net"/ /configdivs log4net root level value="INFO" / appender-ref ref="LogFileAppender" / a

七、为表示层项目添加日志能力

1、在Web.Config里面添加如下代码:

2、在持久层项目OfficeDAL里面Properties的AssemblyInfo.cs文件添加如下代码:

[assembly: log4net.Config.XmlConfigurator(ConfigFile="Web.Config",Watch=true)]

运行Web项目,将会在项目下生成日志文件log-file.txt

3、我们可以在持久层添加自己的日志代码,做日志 输出:

using System; using System.Collections.Generic; using System.Text; using NHibernate; using OffficeModel.Pagination; using NHibernate.Criterion; using System.Collections; using log4net; namespace OfficeDAL { public class BaseService { protected ILog log = LogManager.GetLogger(typeof(BaseService)); protected ISession GetSession() { log.Info("Office :Session"); return HibernateSessionFactory.GetSession(); } /// /// 保存 /// /// public void Persist(Object obj) { ISession session = null; ITransaction tx = null; try { session = HibernateSessionFactory.GetSession(); tx = session.BeginTransaction(); session.Persist(obj); tx.Commit(); } catch { tx.Rollback(); throw; } finally { session.Close(); } } /// /// 删除对象 /// /// public void Remove(Object obj) { ISession session = null; ITransaction tx = null; try { session = HibernateSessionFactory.GetSession(); tx = session.BeginTransaction(); session.Delete(obj); tx.Commit(); } catch { tx.Rollback(); throw; } finally { session.Close(); } } /// /// 查询所有 /// /// /// /// public IList findAll () where T:new() { log.Info("Office: BaseService FindAll"); ISession session = null; try { session = HibernateSessionFactory.GetSession(); return session.CreateCriteria(typeof(T)).List (); } finally { session.Close(); } } /// /// 查询所有 /// /// /// /// public IList GetAll(Type entityType) { ISession session = null; try { session = HibernateSessionFactory.GetSession(); return session.CreateCriteria(entityType).List(); } finally { session.Close(); } } /// /// 根据对象oid进行查询 /// /// /// /// public T findById (object oid) where T:new() { ISession session = null; try { session = HibernateSessionFactory.GetSession(); return session.Get (oid); } finally { if (session != null) { session.Close(); } } } /// /// 修改对象 /// /// public void Update(object obj) { using (ISession session = HibernateSessionFactory.GetSession()) { session.Update(obj); session.Close(); } } public void DoPager(PageInfo pi) { log.Info("Office: DoPager"); if (pi.EntityType == null) { throw new Exception("分页类名不能为空"); } using (ISession session = HibernateSessionFactory.GetSession()) { ICriteria qbc = session.CreateCriteria(pi.EntityType); //总条数 qbc.SetProjection(NHibernate.Criterion.Projections.RowCount()); prepareConditions(qbc, pi.Conditions); pi.RecordCount = qbc .SetMaxResults(1) .UniqueResult (); //总页数 pi.PageCount = pi.RecordCount % pi.PageSize == 0? pi.RecordCount / pi.PageSize: pi.RecordCount / pi.PageSize + 1; //qbc.SetProjection(null); //分页结果 ICriteria _qbc = session.CreateCriteria(pi.EntityType); prepareConditions(_qbc, pi.Conditions); //设置排序 prepareOrder(_qbc, pi.OrderFields); //分页结果 pi.List = _qbc .SetFirstResult((pi.PageIndex - 1) * pi.PageSize) .SetMaxResults(pi.PageSize) .List(); } } /// /// 处理条件 /// /// /// private void prepareConditions(ICriteria qbc,params NCondition[] conditions) { if (qbc == null || conditions == null || conditions.Length == 0) { return; } foreach (NCondition condition in conditions) { switch (condition.Operate) { case Operation.EQ: qbc.Add(Expression.Eq(condition.PropertyName, condition.PropertyValue)); break; case Operation.GT: qbc.Add(Expression.Gt(condition.PropertyName, condition.PropertyValue)); break; case Operation.LT: qbc.Add(Expression.Lt(condition.PropertyName, condition.PropertyValue)); break; case Operation.GE: qbc.Add(Expression.Ge(condition.PropertyName, condition.PropertyValue)); break; case Operation.LE: qbc.Add(Expression.Le(condition.PropertyName, condition.PropertyValue)); break; case Operation.NE: qbc.Add(Expression.Not( Expression.Eq(condition.PropertyName, condition.PropertyValue) )); break; case Operation.BETWEEN: qbc.Add(Expression.Between( condition.PropertyName, (condition.PropertyValue as Object[])[0], (condition.PropertyValue as Object[])[1] ) ); break; case Operation.LIKE: qbc.Add(Expression.Like( condition.PropertyName, condition.PropertyValue.ToString(), MatchMode.Anywhere ) ); break; case Operation.IN: qbc.Add(Expression.In(condition.PropertyName, condition.PropertyValue as object[])); break; } } } /// /// 处理排序 /// /// /// private void prepareOrder(ICriteria qbc, params OffficeModel.Pagination.NOrder[] orderFields) { if (qbc == null || orderFields == null || orderFields.Length == 0) { return; } foreach (OffficeModel.Pagination.NOrder order in orderFields) { qbc.AddOrder( order.OrderType == OffficeModel.Pagination.NOrder.OrderDirection.ASC ? Order.Asc(order.PropertyName) : Order.Desc(order.PropertyName) ); } } /// /// 根据单个属性查询对象 /// /// /// /// /// public T FindByProperty (String propertyName,Object propertyValue) { using (ISession session = HibernateSessionFactory.GetSession()) { return session .CreateCriteria(typeof(T)) .Add(Restrictions.Eq(propertyName, propertyValue)) .SetMaxResults(1) .UniqueResult (); } } public T FindByProperty (String[] propertyNames,Object[] propertyValues) { if (propertyNames == null || propertyValues == null || propertyNames.Length == 0 || propertyValues.Length == 0 || propertyNames.Length != propertyValues.Length) { return default(T); } using (ISession session = HibernateSessionFactory.GetSession()) { ICriteria qbc = session.CreateCriteria(typeof(T)); for (int i = 0; i (); } } } }

观察日志文件 输出:

查看更多关于NHibernate+Oracle10g搭建一个项目架构全程解析(五)的详细内容...

  阅读:41次