How do I add properties to a Web API Response that

2019-03-01 08:13发布

问题:

I am building a C# Web API using Entity Framework 6.0. I have the simplest User Class with 3 properties that I persist on SQL into a User Table with 3 corresponding columns where UserID is its the Primary Key.

public partial class User
{
    public string UserID {get; set;}
    public string FirstName {get; set;}
    public string LastName {get; set;}
}

I want to add to the Web API two output-only properties on the fly that I do not care to store in my DB. I use these properties to communicate to the consuming client "Status" and "Message" information that are not part of the User Class. Status = OK|Error|Warning. Message would be any message the Web API needs to communicate back to the calling client.

My question is: what is the simplest way to add these two properties on the fly upon sending back the Web API's response WITHOUT modifying the underlying User Table on SQL? I know I can add these two as dummy columns to the User Table. I don't want to carry around that overhead on the SQL side when I don't need it there.

回答1:

I would go with more generic approach:

public class MyResponse<T>
{
    public T Data {get;set;}
    public Status ResponseStatus{get;set;}
    public string Message{get;set;}
}

This way you can handle all you models/data in the same way.

UPDATE

[AllowAnonymous]
[RoutePrefix("api/home")]
public class HomeController : ApiController
{
    [HttpGet]
    [Route("ok")]
    public MyResponse<MyUser> OK()
    {
        MyUser m = new MyUser();
        var r = MyResponse<MyUser>.Success(m);
        return r;
    }

    [Route("nok")]
    [HttpGet]
    public MyResponse<MyUser> NOK()
    {
        var r = MyResponse<MyUser>.Error("something went terribly wrong");
        return r;
    }
}

public class MyResponse<T>
{
    public T Data { get; set; }
    public Status ResponseStatus { get; set; }
    public string Message { get; set; }

    private MyResponse() { }

    public static MyResponse<T> Success(T data)
    {
        return new MyResponse<T> { Data = data, ResponseStatus = Status.Success };
    }

    public static MyResponse<T> Error(string message)
    {
        return new MyResponse<T> { ResponseStatus = Status.Error, Message = message };
    }
}

public class MyUser
{
    public int Id { get; set; }
    public string Name { get; set; }
}


public enum Status
{
    Unknown = 0,
    Success = 1,
    Error
}


回答2:

Ideally you should create a data transfer object (DTO) class in model with all the properties you require, then use a mapper to map your user to the DTO

public class UserDto{
   public string UserID {get; set;}
   public string FirstName {get; set;}
   public string LastName {get; set;}

   public string Message {get; set;}
   public string Status {get; set;}
}

Then in your action

[ResponseType(typeof(UserDto))]
public IHttpActionResult User(string userId){

   // retrive user from db

   var userDto = Mapper.Map<UserDto>(dbUser);

   if(condition){
      userDto.Message = "the message";
      userDto.Status = "the status";
   }

   return Ok(userDto);
}

Then you can install Automapper to from nuget and configure it to do the mapping for you.