假設我們有一個存儲客戶細節的系統和一個存儲員工細節的系統(假設情況!)。當EmployeeSystem訪問員工,客戶信息是從使用WCF的ClientSystem訪問,在IUserType實施:NHibernate:延遲加載IUserType
NHibernate的映射:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="EmployeeSystem" namespace="EmployeeSystem.Entities">
<class name="Employee" table="`Employee`" >
<id name="Id" column="`Id`" type="long">
<generator class="native" />
</id>
<property name="Name"/>
<property
name="Client" column="`ClientId`"
lazy="true"
type="EmployeeSystem.UserTypes.ClientUserType, EmployeeSystem" />
</class>
</hibernate-mapping>
IUserType實現:
public class ClientUserType : IUserType
{
...
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
object obj = NHibernateUtil.Int32.NullSafeGet(rs, names[0]);
IClientService clientService = new ClientServiceClient();
ClientDto clientDto = null;
if (null != obj)
{
clientDto = clientService.GetClientById(Convert.ToInt64(obj));
}
Client client = new Client
{
Id = clientDto.Id,
Name = clientDto.Name
};
return client;
}
...
}
即使我在該屬性上具有lazy =「true」,只要Employee被加載,它就會加載客戶端。這是正確的行爲?我是否必須在NullSafeGet中實現惰性加載,還是我錯過了某些東西?
向我們展示如何加載員工。 – Dmitry