Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

asp.net - Serialize C# object to JSON with constraint : Get old value if constraint is true

In my WEB API .NET core application, i have a route like that :

[Httput("{id}")]
api/task/{id}/content

The model "content" is C# class model with simple properties, but also with List or nested objects.

For exemple :

public class Content
{
    public string Reason{ get; set; }
    public string Title{ get; set; }
    public Description Description { get; set; }
    public IEnumerable<Contact> Contacts{ get; set; }
}

I save this model in JSON format in my database ( because all this appears in a generic system).

Between controller and this JSON column in database, we do some treatment.

It's an Angular application which calls this API, the application can be in different working state, let's name it "STATE1", "STATE2", ...

when the angular application calls the api, I know what state context we are in. What I want is:

I want to put a constraint on each property, which says "this property can only be updated in state X", we can secure on the angular side, but it is necessary to secure direct calls (like via postman)

if this property cannot be saved, i want to recover the property content of the previous object.

imagine something like this : ( i don't want this code, its just for understand the question )

    Something contentFromAngular = xx;
    Something currentContentFromDataBase = xx;
    if(STATE==STATE1)
    {
       contentFromAngular.Reason=currentContentFromDataBase.Reason
    }
    else If(STATE==STATE2)
    {
       contentFromAngular.Title=currentContentFromDataBase.PropA
       contentFromAngular.PropB=currentContentFromDataBase.PropB
    }
    ...
    SaveNewContent(JsonConvert.SerializeObject(contentFromAngular)); //Save json in database after remove property "unauthorized"

I don't want a code like that, because if i have for example 20 STATE+ with dozens of properties, it's difficult to maintain.

i want something a little more generic.

My first idea its using on each of the properties custom attributes like :

   public class Content
{
    [UnAuthorizedState("STATE1")]
    public string Reason{ get; set; }
    [UnAuthorizedState("STATE2")]
    public string Title{ get; set; }
    public Description Description { get; set; }
    public IEnumerable<Contact> Contacts{ get; set; }
}
public class Contact
{
    [AuthorizedState("STATE5")]
    public string ID { get; set; }
}

But its very hard to set old value in reflection, because attribute is set on each sub class and not only on attribute on the "parent class"

So, i need your help for a solution.

Thank you

question from:https://stackoverflow.com/questions/66051745/serialize-c-sharp-object-to-json-with-constraint-get-old-value-if-constraint-i

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...