我已经设置了一个 WCF Web Api 服务,并且一切正常,直到我开始使用 DTO 来公开数据。
以前我在 WCF 服务上有我的模型对象,它被称为 Game.cs:
public class Game
{
public int Id { get; set; }
public string Description { get; set; }
public string Developer { get; set; }
public Genre Genre { get; set; }
public string Name { get; set; }
public decimal? Price { get; set; }
public string Publisher { get; set; }
public Rating Rating { get; set; }
public DateTime? ReleaseDate { get; set; }
}
服务中的 get 方法如下所示:
public IQueryable<Game> GetGames()
{
var db = new XBoxGames();
var games = db
.Games
.Include("Genre")
.Include("Rating")
.OrderBy(g => g.Id)
.ToList()
.AsQueryable();
return games;
}
在 MVC 3 客户端应用程序中,我有一个请求我所有游戏的控制器操作:(我正在使用 restsharp)
public ActionResult Index()
{
var request = new RestRequest(Method.GET);
var restClient = new RestClient();
restClient.BaseUrl = "http://localhost:4778";
request.Resource = "games";
var response = restClient.Execute<List<Game>>(request);
var games = response.Data;
return View(games);
}
和客户端模型:
public class Game
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Developer { get; set; }
public int GenreId { get; set; }
}
在我开始使用 DTO 后,客户端没有获得任何游戏的任何信息,尽管服务正在返回信息。服务中的 get 方法发生了一些变化,现在我没有返回我的模型,而是返回了一个 DTO 对象,该对象具有我真正想要通过 API 公开的信息:
[WebGet(UriTemplate = "")]
public IQueryable<GameDTO> GetGames()
{
var db = new XBoxGames();
var games = db
.Games
.Include("Genre")
.Include("Rating")
.OrderBy(g => g.Id)
.ToList();
var gamesDTO = Mapper.Map<List<Game>, List<GameDTO>>(games);
return gamesDTO.AsQueryable();
}
DTO 对象具有以下结构:
public class GameDTO
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Developer { get; set; }
public string GenreName { get; set; }
}
服务返回的 xml 如下所示:
<ArrayOfGameDTO xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<GameDTO>
<Id>3</Id>
<Name>Jade Empire™</Name>
<Description>
...
</Description>
<Developer>BioWare Corp.eeeddd</Developer>
</GameDTO>
<GameDTO
.
.
.
</GameDTO>
</ArrayOfGameDTO>
我注意到 xml 根标记现在已从 ArrayOfGame 更改为 ArrayOfGameDTO,这似乎是 restsharp 的问题,因为在我的客户端应用程序中,我的游戏模型称为 Game.cs,所以为了让客户端应用程序工作我的客户端模型需要与服务 (GameDTO) 中的 de DTO 对象具有相同的名称。我发现这个解决方案有点奇怪,所以我的问题是:有没有办法在没有 DTO 和客户端模型命名相同的情况下让事情正常工作?
任何帮助将不胜感激...在此先感谢。