解读Petshop3.2用Nhibernate重构系列(三)

这一篇讲的是如何插入/删除简单对象--一对一关系或者简单的一个对象。
首先,我们来看看SignOn的NHibernate描述:
   SignOn.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
    
<class name="PetShop.BLL.SignOn, PetShop.BLL" table="SignOn">
        
<id name="UserId" column="UserName" length="20" type="String">
            
<generator class="assigned" />
        
</id>
        
<property name="Password" length="20" column="Password" not-null="true" type="String" />
        
<one-to-one name="Profile" class="PetShop.BLL.Profile, PetShop.BLL" cascade="save-update" />
        
<one-to-one name="Account" class="PetShop.BLL.Account, PetShop.BLL" cascade="save-update" />
    
</class>
</hibernate-mapping>

   在这里我们看到了,这个对象对应的命名空间的PetShop.BLL.SignOn对象,对应的数据库表是SignOn。他拥有4个属性:UserID Password Account信息 Profile信息
     其中Userid对应了数据库表的用户名,20个字符型,属于赋值型的
           Password是一个不为空的20长度的字符型属性
           Profile是一个1:1的属性,类名:PetShop.BLL.Profile
           Account是一个1:1的属性,类名:PetShop.BLL.Account
           他们指定了当作新增/修改的时候提示NHibernate进行级联操作。
接着,我们来看看SignOn对象的DataAccess接口

using System;

//References to PetShop specific libraries
//PetShop busines entity library
using PetShop.BLL;

namespace PetShop.IDAO{
    
/// <summary>
    
/// Inteface for the Account DAL
    
/// </summary>

    public interface ISignOnDAO{
        
/// <summary>
        
/// Authenticate a user
        
/// </summary>

        bool SignIn(SignOn signOn);

        
/// <summary>
        
/// Insert an account into the database
        
/// </summary>
        
/// <param name="account">Account to insert</param>

        void Insert(SignOn signOn);

        
/// <summary>
        
/// Update an account in the database
        
/// </summary>
        
/// <param name="Account">Account information to update</param>

        void Update(SignOn signOn);
    }

}


在这里,我们可以很清楚地看到SignOn对象所具备的行为规范:SignOn Insert Update Delete

接着,我们来看看SignOn实体类

using System;
using PetShop.IDAO;

namespace PetShop.BLL{
    
/// <summary>
    
/// A business compoment to manage user SignOn
    
/// </summary>

    [Serializable]
    
public class SignOn{
        
private string _userId;
        
private string _password;
        
private Profile _profile;
        
private Account _account;

        
public string UserId{
            
getreturn _userId; }
            
set{ _userId = value; }
        }


        
public string Password{
            
getreturn _password; }
            
set{ _password = value; }
        }


        
public Profile Profile{
            
getreturn _profile; }
            
set
                value.SignOn 
= this;
                _profile 
= value; 
            }

        }


        
public Account Account{
            
getreturn _account; }
            
set
                value.SignOn 
= this;
                _account 
= value;
            }

        }


        
/// <summary>
        
/// Method to login into the system. The user must supply a username and password
        
/// </summary>

        public bool SignIn(){
            
// Validate input
            if ((UserId.Trim() == string.Empty) || (Password.Trim() == string.Empty)) return false;

            
// Try to sign in with the given credentials
            return ((ISignOnDAO)ObjectFactory.GetInstance("SignOnDAO")).SignIn(this);
        }


        
/// <summary>
        
/// A method to insert a new Account
        
/// </summary>

        public void Insert(){
            
// Validate input
            if (this.UserId.Trim() == string.Empty) return;

            
// Call the DAL to insert the account
            ((ISignOnDAO)ObjectFactory.GetInstance("SignOnDAO")).Insert(this);
        }


        
/// <summary>
        
/// A method to update an existing account
        
/// </summary>

        public void Update(){
            
// Validate input
            if (this.UserId.Trim() == string.Empty) return ;

            
// Send the udpated account information to the DAL
            ((ISignOnDAO)ObjectFactory.GetInstance("SignOnDAO")).Update(this);
        }

    }

}

在这里,我们可以看到当一个SignOn对象作新增操作的时候
 他通过对象工厂去获取一个SignOnDAO的实例,并且以接口的形式返回,然后执行更新的操作。其实,我个人偏向,还是应该增加一个外观层,让SignOnDAO只是一个实体类。
哦,忘了补充SingONDAO对象了。
using System;
using System.Collections;
using NHibernate;
using PetShop.BLL;
using PetShop.IDAO;

namespace PetShop.DAO{
    
    
public class SignOnDAO : ISignOnDAO{
        
        
public void Insert(SignOn signOn){
            ISession session 
= Sessions.PetShop.OpenSession();
            session.Save(signOn);
            session.Flush();
            session.Close();
        }


        
public void Update(SignOn signOn){
            ISession session 
= Sessions.PetShop.OpenSession();
            session.Update(signOn);
            session.Flush();
            session.Close();
        }


        
public bool SignIn(SignOn signOn){
            ISession session 
= Sessions.PetShop.OpenSession();
            IList list 
= session.CreateQuery("from SignOn s where s.UserId = :userId and s.Password = :password")
                                .SetString(
"userId", signOn.UserId)
                                .SetString(
"password", signOn.Password)
                                .List();
            session.Close();
            
if( list.Count == 0 ) return false;
            SignOn result  
= (SignOn)list[0];
            signOn.Profile 
= result.Profile;
            signOn.Account 
= result.Account;
            
return true;
        }

    }

}
我们看到这个对象还是比较简单的。
启动一个Session,保存,提交,关闭

原文地址:https://www.cnblogs.com/wildfish/p/139105.html