简体   繁体   English

NHibernate - 执行SQL来填充DTO

[英]NHibernate - Execute SQL to populate DTO

I have some instances for reporting where executing sprocs is easier and simpler than complicated QueryOver statements. 我有一些实例报告执行sprocs比复杂的QueryOver语句更容易和简单。

I have a DTO, not an entity, that represents the data returned from the query and want to populate the results of the query into the DTO. 我有一个DTO,而不是一个实体,它代表从查询返回的数据,并希望将查询结果填充到DTO中。 I am using named queries and session.GetNamedQuery() to execute the query. 我使用命名查询和session.GetNamedQuery()来执行查询。

  1. Do I have to create mapping files for the DTO? 我是否必须为DTO创建映射文件?
  2. If so, is is possible to let NHibernate/FluentNHibernate know that it should not create tables for the DTO? 如果是这样,是否可以让NHibernate / FluentNHibernate知道它不应该为DTO创建表? My units tests drop and create the schema using NH's SchemaExport tool and don't want to create tables for the DTO's 我的单位测试使用NH的SchemaExport工具删除并创建模式,并且不想为DTO创建表格

Please note I don't want to project a QueryOver/Linq query using Projections and AliasToBean - I need to execute the stored procedure. 请注意我不想使用Projections和AliasToBean投影QueryOver / Linq查询 - 我需要执行存储过程。

Cheers 干杯

With CreateSQLQuery, the following would work without any mapping file. 使用CreateSQLQuery,以下将在没有任何映射文件的情况下工作。 Maybe you can give it a try with named queries : 也许您可以尝试使用命名查询:

public class YourDto
{
    public int YourDtoId { get; set; }
    public string YourDtoTitle { get; set; }
}

then 然后

var result = yourNhSession
    .CreateSQLQuery("select yourColumn1 as YourDtoId, yourColumn2 as YourDtoTitle from YOUR_TABLE")
    .SetResultTransformer(Transformers.AliasToBean<YourDto>())
    .List<YourDto>();

If you want the simplest solution I suggest you add in your architecture a micro / orm like Dapper to do this. 如果你想要最简单的解决方案,我建议你在你的架构中添加像Dapper这样的微/ orm来做到这一点。 It is just dropping a single file in your project and follow the sample. 它只是删除项目中的单个文件并按照示例进行操作。 In order to me it is the best solution to pair to NH when you have to do the thinghs that are for some reason out of the entity logic . 为了对我来说,当你必须从实体逻辑中做某些事情时,它是与NH配对的最佳解决方案。

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

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