0

I am trying to use JSON patch in my .net core project to update data. I am using Table-per-type in my architecture for this particular feature.

So my models are like this.

I have a form model

public class HrFormV1
{
    public HrFormV1()
    {
        ...
        
    }

    public HrFormV1(HrFormV1DTO form)
    {
        ...
    }

    public string Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string OrganizationId { get; set; }
    public Organizations Organization { get; set; }

    public List<HrFormSectionV1> FormSections { get; set; }

    public DateTime Created { get; set; }
    public DateTime Updated { get; set; }
    public HrFormType Type { get; set; }
    public string CreatedById { get; set; }
    public User CreatedBy { get; set; }

    public static void DefineDbModel(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<HrFormV1>().Property(f => f.Id).ValueGeneratedOnAdd();
    }
}

Each form can have multiple sections.

public class HrFormSectionV1
{
    public string Id { get; set; }
    public int Index { get; set; }
    public string FormId { get; set; }
    public HrFormV1 Form { get; set; }

    public HrFormSectionV1()
    {
       ...
    }

    public HrFormSectionV1(HrFormSectionV1DTO section)
    {
        ...
        BuildingComponents = section.BuildingComponents.Select(b => @switch[b.GetType()](b)).ToList();
    }

    public ICollection<HrBuildingComponentsV1> BuildingComponents { get; set; }

    public static void DefineDbModel(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<HrFormSectionV1>(entity =>
        {
            entity.Property(e => e.Id).ValueGeneratedOnAdd();
        });
    }

    private static readonly Dictionary<Type, Func<HrBuildingComponentV1DTO,HrBuildingComponentsV1>> @switch = new Dictionary<Type, Func<HrBuildingComponentV1DTO,HrBuildingComponentsV1>> {
        { typeof(HrTextBuildingComponentV1DTO), b => new HrTextBuildingComponentV1((HrTextBuildingComponentV1DTO)b) },
    };
}

Each section can have multiple components. The thing is that components can be of multiple types like text, textarea etc.

So I have created an abstract component class like this

{
    public abstract class HrBuildingComponentsV1
    {
        public HrBuildingComponentsV1() { }
        public HrBuildingComponentsV1(HrBuildingComponentV1DTO buildingComponent)
        {
            Id = buildingComponent.Id;
            Label = buildingComponent.Label;
            Type = buildingComponent.Type;
            Index = buildingComponent.Index;
            IsFilter = buildingComponent.IsFilter;
            SectionId = buildingComponent.SectionId;
        }

        public string Id { get; set; }
        public string Label { get; set; }
        public BuildingComponentType Type { get; set; }
        public int? Index { get; set; }
        public bool IsFilter { get; set; }
        public string SectionId { get; set; }
        public HrFormSectionV1 Section { get; set; }
    }

    public enum BuildingComponentType
    {
        ...
    }
}

and then each component can inherit from that class, for example text

public class HrTextBuildingComponentV1 : HrBuildingComponentsV1
{
    public string Value { get; set; }

    public HrTextBuildingComponentV1() : base() { }
    public HrTextBuildingComponentV1(HrTextBuildingComponentV1DTO buildingComponentdto) 
        : base(buildingComponentdto) {
        Value = buildingComponentdto.Value;
    }

    public static void DefineDbModel(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<HrTextBuildingComponentV1>().ToTable("HrTextBuildingComponentV1");
    }
}

Now the issue is that when applying json patch I am not sure how to do it.

If I send this

[{
        "op": "add",
        "path": "/formsections/0/buildingcomponents/-",
        "value": {
            "sectionid": "section_id",
            "id": "ID",
            "type": 0,
            "label": "Text",
            "index": 0,
            "isFilter": false,
            "value":"Yoo"
        }
}]

I get error saying

The value '{ "sectionid": "section_id", "id": "ID", "type": 0,
"label": "Text", "index": 0, "isFilter": false, "value": "Yoo" }' is invalid for target location.

Any help would be highly appreciated.

mohsinali1317
  • 4,255
  • 9
  • 46
  • 85
  • Does it work if the base class it not abstract? What code are you using to do the actual patch? – Neil Jul 31 '22 at 09:53
  • Thanks for the comment. Tried removing abstract from base class, it did not helped. And I am using Microsoft.AspNetCore.JsonPatch to do patching. @Neil – mohsinali1317 Jul 31 '22 at 10:02
  • When I removed abstract and tried it it created an entry into the Components class instead of creating one for the child class (text in this case). @ne – mohsinali1317 Jul 31 '22 at 11:16
  • OK, so it's no longer erroring, which suggests that the abstract base class is not going to work. – Neil Jul 31 '22 at 11:48
  • that is true but now I am not sure which json format to send so that it corresponds to correct change? @Neil – mohsinali1317 Jul 31 '22 at 11:56

0 Answers0