0

我已经设置了一个 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™&lt;/Name>
    <Description>
     ...
    </Description>
    <Developer>BioWare Corp.eeeddd</Developer>
 </GameDTO>
 <GameDTO
  .
  .
  . 
 </GameDTO>
</ArrayOfGameDTO>

我注意到 xml 根标记现在已从 ArrayOfGame 更改为 ArrayOfGameDTO,这似乎是 restsharp 的问题,因为在我的客户端应用程序中,我的游戏模型称为 Game.cs,所以为了让客户端应用程序工作我的客户端模型需要与服务 (GameDTO) 中的 de DTO 对象具有相同的名称。我发现这个解决方案有点奇怪,所以我的问题是:有没有办法在没有 DTO 和客户端模型命名相同的情况下让事情正常工作?

任何帮助将不胜感激...在此先感谢。

4

1 回答 1

1

您可以尝试执行以下操作

namespace MyDTONamespace {
  public class Game { ... }
}

并为您服务:

using DTO = MyDTONamespace;

namespace MyServiceNamespace {
  public class MyService {
    [WebGet(UriTemplate = "")]
    public IQueryable<DTO.Game> 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<DTO.Game>>(games);
      return gamesDTO.AsQueryable();
    }
  }
}

我很好奇这个序列化的内容,但我认为这可以工作。

编辑:我以为你在客户端上将它们重命名为 DTO。

我能想到的另一个选择是让你的 DTO 序列化感知。我假设您使用的是DataContractSerializer(WCF 默认),因此您可以使用该Name属性的DataContract属性,请参见此处

于 2011-10-17T20:36:55.317 回答