WebApi用于获取和发布的不同DTO [英] WebApi different DTO for Get and Post

查看:42
本文介绍了WebApi用于获取和发布的不同DTO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在GET和POST操作中使用不同的DTO吗?

Is it ok to have different DTO's for GET and POST actions ?

原因是这两个数据模型之间通常存在巨大差异.

The reason for this is there is usually a vast difference between these two data models.

例如:

我的POST看起来像这样:

My POST would look like this:

/// <summary>
/// Add new user
/// </summary>
/// <param name="item">User data</param>
/// <returns>Newly added user id</returns>
[HttpPost]
public IHttpActionResult Post([FromBody] UserDto item)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    var model = _mapper.Map<User>(item);

    int itemid = _usersRepository.Insert(model);

    return Ok(itemid);
}

public class UserDto
{
    private string _password;

    [Required]
    [StringLength(100, ErrorMessage = "Name {0} must consist of at least {2} letters.", MinimumLength = 6)]
    public string Name { get; set; }

    [Required]
    public string ExternalName { get; set; }

    [Required]
    public bool Active { get; set; }

    [Required]
    public string Password
    {
        get { return _password; }
        set { _password = value.Hash(); }
    }
}

我的GET看起来像这样:

and my GET would look like this:

/// <summary>
/// Get all users
/// </summary>
/// <returns>Users list</returns>
[HttpGet]
[ResponseType(typeof(List<UserInfoDto>))]
public IHttpActionResult Get()
{
    IList<UserInfoDto> values = _usersRepository.SelectAll();

    if (values == null || !values.Any())
        return Ok();

    return Json(new { collection = values });
}

public class UserInfoDto
{
    public int Id { get; set; }

    public string Name { get; set; }

    public string ExternalName { get; set; }

    public bool Active { get; set; }
}

之所以这样做,是因为在发布资源时不需要ID,但是需要密码.使用GET时相反.

The reason I'd do this is that I don't need Id when POSTing a resource but Password is necessary. It is reversed when using GET.

使用WebApi(为响应和请求创建不同的Dto)时,这是正确的方法吗?还是有其他方法可以做到这一点?

Is this the correct approach when using WebApi (Creating different Dtos for responses and requests)? Or is there some other way to do this ?

推荐答案

可以在GET和POST操作中使用不同的DTO吗?

Is it ok to have different DTO's for GET and POST actions ?

针对不同的动作使用不同的dto并没有错.

There is nothing wrong with having different dto's for different actions.

如果该API被第三方使用,则需要确保该文档已被正确记录.

If the api is being used by 3rd parties you would want to make sure that it is well documented.

使用WebApi时这是正确的方法吗?

Is this the correct approach when using WebApi?

这是正确的方法还是见仁见智.公开或接受必要的信息,以使系统按预期运行.

Whether this is correct approach or not is a matter of opinion. Expose or accept the necessary information for the system to perform as intended.

每个动作都可以使用自己的唯一dto.并不意味着它应该.您想确保自己不会泄漏比必要更多的信息.

Each action could use its own unique dto. Doesn't mean that it should. You want to make sure you are not leaking more information than is necessary.

这篇关于WebApi用于获取和发布的不同DTO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆