In the previous article, we built the entire solution project and built the database to complete the entity classes and nhibernate mapping files. In this article, you will define the data access interface and implement the interface using NHibernate. Use spring.net to configure DAO. and unit Test it.
The data access layer, like the PetShop framework, is divided into data access interfaces and implementations, but the data access implementation is much clearer and more obvious than the nhibernate itself, so it's not for multiple databases, But to nhibernate the plug, even if one day found that due to some problems, such as performance problems, you can implement the Idal interface, but not to the business layer caused a larger change.
Here, in fact, we still do not know what the front desk really needs, so we simply realize the deletion of the entity to check it. When other functions are needed, the interface and implementation are perfected.
Directcenter.idal is not dependent on spring or nhibernate, just add the project reference Directcenter.model. To the user, add the interface IUserDao.cs
IUserDao.cs
using System.Collections;
using DirectCenter.Model;
namespace DirectCenter.IDAL
{
public interface IUserDao
{
User FindById(string userId);
void Delete(User user);
User Save(User user);
User SaveOrUpdate(User user);
}
}
Again, we add Icompanydao and Idepartmentdao.
Next, we implement the data interface just now, add the project references to Directcenter.idal and Directcenter.model in the Directcenter.dal project, Then add the reference Spring.data.nhibernate20,spring.data,spring.core (below lib/spring.net).
One way to do this is to use NHibernate directly to complete data access:
UserDao.cs
public User Save(User user)
{
//通过一种方式获取Session
Session session = SessionFactory.OpenSession();
session.Save(user);
}