从asp.net核心到Angular的过程中丢失继承类型

时间:2019-01-23 00:34:15

标签: json angular asp.net-core-2.2 ef-core-2.2

我正在使用服务器上的asp.net core 2.2和ef core 2.2以及客户端的Angular 7开发应用程序。我无法弄清楚:

我有以下(简化的)模型:

public abstract class LegalEntity
{
    public int Id { get; set; }
    public int Name { get; set; }
    public Address FiscalAddress { get; set; }
}

public class Organisation : LegalEntity
{
    public Organisation(){}
    public Organisation(LegalEntityType legalEntityType, string name, Address fiscalAddress)
    {
        LegalEntityType = legalEntityType;
        Name = name;
    }
    public LegalEntityType LegalEntityType { get; set; }
}


public class LegalEntityType
{
    public int Id { get; set; }
    public int Name { get; set; }
    public LegalEntityType(){}
    public LegalEntityType(string name)
    {
        Name = name;
    }
}


public class Person : LegalEntity
{
    public Gender Gender { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public override string Name => string.Format("{0} {1}", FirstName, LastName);
}

public class Gender
{
    public int Id { get; set; }
    public int Name { get; set; }
}

public class Customer
{
    public int Id { get; set; }
    public Customer(){}
    public Customer(LegalEntity legalEntity)
    {
        LegalEntity = legalEntity;
    }
    public LegalEntity LegalEntity { get; set; }
}

当我通过API将客户实体返回给客户时,有时LegalEntity是一个组织,有时是一个人。在返回哪种类型(组织或个人)之后,我希望将属性LegalEntityType(对于组织而言)或属性Gender(对于Person而言)呈现为JSON代码。这是我的第一个问题,以下内容使它们都为空:

.Include(o => o.Customer).ThenInclude(o => o.LegalEntity)

,因为这不会加载仅在继承实体中存在的导航属性。这是

情况下JSON字符串的摘录

人员:

...
 "customer": {
            "legalEntity": {
                "gender": null,
                "firstName": "Denis",
                "lastName": "Testmann",
                "name": "Denis Testmann",
                "id": 9
            },
...

组织:

...
 "customer": {
            "legalEntity": {
                "legalEntityType": null,
                "name": "Companyname GmbH",
                "id": 6
            },
...

应显示以下内容:

人员:

...
 "customer": {
            "Person": {
                "gender": null,
                "firstName": "Denis",
                "lastName": "Testmann",
                "name": "Denis Testmann",
                "id": 9
            },
...

组织:

...
 "customer": {
            "Organisation": {
                "legalEntityType": null,
                "name": "Companyname GmbH",
                "id": 6
            },
...

要指出的是:客户可能是个人或组织,实体(组织和个人)都继承自LegalEntity,因此客户财产“ LegalEntity”有时是个人,有时是组织。当我渲染JSON时,必须保持特定的类型。

希望我已经足够清楚了-请原谅我这么久,我想确定问题已得到理解。

2 个答案:

答案 0 :(得分:0)

新答案

我通过dotnet core 2.1通过dotnet new console整理了以下Program.cs:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

class Program
{
    static void Main(string[] args)
    {
        var list = new List<Customer>();
        list.Add(new Customer(new Person { Gender = new Gender{ Name = "Male"}}));
        list.Add(new Customer(new Organisation { LegalEntityType = new LegalEntityType{ Name = "GmbH"}}));
        Console.WriteLine(JsonConvert.SerializeObject(list, Newtonsoft.Json.Formatting.Indented));
    }
}

public abstract class LegalEntity
{
   public int Id { get; set; }
   public virtual string Name { get; set; }
   public Address FiscalAddress { get; set; }
}

public class Organisation : LegalEntity
{
   public Organisation(){}
   public Organisation(LegalEntityType legalEntityType, string name, Address fiscalAddress)
   {
       LegalEntityType = legalEntityType;
       Name = name;
   }
   public LegalEntityType LegalEntityType { get; set; }
}


public class LegalEntityType
{
   public int Id { get; set; }
   public string Name { get; set; }
   public LegalEntityType(){}
   public LegalEntityType(string name)
   {
       Name = name;
   }
}

public class Person : LegalEntity
{
   public Gender Gender { get; set; }
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public override string Name => string.Format("{0} {1}", FirstName, LastName);
}

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


public class Customer
{
   public int Id { get; set; }
   public Customer(){}
   public Customer(LegalEntity legalEntity)
   {
       LegalEntity = legalEntity;
   }
   public LegalEntity LegalEntity { get; set; }
}    

public class Address
{
   public int Id { get; set; }
   public string Line1 { get; set; }
   public string Line2 { get; set; }   
}

输出显示如下:

[
  {
    "Id": 0,
    "LegalEntity": {
      "Gender": {
        "Id": 0,
        "Name": "Male"
      },
      "FirstName": null,
      "LastName": null,
      "Name": " ",
      "Id": 0,
      "FiscalAddress": null
    }
  },
  {
    "Id": 0,
    "LegalEntity": {
      "LegalEntityType": {
        "Id": 0,
        "Name": "GmbH"
      },
      "Id": 0,
      "Name": null,
      "FiscalAddress": null
    }
  }
]

这看起来还好吗?也许请检查您的API端点绑定程序正在使用哪些序列化程序设置。

旧答案

如果LegalEntity是数据库中的真实表,则您应该可以使用:

    [ForeignKey("LegalEntity")]
    public int LegalEntityId { get; set; }

在您的Customer定义中。

您还需要设置密钥:

    public abstract class LegalEntity
    {
        [Key]
        public int Id { get; set; }
        public int Name { get; set; }
        public Address FiscalAddress { get; set; }
    }

如果LegalEntity不是真实的表格,则为“人员/组织”添加单独的导航

答案 1 :(得分:0)

好-找到解决方案了!

在服务器端,您必须使用

序列化JSON内容

SerializerSettings.TypeNameHandling = TypeNameHandling.Auto

您应该在Startup.cs文件中做什么:

    services.AddJsonOptions(opt =>
    {
        opt.SerializerSettings.ReferenceLoopHandling =
            Newtonsoft.Json.ReferenceLoopHandling.Ignore;
        opt.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;
    })

,在客户端,您可以使用类转换器(https://github.com/typestack/class-transformer)。

在我的示例中,客户类别-LegalEntity可能是Person或Organization类型的外观如下:

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

    @Type( () => Object, {
        discriminator: {
          property: '$type',
          subTypes: [
            {value: Person, name: 'Your.Name.Space.Person, YourApp.Name'},
            {value: Organisation, name: 'Your.Name.Space.Organisation, YourApp.Name'},
          ],
        },
      })
       public LegalEntity LegalEntity { get; set; }
   }

然后从像这样的普通javascript对象构建customer类的实例:

    import { plainToClass } from 'class-transformer';

    export class SomeClassInYourAngularApp implements OnInit {

customerList: Customer[];

      ngOnInit() {
        this.customerList = new Array<Customer>();
        let u: any;

    // lets get the plain js object form somewhere, ex. from a resolver
        this.route.data.subscribe(data => {
          u = data['customerList'].result;
        });

        u = plainToClass(Customer, u);
        Object.assign(this.customerList, u);
      }

就是这样!