您的位置:首页 > 数据库 > Oracle

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

2009-10-22 19:59 441 查看
七、为表示层项目添加日志能力

1、在Web.Config里面添加如下代码:
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net>
<root>
<level value="INFO" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
<param name="File" value="log-file.txt" />
<param name="AppendToFile" value="false" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]/r/n"/>
<param name="Footer" value="[Footer]/r/n"/>
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x]  - %m%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="WARN" />
</filter>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="WARN" />
</filter>
</appender>
</log4net>


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();
}

/// <summary>
/// 保存
/// </summary>
/// <param name="obj"></param>
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();
}
}

/// <summary>
/// 删除对象
/// </summary>
/// <param name="obj"></param>
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();
}
}

/// <summary>
/// 查询所有
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entiyType"></param>
/// <returns></returns>
public IList<T> findAll<T>() where T:new()
{
log.Info("Office: BaseService FindAll");
ISession session = null;
try
{
session = HibernateSessionFactory.GetSession();
return session.CreateCriteria(typeof(T)).List<T>();
}
finally
{
session.Close();
}
}

/// <summary>
/// 查询所有
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entiyType"></param>
/// <returns></returns>
public IList GetAll(Type entityType)
{
ISession session = null;
try
{
session = HibernateSessionFactory.GetSession();
return session.CreateCriteria(entityType).List();
}
finally
{
session.Close();
}
}

/// <summary>
/// 根据对象oid进行查询
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="oid"></param>
/// <returns></returns>
public T findById<T>(object oid) where T:new()
{
ISession session = null;
try
{
session = HibernateSessionFactory.GetSession();
return session.Get<T>(oid);
}
finally
{
if (session != null)
{
session.Close();
}

}
}

/// <summary>
/// 修改对象
/// </summary>
/// <param name="obj"></param>
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<int>();
//总页数
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();
}
}

/// <summary>
/// 处理条件
/// </summary>
/// <param name="qbc"></param>
/// <param name="conditions"></param>
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;
}
}
}

/// <summary>
/// 处理排序
/// </summary>
/// <param name="qbc"></param>
/// <param name="orderFields"></param>
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)
);
}
}

/// <summary>
/// 根据单个属性查询对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="propertyName"></param>
/// <param name="propertyValue"></param>
/// <returns></returns>
public T FindByProperty<T>(String propertyName,Object propertyValue)
{
using (ISession session = HibernateSessionFactory.GetSession())
{
return
session
.CreateCriteria(typeof(T))
.Add(Restrictions.Eq(propertyName, propertyValue))
.SetMaxResults(1)
.UniqueResult<T>();
}
}

public T FindByProperty<T>(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 < propertyNames.Length; i++)
{
qbc.Add(Restrictions.Eq(propertyNames[i],propertyValues[i]));
}

qbc.SetMaxResults(1);
return qbc.UniqueResult<T>();
}
}
}
}


观察日志文件输出:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: