C
C#2mo ago
error ♡

How do I make a good DTO of lot's of classes.

yo, i have a question, so I have a C# class, which basically has a bunch of classes as it's fields. Class King { public A AClassObj; public B BClassObj } and I will only ever set one of these properties, that is King.AclassObj = setAClass. Context: basically i'm needed to list down all these classes as properties because swagger autogen can pass my classes to the client and generate code for me. In my use case, king will only contain one non null field, Is there a better way to do this. The problem with this that i'm facing right now is, Will parsing this King Object on my service, I will have to check/parse each field with it's associated type and then set using that type correctly (lots of switch statements). and I don't wanna use reflection cause it's slow and also doesn't work for me. Anyone up to sit down and understand this and help me out lol?
6 Replies
Jimmacle
Jimmacle2mo ago
Context: basically i'm needed to list down all these classes as properties because swagger autogen can pass my classes to the client and generate code for me. i'm not super clear on this part is King a DTO for one of your endpoints or is it something else?
error ♡
error ♡2mo ago
yes King is a DTO for my endpoint
Jimmacle
Jimmacle2mo ago
and the endpoint accepts one of several potential models but only ever one at a time?
error ♡
error ♡2mo ago
yes, kinda it accepts an array of king, which each king having a single field only set
Jimmacle
Jimmacle2mo ago
have you looked at polymorphic serialization? assuming these classes can inherit from one base class
error ♡
error ♡2mo ago
these can't that's the problem let's say, u have bunch of classes like, point, line, curve and I want to have a Geometry class, which would be the DTO that interacts with my client, and the geometry class can have any one of those primitive types I have a bunch of C# classes, If i combined them into one class, I can use it, So that Swagger can Autogen my C# classes into python and other stuff i want my API to be language agnostic i am looking for something like, enum of classes , and that field can be set by only one class not sure, if that's a thing problem is, rn, while i'm parsing this List<Geometry> I have specific function on my service, which takes Line,Point(Primitive) class as input, which is basically present inside the geometry class, but I can't specify it correct. Does this make sense? thanks for this, It might be what i'm looking for
Want results from more Discord servers?
Add your server