开发者

Convert data from object of class A to an object from class B

I have three different classes:

Task
Order
Transmission

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.

I need to convert between each class to another:

Task -> Order
Order -> Transmission
Transmission -> Task
Transmission -> Order
Order -> Task
Task -> Transmission 

for that I created:

  1. Static class of static keys where each key represents the name of the property.
  2. "DataObject" that holds a dictionary of a property name and an object as its value.
  3. Each class (Task, Order, Transmission) implements IDataShare interface:

    public interface IDataShare { DataObject ToDataObject(); void FromDataObject(DataObject data); }

For example, task objects with the following properties:

WorkerId:5
CustomerId:7
VehicleId:null
StartDate:null

And with the following custom fields:

Subcontractor: {listId:5, Value:4} (this is list field)
delivery Note: "abc" (this is text field)

will be convert to the following dictionary:

{"WorkerId", 5}
{"CustomerId", 7}
{"VehicleId", null}
{"StartDate", null}
{"Subcontractor", {listId:5, Value:4}}
{"delivery Note", "abc"}

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.

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 addit开发者_开发百科ion, I have to remember how to store the type of the value when implementing IDataShare interface which makes the work harder.

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?

Thanks!

UPDATE

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 list: {listId:5, Value:4}. 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.


Well, if the number of classes you're converting between is really as limited as you've said, may I suggest just writing casting operators for your classes?

http://msdn.microsoft.com/en-us/library/xhbhezf4%28v=VS.100%29.aspx

It seems like the amount of logic that you're putting into the conversion is enough to warrant something like this. On the other hand, it seems like there is a base set of fields being used across the different objects and you're just stuffing them into an untyped dictionary. If the fields are common across all types, could you use a conversion to a strongly typed common object? Which also begs the question: could you use a common base class object?

If you have options of modifying the Task, Order, and Transmission definitions, I'd take a look at them again. This sort of scenario seems like a "code smell".


If I understand this correctly ToDataObjectis basically a serializer and FromDataObject is a deserializer. If the data contained by these object is type compatible, then it seems that the very act of serializing it into untyped data is the source of your problem. Why do this, instead of just keeping the data in its native format?

If you need to use an adapter because there are incompatibilities between the objects that can't be resolved for some reason, I would think that you can make one that at least keep the data in its native structures instead of serializing everything to a string. A dictionary in C# can contain anything, at a minimum you could be using a Dictionary<string,object>.

It's also unclear what all this verification is about - why would data be incompatible, if you are mapping properties of the same data types? E.g. assuming that this is an internal process, under what circumstance could (e.g.) a string from one object be trying to be assigned to a long in another object? Seems that would only be necessary if the data were strongly typed in one object, but not in another.


Have you considered using generics?

If Task, Order and Transmission all inherit from a base class like Property, then you could expose a common method for getting the values you need. GetMyValue() where T : Property

It's not very clear what you are trying to achieve.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜