【发布时间】:2013-12-08 01:47:35
【问题描述】:
我正在调用 Bloomberg Server API(用于股票市场数据)并在 Dictionary<string, object> 中取回数据,其中字典中的 Key 是 Bloomberg 一侧的 Field Name,并且该对象包含来自 Bloomberg 的数据值可以是string、decimal、DateTime、boolean等
获得彭博数据后,我需要使用返回的值填充我的强类型实体/类。根据我在向bloomberg 发送的请求中发送的字段名称,返回的字典可能具有不同的键值。我遇到的问题是,bloomberg 字段名称和我的 .net 实体的属性名称不匹配,所以我不确定是否可以使用 AutoMapper 或类似库进行此映射。
我还尝试使用Tuple<string,string,object>,其中第一个元组项是Bloomberg 字段名称,第二个元组项是我的实体的属性名称,第三个元组项是Bloomberg 返回的数据值。这也不起作用(到目前为止),所以我想知道是否有一种简单直接的方法来维护这个bloombergfieldEntityProperty 映射并使用相应字段的彭博数据值填充实体的值。泛型(即使用 C# 泛型)解决方案会更好!
我已粘贴以下示例控制台应用程序代码,以便您粘贴并试用。 2 个字典,1 个用于stockdata,另一个用于bonddata 有假数据,但你明白了。我还在下面添加了 cmets 以重申我想要完成的工作。
谢谢!!
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.
}
}
}
【问题讨论】:
标签: c# dictionary mapping automapper bloomberg