如何向未存储在数据库中的 Web API 响应添加属性? [英] How do I add properties to a Web API Response that I do not store in my DB?

查看:22
本文介绍了如何向未存储在数据库中的 Web API 响应添加属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Entity Framework 6.0 构建一个 C# Web API.我有一个最简单的用户类,它有 3 个属性,我在 SQL 上坚持到一个用户表中,其中用户 ID 是它的主键.

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;}
}

我想动态地向 Web API 添加两个仅输出属性,我不想将它们存储在我的数据库中.我使用这些属性与不属于用户类的消费客户端状态"和消息"信息进行通信.状态 = 正常|错误|警告.消息可以是 Web API 需要与调用客户端进行通信的任何消息.

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.

我的问题是:在发送回 Web API 的响应而不修改 SQL 上的基础用户表时,动态添加这两个属性的最简单方法是什么?我知道我可以将这两个作为虚拟列添加到用户表中.当我不需要它时,我不想在 SQL 端进行开销.

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.

推荐答案

我会采用更通用的方法:

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.

更新

[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
}

这篇关于如何向未存储在数据库中的 Web API 响应添加属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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