NHibernate中的HQL非常的方便,它属于一种类面向对象的SQL语句。
该篇基于上一篇。
我们直接进入数据访问层DAL和数据访问层测试DAL.Test
DAL下的AnimalDAL.cs代码如下:
using System;
using System.Collections.Generic;
using Model.Entities;
using NHibernate;
namespace DAL
{
public class AnimalDAL
{
private ISession m_session;
public AnimalDAL(ISession session)
{
m_session = session;
}
public AnimalInfo GetAnimalByID(Int32 animalID)
{
return m_session.Get<AnimalInfo>(animalID);
}
public IList<AnimalInfo> GetAnimals()
{
String queryString = "from AnimalInfo";
IQuery query = m_session.CreateQuery(queryString);
return query.List<AnimalInfo>();
}
public IList<Int32> GetAnimalsID()
{
String queryString = "select animal.AnimalID from AnimalInfo as animal";
IQuery query = m_session.CreateQuery(queryString);
return query.List<Int32>();
}
public IList<AnimalInfo> SearchAnimal(String animalType)
{
String queryString = "from AnimalInfo as animal where animal.AnimalType like '%:animalType%'";
IQuery query = m_session.CreateQuery(queryString).SetString("animalType", animalType);
return query.List<AnimalInfo>();
}
}
}
在数据访问层AnimalDAL.cs中,小菜增加了三个函数GetAnimals(),GetAnimalsID(),SearchAnimal(String animalType)using System.Collections.Generic;
using Model.Entities;
using NHibernate;
namespace DAL
{
public class AnimalDAL
{
private ISession m_session;
public AnimalDAL(ISession session)
{
m_session = session;
}
public AnimalInfo GetAnimalByID(Int32 animalID)
{
return m_session.Get<AnimalInfo>(animalID);
}
public IList<AnimalInfo> GetAnimals()
{
String queryString = "from AnimalInfo";
IQuery query = m_session.CreateQuery(queryString);
return query.List<AnimalInfo>();
}
public IList<Int32> GetAnimalsID()
{
String queryString = "select animal.AnimalID from AnimalInfo as animal";
IQuery query = m_session.CreateQuery(queryString);
return query.List<Int32>();
}
public IList<AnimalInfo> SearchAnimal(String animalType)
{
String queryString = "from AnimalInfo as animal where animal.AnimalType like '%:animalType%'";
IQuery query = m_session.CreateQuery(queryString).SetString("animalType", animalType);
return query.List<AnimalInfo>();
}
}
}
String queryString = "from AnimalInfo";
String queryString = "select animal.AnimalID from AnimalInfo as animal";
String queryString = "from AnimalInfo as animal where animal.AnimalType like '%:animalType%'";
从这三个语句可以看出我们过度到HQL基本没什么阻碍。和SQL语句类似,而且还更方便。
HQL语句充满面向对象的影子,看起来很舒服。
还有其它一些功能,如:where,order by,group by等等大家可以自己试试,和Sql语句没有多少区别。
测试
DAL.Test下的AnimalDALFixture.cs代码如下:
using System;
using System.Collections.Generic;
using Model.Entities;
using NHibernate;
using NUnit.Framework;
namespace DAL.Test
{
[TestFixture]
public class AnimalDALFixture
{
private ISession m_session;
private AnimalDAL m_animalDAL;
private IList<AnimalInfo> m_expectedAnimals; // 数据库中已有数据
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
m_expectedAnimals = new List<AnimalInfo>();
AnimalInfo animalInfo = null;
animalInfo = new AnimalInfo();
animalInfo.AnimalID = 1;
animalInfo.AnimalType = "狗";
m_expectedAnimals.Add(animalInfo);
animalInfo = new AnimalInfo();
animalInfo.AnimalID = 2;
animalInfo.AnimalType = "猫";
m_expectedAnimals.Add(animalInfo);
}
[SetUp]
public void SetUp()
{
m_session = SessionManager.GetSession();
m_animalDAL = new AnimalDAL(m_session);
}
[Test]
public void GetAnimalByIDTest()
{
AnimalInfo animalInfo = m_animalDAL.GetAnimalByID(m_expectedAnimals[0].AnimalID);
Assert.AreEqual(m_expectedAnimals[0].AnimalID, animalInfo.AnimalID);
Assert.AreEqual(m_expectedAnimals[0].AnimalType, animalInfo.AnimalType);
}
[Test]
public void GetAnimalsTest()
{
IList<AnimalInfo> animals = m_animalDAL.GetAnimals();
for (int i = 0; i < m_expectedAnimals.Count; i++)
{
Assert.AreEqual(m_expectedAnimals[i].AnimalID, animals[i].AnimalID);
Assert.AreEqual(m_expectedAnimals[i].AnimalType, animals[i].AnimalType);
}
}
[Test]
public void GetAnimalsID()
{
IList<Int32> animalsID = m_animalDAL.GetAnimalsID();
for (int i = 0; i < m_expectedAnimals.Count; i++)
{
Assert.AreEqual(m_expectedAnimals[i].AnimalID, animalsID[i]);
}
}
[Test]
public void SearchAnimalTest()
{
IList<AnimalInfo> animals = m_animalDAL.SearchAnimal(m_expectedAnimals[0].AnimalType);
Assert.AreEqual(1, animals.Count); // 只找到一条记录
Assert.AreEqual(m_expectedAnimals[0].AnimalID, animals[0].AnimalID);
Assert.AreEqual(m_expectedAnimals[0].AnimalType, animals[0].AnimalType);
}
}
}
using System.Collections.Generic;
using Model.Entities;
using NHibernate;
using NUnit.Framework;
namespace DAL.Test
{
[TestFixture]
public class AnimalDALFixture
{
private ISession m_session;
private AnimalDAL m_animalDAL;
private IList<AnimalInfo> m_expectedAnimals; // 数据库中已有数据
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
m_expectedAnimals = new List<AnimalInfo>();
AnimalInfo animalInfo = null;
animalInfo = new AnimalInfo();
animalInfo.AnimalID = 1;
animalInfo.AnimalType = "狗";
m_expectedAnimals.Add(animalInfo);
animalInfo = new AnimalInfo();
animalInfo.AnimalID = 2;
animalInfo.AnimalType = "猫";
m_expectedAnimals.Add(animalInfo);
}
[SetUp]
public void SetUp()
{
m_session = SessionManager.GetSession();
m_animalDAL = new AnimalDAL(m_session);
}
[Test]
public void GetAnimalByIDTest()
{
AnimalInfo animalInfo = m_animalDAL.GetAnimalByID(m_expectedAnimals[0].AnimalID);
Assert.AreEqual(m_expectedAnimals[0].AnimalID, animalInfo.AnimalID);
Assert.AreEqual(m_expectedAnimals[0].AnimalType, animalInfo.AnimalType);
}
[Test]
public void GetAnimalsTest()
{
IList<AnimalInfo> animals = m_animalDAL.GetAnimals();
for (int i = 0; i < m_expectedAnimals.Count; i++)
{
Assert.AreEqual(m_expectedAnimals[i].AnimalID, animals[i].AnimalID);
Assert.AreEqual(m_expectedAnimals[i].AnimalType, animals[i].AnimalType);
}
}
[Test]
public void GetAnimalsID()
{
IList<Int32> animalsID = m_animalDAL.GetAnimalsID();
for (int i = 0; i < m_expectedAnimals.Count; i++)
{
Assert.AreEqual(m_expectedAnimals[i].AnimalID, animalsID[i]);
}
}
[Test]
public void SearchAnimalTest()
{
IList<AnimalInfo> animals = m_animalDAL.SearchAnimal(m_expectedAnimals[0].AnimalType);
Assert.AreEqual(1, animals.Count); // 只找到一条记录
Assert.AreEqual(m_expectedAnimals[0].AnimalID, animals[0].AnimalID);
Assert.AreEqual(m_expectedAnimals[0].AnimalType, animals[0].AnimalType);
}
}
}
在一次感叹一下,测试真的是个好东西啊!!!啊!!!
代码下载:https://files.cnblogs.com/a-peng/ORM_Chapter02.rar