简体   繁体   English

使用Webservice中的值预加载IUserType

[英]Preloading IUserType with values from webservice

This post: 这个帖子:

http://kennytordeur.blogspot.com/2011/04/nhibernate-in-combination-with_06.html http://kennytordeur.blogspot.com/2011/04/nhibernate-in-combination-with_06.html

Describes how to load an entity from a resource other than a database, in this case a webservice. 描述如何从数据库以外的资源(在本例中为Web服务)加载实体。 This is great, but if I load a number of clients in one query, each with a different MaritialState, it will have to call the webservice for each Client. 很好,但是如果我在一个查询中加载多个客户端,每个客户端具有不同的MaritialState,则它将必须为每个客户端调用Web服务。 Is there a way to preload all marital states, so it doesn't have to go back and forth to the webservice for each client? 有没有一种方法可以预加载所有婚姻状态,因此不必为每个客户端来回访问网络服务?

I don't think Hibernate supports this. 我不认为Hibernate支持这一点。 'n+1 select problem' is a well know issue and Hibernate has quite a few strategies for dealing with it (batches, subselects, eager fetching etc). “ n + 1选择问题”是一个众所周知的问题,并且Hibernate有很多处理它的策略(批处理,子选择,渴望获取等)。 The problem is you have 'n+1 web service call' and all these mechanisms are useless. 问题是您有“ n + 1个Web服务调用”,所有这些机制都没有用。 Hibernate simply does not know about what you are doing in IUserType. Hibernate根本不知道您在IUserType中正在做什么。 It assumes that you converting already loaded data. 它假定您转换的是已加载的数据。

It looks like you will have to implement your own preloading. 看来您将必须实现自己的预加载。 Something like this: 像这样:

// 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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM