发送 limited/reduced class 到前端

Send a limited/reduced class to frontend

我想要的

我想发送 limited/reduced class/object 到前端(如 JSON)。我使用 .NET Core 5。

我有什么

我有一个这样的模型class:

namespace Tasks.Models
{
    public class Resources
    {
        public Guid Id { get; set; }
        public string Type { get; set; }
        public string Url { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public DateTime? Createdon { get; set; }
        public Guid Userid { get; set; }
        public Guid Taskid { get; set; }
        public int Clicked { get; set; }
        public byte Active { get; set; }
        +++ many more properties
    }
}

现在,根据调用此模型的控制器,我希望拥有不同“种类”的模型。因此,如果资源是文件,我可能需要属性 Id、Type、Name。但是如果资源是URL我要Id,Url,Name.

我尝试设置一个方法来“初始化我想要的字段,但也返回了所有属性

public static Responses FileResponse()
{
    var response = new Responses()
    {
        Id = new Guid(),
        Name = "",
        Type = "File",
    };

    return response;
}

现在,当我调用 Resources class 或此方法时,我获得了所有属性,并将其返回给视图显示所有属性,但大部分为 null,因为我只在方法中设置了三个字段.

推荐的解决方法是什么?

It is just limiting the Resource class I am not able to do

是的,C# 的副作用是强类型,对象 X 肯定具有属性 Y 和 Z。您需要 不同形状的 对象 - classes或记录 - 命名减少的属性集,因为序列化程序将查找对象并在它可以找到的每个 属性 中进行服务。

您可以为每个变体制作一个新的 class - 使用记录快速简便,并且易于在您的 C# 中传递:

public record FileThing(string Id, string Type, string Name);

//make a new one and return it
new FileThing(someResources.Id, someResources.Type, someResources.Name);

或者可以考虑使用匿名类型,如果你确实希望将一些属性放入某些 json,通过套接字连接到消费前端(我不能完全确定你的意思“视图”——它似乎不是一个 MVC 视图),它只关心许多

中的一些道具

So if the resource is file I maybe want the properties Id,Type,Name. But if the resource is URL I want Id, Url, Name.

public ActionResult SomeControllerMethod(){

  if(isFile)
    return Ok(new { someResources.Id, someResources.Type, someResources.Name });
  else if(isUrl)
    return Ok(new { someResources.Id, someResources.Url, someResources.Name });

}

匿名类型有点难用,因为编译器会为你编写 class,所以如果方法是 [=27],那么从方法中声明 return 类型是很棘手的=]ing 一个 AT.. 但是如果你在一个方法中使用它作为一些填充,比如“制作这个并序列化它”,它们工作得很好..

如果字段为 null 而不是在 json 中显示空值时要删除该字段。

    public class Resources
    {
        public Guid Id { get; set; }
        public string Type { get; set; }
        // if null, dont show it in JSON output
        [JsonIgnoreAttribute(Condition = JsonIgnoreCondition.WhenWritingNull)]
        public string Url { get; set; }
        // if null, dont show it in JSON output
        [JsonIgnoreAttribute(Condition = JsonIgnoreCondition.WhenWritingNull)]
        public string Name { get; set; }
        public string Description { get; set; }
        public DateTime? Createdon { get; set; }
        public Guid Userid { get; set; }
        public Guid Taskid { get; set; }
        public int Clicked { get; set; }
        public byte Active { get; set; }
    }

PS: Fiddle https://dotnetfiddle.net/hiMAci

我认为您的方法在这里不正确。在这种情况下,我倾向于遵循更通用的 OO 指南(注意,有些人认为这些有点过时,并且存在其他解决方案。但它们仍然很常用)

您针对接口编写。那么让我们看看您想要什么...一个 guid、类型和名称。所有其他细节都不重要。

    public interface IResourceDetails
    {
        public Guid Id { get; }
        public string Name { get; }
        public string Type { get;  }
    }

并且您可以拥有多个这样的接口。 然后,您可以按类型实现接口。但我可能会将它们组合成一个基础 class

    public abstract class ResourceBase : IResourceDetails
    {
        public Guid Id { get; } = new ();

        public string Name { get; init; }

        public string Type { get; }

        public ResourceBase(string type)
        {
            Type = type;
        }
    }

每种资源类型都有自己的实现

    public class FileResource : ResourceBase
    {
        public FileResource() : base("File") { }

        // File-specific properties.
        public string Description { get; init; }
        public DateTime? Createdon { get; init; }
    }

然后可以将响应方法设为通用的,如下所示

        public static IActionResult Response(IResourceDetails resource)
        {
            return Ok(new
            {
                resource.Id,
                resource.Name,
                resource.Type,
            });
        }