Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert data from object of class A to an object from class B
    text
    copied!<p>I have three different classes:</p> <pre><code>Task Order Transmission </code></pre> <p>Each class have properties with different types. Also, there is a possibility to attach data that represented by custom fields (implemented by an array of IField, where IField can be text field or list field). Each custom field have a name that represent the name of the attached data property.</p> <p>I need to convert between each class to another:</p> <pre><code>Task -&gt; Order Order -&gt; Transmission Transmission -&gt; Task Transmission -&gt; Order Order -&gt; Task Task -&gt; Transmission </code></pre> <p>for that I created:</p> <ol> <li>Static class of static keys where each key represents the name of the property.</li> <li>"<code>DataObject</code>" that holds a dictionary of a property name and an object as its value.</li> <li><p>Each class (Task, Order, Transmission) implements IDataShare interface:</p> <p><code>public interface IDataShare { DataObject ToDataObject(); void FromDataObject(DataObject data); }</code></p></li> </ol> <p>For example, task objects with the following properties:</p> <pre><code>WorkerId:5 CustomerId:7 VehicleId:null StartDate:null </code></pre> <p>And with the following custom fields:</p> <pre><code>Subcontractor: {listId:5, Value:4} (this is list field) delivery Note: "abc" (this is text field) </code></pre> <p>will be convert to the following dictionary:</p> <pre><code>{"WorkerId", 5} {"CustomerId", 7} {"VehicleId", null} {"StartDate", null} {"Subcontractor", {listId:5, Value:4}} {"delivery Note", "abc"} </code></pre> <p>the string keys "WorkerId", "CustomerId", "VehicleId", "StartDate" were taken from static class that contains string consts where "Subcontractor" and "deliveryNote" are the names of the custom fields the user added (I don't know which fields the user might add so I just use the field name). When I fill an object using DataObject I have to verify the name of the property is the same as the name of the key and also verify the value is correct (string cannot inserted into long). In addition, custom list field (subcontractor) can't have only itemId as a value because when I have to verify that the listId of the custom field in the object is the same with the listId of the customField in the DataObject.</p> <p>I have many problems about knowing the type of the value. I always have to use "X is Y" if statements of "X as Y" statements. In addition, I have to remember how to store the type of the value when implementing IDataShare interface which makes the work harder.</p> <p>Can anyone help me think about constraint I can add to the conversion proccess from an object to DataObject? Can anyone help me improve this method of converting objects?</p> <p>Thanks!</p> <p><strong>UPDATE</strong><br> I want to explain a point. My biggest problem is that there are several ways to translate each property/custom field so I need to remember the type of the value in DataObject. For example, in Transmission class I have VehicleId property. I want to convert Task object with custom field with the name "VehicleId" to Transmission. All I want is that Task's custom field VehicleId's value will be converted into the VehicleId property of Transmission. But, because it is custom field - as I wrote before - there is a way I store custom field that based on a <code>list: {listId:5, Value:4}</code>. Now, in the conversion proccess (FromDataObject in Transmission) in case the DataObject has "VehicleId" key, I have to check whether the value is long (vehicle id as property) or IListField (vehicle id as custom list field). those type checking really makes mess.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload