return nhibernate entity from web api?

2019-08-29 10:25发布

Is it a good idea to return nHibernate Entity from WebApi? For simple entity I tried to use this but what about complex entities. How will lazy loading be handled in that case?

1条回答
放我归山
2楼-- · 2019-08-29 11:06

It is possible to return complex entities via WebAPI, however it is not the best way to do it, what I would suggest is create a DTO object to match the client side operation map it to the NHibernate entity and send it over the API.

With regard to the lazy loading, it will have no use if you expose the object via the web API, as the object serializes all the properties of the entity will be accessed and they will be loaded from the persistence, so in fact using lazy loading is inefficient in this scenario.

查看更多
登录 后发表回答