简体   繁体   English

已知类型的AliasToBean DTO

[英]AliasToBean DTO with known type

All the examples I am finding for using the AliasToBean transformer use the sessions CreateSqlQuery method rather than the CreateQuery method. 我发现的所有使用AliasToBean转换器的示例都使用会话CreateSqlQuery方法而不是CreateQuery方法。 They also only return the basic value types, and not any object's of the existing mapped types. 它们还仅返回基本值类型,而不返回现有映射类型的任何对象。

I was hoping it would be possible that my DTO have a property of one of my mapped Domain objects, like below, but I am not getting traction. 我希望我的DTO可能具有我映射的Domain对象之一的属性,如下所示,但我没有受到关注。 I get the following exception: 我得到以下异常:

Could not find a setter for property '0' in class 'namespace.DtoClass' 在类'namespace.DtoClass'中找不到属性'0'的设置器

My select looks like the following on my mapped classes (I have confirmed the mappings pull correctly): 我的选择在映射的类上如下所示(我已确认映射正确提取):

SELECT 
fcs.MeasurementPoint, 
fcs.Form, 
fcs.MeasurementPoint.IsUnscheduled as ""IsVisitUnscheduled"", 
fcs.MultipleEntryAllowed
FROM FormCollectionSchedule fcs

My end query will be more complex, but I wanted to confirm if this AliasToBean method can return mapped domain objects as well as basic field values from tables retrieved via sql. 我的最终查询将更加复杂,但是我想确认此AliasToBean方法是否可以从通过sql检索的表中返回映射的域对象以及基本字段值。

the query execution looks like the following: 查询执行如下所示:

var result = session.CreateQuery(hqlQuery.ToString())
                .SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean(typeof (VisitFormCollectionResult)))
                .List<VisitFormCollectionResult>();

note: the VisitFormCollectionResult DTO has more properties, but I wanted to know if I could populate the domain object properties matching the names 注意:VisitFormCollectionResult DTO具有更多属性,但是我想知道是否可以填充与名称匹配的域对象属性

update found my problem! 更新发现我的问题! I have to explicitly alias each of the fields. 我必须显式地别名每个字段。 once I added an alias, even though the member property on the class matched my DTO's property name, the hydration of the object worked correctly. 一旦添加了别名,即使该类上的member属性与我的DTO的属性名称匹配,该对象的水合也能正常工作。

The answer to my own question was that each of the individual fields in the select needed an explicit alias matching the property, regardless if the field name already matched the property name of the DTO object: 我自己的问题的答案是,无论字段名称是否已经与DTO对象的属性名称匹配,select中的每个单独字段都需要一个与属性匹配的显式别名:

SELECT 
fcs.MeasurementPoint as "MeasurementPoint", 
fcs.Form as "Form", 
fcs.MeasurementPoint.IsUnscheduled as "IsVisitUnscheduled", 
fcs.MultipleEntryAllowed as "MultipleEntryAllowed"
FROM FormCollectionSchedule fcs

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

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