简体   繁体   English

使用Nhibernate使用相同的存储库方法返回不同的Dto

[英]Return different Dto using same repository method with Nhibernate

I have a Web API that request a GET method that can return based in the same parameters different results (Dtos) example: 我有一个Web API,它请求一个GET方法,该方法可以根据相同的参数返回不同的结果(Dtos)示例:

GET - http://localhost/books/searchAll?Detailed=true `
Returns DTO 1 ["Id":1,"Date": 01/01/2014]

GET - http://localhost/books/searchAll?Detailed=false `
Returns DTO 2 ["Id":1]

What is the best approach to deal with this situation : 解决这种情况的最佳方法是什么:

Should I create a http response with one Dto that includes two different list of results : 我是否应该使用一个Dto创建一个包含两个不同结果列表的http响应:

Public Class ResultList
{
   Public List<Dto> DtoList1{get;set;}
   Public List<Dto2> DtoList2{get;set;}
}

Or with just one dto list that return a dynamic list 或仅具有一个返回动态列表的dto列表

Public Class ResultList
{
  Public List<dynamic> DtoList{get;set;}
}

Or other solution ? 还是其他解决方案?

Regards 问候

If you are returning two different resources (or classes or types) I would definitely go for having two different methods but also they would serve two different URLs, let's say for eg. 如果您要返回两个不同的资源(或类或类型),那么我肯定会使用两个不同的方法,但是它们将提供两个不同的URL,例如。 Search and searchDetail. 搜索和searchDetail。

If you re returning the same resource but one time full and the other partially then I would have one method, use the detail parameter you suggested but I would return the same dto class sometimes with all it's attributes wit values and others only with its minimum attributes with value. 如果您返回相同的资源,但一次又一次,而另一部分则是全部,那么我将有一个方法,使用您建议的detail参数,但有时会返回相同的dto类,其中包含所有属性wit值,而其他则仅返回其最小属性具有价值。 After all you are returning a json object which can have dynamic attributes without any problem. 毕竟,您将返回一个JSON对象,该对象可以具有动态属性而没有任何问题。

That's what I think. 那就是我所想的。

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

相关问题 在NHibernate中为两个不同的存储库使用相同的模型 - Using the same model for two different repository in NHibernate 为相同的方法返回不同的类型 - Return different types for same method Nhibernate Projection Query DTO,使用方法代替属性 - Nhibernate Projection Query DTO, use method in stead of property 执行UPDATE并返回使用NHibernate CreateSQLQuery方法刚刚更新的记录? - Execute UPDATE and return the record just updated using NHibernate CreateSQLQuery method? 在NHibernate中使用Merge()时出错:具有相同标识符的另一个对象 - Error when using Merge() in NHibernate: a different object with the same identifier 使用相同类型的对象(NHibernate,C#)访问不同的表 - Access different tables using the same type of object (NHibernate, C#) 基类的相同方法的不同返回类型 - Different return type for same method of a base class 名称和参数相同但返回类型不同的方法 - Method with same name and parameter but different return type 在某些情况下,我可以从存储库中返回DTO吗? - Can I return DTO from my repository in some cases? 存储库层是否应该返回数据传输对象(DTO)? - Should the repository layer return data-transfer-objects (DTO)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM