How do I add properties to a Web API Response that I do not store in my DB?

asp.net-web-api asp.net-web-api2 c# entity-framework entity-framework-6

Question

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
1
3/27/2016 10:13:31 PM

Accepted Answer

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
}
1
3/28/2016 9:14:57 AM

Popular Answer

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.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow