简体   繁体   English

使用nHibernate将未映射的数据转换为DTO

[英]using nHibernate to cast unmapped data into a DTO

So I'm doing a postcode lookup provided by an outside data provider on a database that we're controlling with nHibernate. 所以我正在使用外部数据提供程序在我们用nHibernate控制的数据库上进行邮政编码查找。 This involves calling a stored procedure and supplying a postcode. 这涉及调用存储过程并提供邮政编码。 In return I get a number of rows each one of which contains multiple columns that make up the parts of the address. 作为回报,我得到了许多行,每行包含构成地址部分的多个列。

We have an address DTO. 我们有一个地址DTO。 But I'm struggling with how to cast the DB results into this object since it's not mapped to anything in the database. 但我正在努力解决如何将DB结果转换为此对象,因为它没有映射到数据库中的任何内容。 I'm doing this: 我这样做:

    Dim spCall As String = String.Format("exec AddressFindListPostCode '{0}'", postcode)
    Dim tran As Global.NHibernate.Transform.IResultTransformer = Global.NHibernate.Transform.Transformers.AliasToBean(GetType(Concrete.Cms.DataTransferObjects.Address))
    Dim streetList As IList(Of Concrete.Cms.DataTransferObjects.Address) = session.CreateSQLQuery(spCall).SetResultTransformer(tran).List(Of Concrete.Cms.DataTransferObjects.Address)()

But of course it can't transform the result set into an object without some help from a mapping of some kind. 但是,如果没有某种映射的帮助,它当然不能将结果集转换为对象。

The problem, essentially, is that the SP returns a List of objects. 问题本质上是SP返回一个对象列表。 Each object (equivalent to a row) contains sub-objects which correspond to columns within the row. 每个对象(相当于一行)包含对应于行内列的子对象。 But I see no way of getting the sub-objects. 但我认为无法获得子对象。 streetList(i, j) won't work and there are no methods or properties on streetList that allow me to access them. streetList(i,j)不起作用,streetList上没有允许我访问它们的方法或属性。

How do I get my data out to map it? 如何获取数据以进行映射?

Cheers, Matt 干杯,马特

You could try bulk loading the data into a bulk class, like: 您可以尝试将数据批量加载到批量类中,例如:

 Dim result As IList(Of BulkLoadAddressList) = session.CreateSQLQuery(spCall)
    .SetResultTransformer(Transformers.AliasToBean(typeof(BulkLoadAddressList)))
    .List(Of BulkLoadAddressList)()

More: NHibernate Ad-hoc mapping 更多: NHibernate Ad-hoc映射

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

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