这个帖子:
http://kennytordeur.blogspot.com/2011/04/nhibernate-in-combination-with_06.html
描述如何从数据库以外的资源(在本例中为 Web 服务)加载实体。这很好,但是如果我在一个查询中加载多个客户端,每个客户端都有不同的 MaritialState,它将不得不为每个客户端调用 web 服务。有没有办法预加载所有婚姻状态,所以它不必为每个客户来回访问网络服务?
这个帖子:
http://kennytordeur.blogspot.com/2011/04/nhibernate-in-combination-with_06.html
描述如何从数据库以外的资源(在本例中为 Web 服务)加载实体。这很好,但是如果我在一个查询中加载多个客户端,每个客户端都有不同的 MaritialState,它将不得不为每个客户端调用 web 服务。有没有办法预加载所有婚姻状态,所以它不必为每个客户来回访问网络服务?
我不认为 Hibernate 支持这一点。“n+1 选择问题”是一个众所周知的问题,Hibernate 有很多策略来处理它(批处理、子选择、急切获取等)。问题是您有“n+1 个网络服务调用”,所有这些机制都是无用的。Hibernate 根本不知道你在 IUserType 中做什么。它假定您转换已加载的数据。
看起来您将不得不实现自己的预加载。像这样的东西:
// TODO: not thread safe, lock or use ConcurrentDictionary
static IDictionary<Int32, ClientDto> _preLoadedClients
= new IDictionary<int,ClientDto>();
public Object NullSafeGet(IDataReader rs, String[] names, ...) {
Int32 clientid = NHibernateUtil.Int32.NullSafeGet(rs, names[0]);
// see if client has already been preloaded:
if(_preLoadedClients.ContainsKey(clientid)) {
return _preLoadedClients[clientid];
}
// load a batch: clientId + 1, client + 2, ... client + 100
var batchOfIds = Enumerable.Range(clientid, 100);
var clientsBatch = clientService.GetClientsByIds(batchOfIds);
_preLoadedClients.Add(clientsBatch);
return _preLoadedClients[clientid];
}