0

I have 3 classes on of which I have control over [Machine - my POCO]. 2 classes are 3rd party; Item and ItemValue (which is derived from Item). Item has a property UniqueIdentifier

I have a server that requires Item[] elements to be passed to it, that are updated by the server. The server has a change event that I receive a callback with an argument ItemValue[] items. Which items can contain anything that changed - including items not in my POCO. I need to easily map the property ItemValue.Value to my POCO properties, so I can set the value of the POCO. I am using the POCO to update a dataBase and also to bind to the UI, I have implemented INotifyPropertyChanged In this POCO.

POCO.Property = ItemValue[index].Value [I need to make sure this actual object corresponds to the POCO.Property ] (some how I need be able to say POCO.Property belongs to ItemValue[index].UniqueID , I do not want to do nested loops of iterations, I do have control over the UniqueID when I create the Item[] , the faster the better, I am thinking to use some sort of dictionary to map it, but I am not sure on exactly how to implement it with the examples or where , my Poco or an intermediary class ..

If at all possible I would like to do this all generically if possible , so maybe I need an intermediary like Mapper Which I am thinking I can use to Create My items Item[] For each Property of T , CreateItem => Item , assigning the UniqueIdentifier, assignTargetAddress, DataType Add to a Dictionary of

I have looked at :

Mapping POCO to Entity in Entity Framework

Adding attributes to POCO properties for mapping x,y cells the answer : a more descriptive class model - this means all of my POCOs would need something like this for float, bool, string, int etc and also makes it pretty much hard coded not so generic. - I would need to do a class for each datatype and then in all my POCOs change to use that class and for UI binding, and Database updates I would need to get those values.

And: http://blogs.msdn.com/b/alexj/archive/2009/06/19/tip-26-how-to-avoid-database-queries-using-stub-entities.aspx seems to be more a database approach , my object is coming back and I have no idea how many or what objects are returned just that they have a uniqueID corresponding to the Item I created and passed to the server. I need some way of using that UniqueID to map the property back to the POCO. And here looks similar to what I am looking for I guess but I am just not sure about it or how to transform - I do not want everything converted to strings - maybe I am looking at this worng. C# User Defined CSV Mapping to POCO

Any help is appreciated as I am more nubish than guru.

Code Sample Below:

public List<Machine> Machines;
public List<Item> MachineItems;

public class Machine : IMachine
{
 [DataMember]
 double SensorA {get; set;}

 [DataMember]
 double SensorB {get; set;}

 [DataMember]
 string Sytem1 {get; set;}
}
public class ItemValue : Item
{
 object value;
}
public class Item
{
 object clientID;
 string targetAddress;
 System.Type sysType;
}
OnNewlyReadValues(ItemValue[] itemValues)
{
   // each itemValue in itemValues represents a property
   // in the POCO .
   // itemValues May contain one itemValue or all itemValue
   // that represent the values in the POCO.
   // Determine if itemValue is for which Machine.PropertyName
   // property SensorA  (or any of the properties)
   // Machine.Property = itemValue.Value;

}
Community
  • 1
  • 1
Stix
  • 485
  • 4
  • 13

1 Answers1

0

I found an answer that was provided to me on codeproject: Using a dictionary with an action will allow me to keep track of the items and also the Property to which it belongs via the clientID

public Dictionary<object, Action<machine,object>> setters
 = new Dictionary<object, Action<machine,object>>();

My clientID is a Guid.PropertyName so now I can easily match Machine(Guid).Property

Stix
  • 485
  • 4
  • 13