POCO到对象映射C# [英] POCO to Object Mapping C#
问题描述
我正在努力解决这个问题,我知道我不应该这样做;但出于某种原因,我现在只是被精神上阻止了。
我有3个课程,我的POCO和两个第三方课程,我试图将第三方类对象的属性值映射到我的一个元素POCO。(我用它作为中介来更新数据库,以及通过WCF的UI)[也许我使用的是一种糟糕的方法,但我对此并不确定]。请参阅下面的代码及其下面的说明。
I am struggling with this concept and I know I should not be; but for some reason I'am just mentally blocked right now.
I have 3 classes, my POCO and two 3rd Party classes and I am trying to map the property value of a 3rd party class object to one of the elements in my POCO .(which I am using as an Intermediary to update a database, and a UI with via WCF) [Maybe I am using a bad approach but I am not sure on this]. See the code below along with the explanatory below it.
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;
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;
}
我只能修改机器类,其他两个类是belo ng到第三方库,期望传递这两种类型; [也许我不知道怎么解决这个问题]。
我将Item实例化为带有参数clientID等的新项目所以我有了clientID并将此Item对象传递给第三方库。
OnNewlyReadValues是来自第三方库的事件,并且具有ItemValue []的参数 - 每个itemValue对象都有一个名为Value的属性,并代表我的一个属性POCO。 ItemValue还有我传递给第三方库的myItem的clientID。
我试图将这个ItemValue.Value属性映射到POCO,效率非常高方式物品不会经常改变,但Value属性会。
如上面的例子,最好的方法是什么?感谢任何示例,ItemValue将clientID作为与我的Item.ClientID匹配的标识符。
I can only Modify Machine Class, the other two classes belong to a 3rd party library, which expects these two types to be passed in; [Maybe I do not know enough to know how to get around that].
I instantiate the Item as a new Item with parameters clientID etc. so I have the clientID and pass this Item object on to the 3rd party Library.
The OnNewlyReadValues is an event from the 3rd party library and has arguments of ItemValue[] - each itemValue object has a property named "Value" and represents a Property in my POCO. ItemValue also has my clientID from myItem that I passed to the 3rd party library.
I am trying to map this ItemValue.Value property to the POCO, in a very efficient manner the Items will not change often, but the Value property will.
What is the best way to do this , given the example above ? Any examples are appreciated,, ItemValue has the clientID as an Identifier which matches my Item.ClientID.
推荐答案
从您的解释我知道您可以将clientID映射到财产。它仍然感觉像魔术一样,我可能会遗漏一些东西,但我们假设你可以进行翻译。
你对构建字典的想法是正确的。它应该是一个setter字典:
From your explanation I understand you are able to map a clientID to a property. It still feels like magic and it's possible that I'm missing something but let's assume you can do the translation.
Your idea about building a dictionary is correct. It should be a dictionary of setters:
public Dictionary<object, Action<machine,object>> setters = new Dictionary<object, Action<machine,object>>
{
{1, (m,v) => m.SensorA = (double)v},
{2, (m,v) => m.SensorB = (double)v},
{3, (m,v) => m.System1 = (string)v},
};
关键是clientId。该值是一个setter - 一个采取POCO和项目值并执行任务的动作。
这可能看起来像是一种矫枉过正,但会让事情成为现实在事件处理程序中更容易:
The key is the clientId. The value is a the setter - an action which takes the POCO and the item value and does the assignment.
This might seem as an overkill but will make things it a lot easier in the event handler:
public void OnNewlyReadValues(ItemValue[] itemValues)
{
var machine = new Machine();
foreach(var itemValue in itemValues)
{
var setter = setters[itemValue.clientID];
setter(machine, itemValue.value);
}
// your POCO is initialized
}
根据clientID检索设置器并将其应用于您的POCO和项目值。
编辑 - 多台机器
如果我理解正确,那么你想在客户端类的生命周期中使用相同的机器实例。 br />
创建一个封装客户端ID的类。在您的第三方库中,clientID是一个对象,因此您可以在那里放置任何内容:
Based on the clientID you retrieve the setter and apply it on your POCO and item value.
Edit - multiple machines
If I understand you correctly then you want to use the same Machine instance trough the life of the client class.
Create a class that will encapsulate client ID. In your third party library the clientID is an object so you can put whatever you want there:
public class ClientId
{
public Machine Machine { get; set; }
public int SetterKey { get; set; }
}
SetterKey是字典中的键。实际上,您可以将其更改为简单数组,然后SetterKey将成为SetterIndex。初始化将如下所示:
The SetterKey is the key in the dictionary. Actually you can change that into simple array and SetterKey will then become SetterIndex. You initialization will look something like this:
Machines = new List<Machine>
{
new Machine(),
new Machine()
};
MachineItems = new List<Item>
{
new Item { clientID = new ClientId { Machine = Machines[0], SetterKey = 1 }},
new Item { clientID = new ClientId { Machine = Machines[0], SetterKey = 2 }},
new Item { clientID = new ClientId { Machine = Machines[0], SetterKey = 3 }},
new Item { clientID = new ClientId { Machine = Machines[1], SetterKey = 1 }},
new Item { clientID = new ClientId { Machine = Machines[1], SetterKey = 2 }},
new Item { clientID = new ClientId { Machine = Machines[1], SetterKey = 3 }},
};
再次这一切都将使事件处理程序中的代码更容易。您将Machine实例直接嵌入clientID中。
Again all this is will make the code in the event handler much easier. You have the Machine instance embedded directly in the clientID.
public void OnNewlyReadValues(ItemValue[] itemValues)
{
foreach(var itemValue in itemValues)
{
var clientId = (ClientId) itemValue.clientID;
var setter = setters[clientId.SetterKey];
setter(clientId.Machine, itemValue.value);
}
}
这篇关于POCO到对象映射C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!