Home > Software engineering >  How to create a class for JObject
How to create a class for JObject

Time:05-14

I want to create a separate class instead of using JObject.

dynamic container = new JObject();
container.Quantity = checkinDetail.Quantity;
container.QuantityUm = checkinDetail.QuantityUm;

dynamic checkinDetail = new JObject();
checkinDetail.DispositionCode = DispositionCode;
checkinDetail.container = new JArray(container);

dynamic checkinObj = new JObject();
checkinObj.CheckinDetailEntities = new JArray(checkinDetail);
checkinObj.InternalReceiptNum = internalReceiptNum;
checkinObj.ReceivingPreferences = receiptCheckIn.ReceivingPreferences;

Here, I am using JArray and JObject. instead of using JArray and JObject. I want to create a separate class and need to use it. checkinDetail and container is an array here

CodePudding user response:

Then create separate classes according to your needs and use them like:

 var container = new SeparateClassSomething();
container.Quantity = checkinDetail.Quantity;
container.QuantityUm = checkinDetail.QuantityUm;

var checkinDetail = new SeparateClassSomethingElse();
checkinDetail.DispositionCode = DispositionCode;
checkinDetail.container = new AnotherSeparateClass(container);

var checkinObj = new YetAnotherSeparateClass();
checkinObj.CheckinDetailEntities = new AndOneMoreSeparateClass(checkinDetail);
checkinObj.InternalReceiptNum = internalReceiptNum;
checkinObj.ReceivingPreferences = receiptCheckIn.ReceivingPreferences;               

CodePudding user response:

Dynamics types are useful in some cases. Sometimes I use it just for quick testing a payload or POC, but when you develop software, it is a good practice to have your custom class model in place.

Here is a simplified example without testing it. I have improvised the property types, but you can change them to fit your need.

public class Container
{
    public int Quantity { get; set; }
    public int QuantityUm { get; set; }
}

public class ContainerDetail
{
    public string DispositionCode { get; set; }
    public IList<Container> Container { get; set; }
}

public class CheckinDetailEntities
{
    public IList<ContainerDetail> ContainerDetail { get; set; }
    public int InternalReceiptNum { get; set; }
    public string ReceivingPreferences { get; set; }
}

and here is a simple object of the class:

Container container = new Container();
container.Quantity = 1;
container.QuantityUm = 1;

ContainerDetail checkinDetail = new ContainerDetail();
checkinDetail.DispositionCode = "DispositionCode";
checkinDetail.Container = new List<Container>
{
    container
};

CheckinDetailEntities checkinObj = new CheckinDetailEntities();
checkinObj.ContainerDetail = new List<ContainerDetail>
{
    checkinDetail
};
checkinObj.InternalReceiptNum = 1;
checkinObj.ReceivingPreferences = "ReceivingPreferences";
  •  Tags:  
  • c#
  • Related