我打電話彭博服務器API(在股市中的數據),並早在Dictionary<string, object>
凡Key
到字典中是彭博的身邊Field Name
獲取數據,對象包含來自彭博的數據值,可以是string
,decimal
, DateTime
,boolean
等當對象屬性和字典鍵名稱不同時如何將對象/類屬性映射到字典?
當我得到彭博數據後,我需要用返回的值填充我的強類型實體/類。根據我向彭博社的請求發送的字段名稱,返回的字典可能具有不同的鍵值。我遇到的問題是,彭博現場的名字和我的.NET實體的屬性名稱不匹配,所以我不知道我能做到使用AutoMapper
或類似的庫這種映射。
我也嘗試過使用Tuple<string,string,object>
其中第一個元組項是bloomberg字段名,第二個元組項是我的實體的屬性名,第三個元組項是從bloomberg返回的數據值。即不工作或者(到目前爲止),所以我想知道是否有維護這個bloombergfield <的一個簡單直接的方式 - > EntityProperty映射和填充使用彭博的數據價值的各個領域實體的價值。通用(即使用C#泛型)解決方案會更好!
我已經粘貼下面的示例控制檯應用程序代碼,這樣你就可以粘貼和嘗試。 2個字典,1 stockdata
等爲bonddata
有僞造數據,但你的想法。我還在下面添加了一些評論以重新實現我正在嘗試完成的內容。
謝謝!
namespace MapBBFieldsToEntityProperties
{
using System;
using System.Collections.Generic;
class Program
{
public class StockDataResult
{
public string Name { get; set; }
public decimal LastPrice { get; set; }
public DateTime SettlementDate { get; set; }
public decimal EPS { get; set; }
}
public class BondDataResult
{
public string Name { get; set; }
public string Issuer { get; set; }
public decimal Duration { get; set; }
public DateTime YieldToMaturity { get; set; }
}
static void Main(string[] args)
{
// Data Coming from Bloomberg.
// Dictionary Key is the Bloomberg Data Field Name.
// Dictionary Object is the Value returns and can be any .Net primitive Type
// Sample Data returned for a Stock Query to Bloomberg
Dictionary<string, object> dctBloombergStockData
= new Dictionary<string, object>
{
{ "NAME", "IBM" },
{ "PX_LAST", 181.30f },
{ "SETTLE_DT", "11/25/2013" } // This is Datetime value
};
// Sample Data returned for a Bond Query to Bloomberg
Dictionary<string, object> dctBloombergBondData =
new Dictionary<string, object>
{
{ "NAME", "IBM" },
{ "ISSUE_ORG","IBM Corp" },
{ "DURATION", 4.430f },
{ "YLD_TO_M", 6.456f }
};
// This is my Stock Entity
StockDataResult stockData = new StockDataResult();
// This is my Bond Entity
BondDataResult bondData = new BondDataResult();
// PROBLEM STATEMENT:
//
// Need to somehow Map the Data returned from Bloomberg into the
// Corresponding Strong-typed Entity for that Data Type.
// i.e.
// map dctBloombergStockData to stockData Entity instance as follows
//
// dctBloombergStockData."NAME" Key <--------> stockData.Name Property so that
// dctBloombergStockData["NAME"] value of "IBM" can be assigned to stockData.Name
//
// dctBloombergStockData."PX_LAST" Key <--------> stockData.LastPrice Property so that
// dctBloombergStockData["PX_LAST"] value 181.30f can be assigned to stockData.LastPrice value.
// ....
// .. you get the idea.
// Similarly,
// map dctBloombergBondData Data to bondData Entity instance as follows
//
// dctBloombergBondData."NAME" Key <--------> bondData.Name Property so that
// dctBloombergBondData["NAME"] value of "IBM" can be assigned to bondData.Name property's value
//
// dctBloombergBondData."ISSUE_ORG" Key <--------> bondData.Issuer Property so that
// dctBloombergBondData["ISSUE_ORG"] value 181.30f can be assigned to bondData.Issuer property's value.
//
// dctBloombergBondData."YLD_TO_M" Key <--------> bondData.YieldToMaturity Property so that
// dctBloombergBondData["YLD_TO_M"] value 181.30f can be assigned to bondData.YieldToMaturity property's value.
}
}
}
這是一些不錯的代碼。我對它做了一些修改。 – Max
感謝Alex&Max。我將在週末測試你的兩個答案,並讓你知道! – Shiva
@Alex這個偉大的工程!謝謝。我也用BondData測試過它。 1個問題,如果BloombergDataDictionary中的字段沒有映射到實體,我得到一個錯誤。所以我改變了MapFrom代碼,如下檢查密鑰。這是解決這個問題的正確方法嗎? foreach(bloombergDict中的var條目) if(_propertyMappers.ContainsKey(entry.Key)) _propertyMappers [entry.Key](instance,entry.Value); } – Shiva