我正在开发一个框架来从C#应用程序访问EVE Online API。本质上,API通过客户端向EVE Online服务器发送GET请求来工作,然后服务器以XML文件的形式发送响应。我的框架将把这些XML文件中的信息解析为对象。来自请求的信息无法修改(即,无法从客户端应用程序更改服务器数据)。因此,对象本身必须是不可变的。
现在,如果您要查看here,您会发现有很多不同的字段会始终与每个请求一起发送。例如,如果您要请求角色的钱包交易,您可以请求
char/WalletTransactions
哪会返回类似这样的内容
<?xml version='1.0' encoding='UTF-8'?>
<eveapi version="2">
<currentTime>2010-12-10 22:10:45</currentTime>
<result>
<rowset name="transactions" key="transactionID" columns="transactionDateTime,transactionID,quantity,typeName,typeID,price,clientID,clientName,stationID,stationName,transactionType,transactionFor">
<row transactionDateTime="2010-11-24 20:33:00" transactionID="1625396969" quantity="2" typeName="Armor Plates" typeID="25605" price="314004.67" clientID="1429013925" clientName="DDV 77" stationID="60008992" stationName="Lisudeh IV - Moon 4 - Theology Council Tribunal" transactionType="sell" transactionFor="personal" />
<row transactionDateTime="2010-11-17 00:15:00" transactionID="1617616497" quantity="2393" typeName="Phased Plasma S" typeID="184" price="14.90" clientID="979676146" clientName="Kaihokohoko McIver" stationID="60001174" stationName="Lisudeh VI - Moon 2 - Kaalakiota Corporation Factory" transactionType="buy" transactionFor="personal" />
<row transactionDateTime="2010-11-13 05:58:00" transactionID="1613691673" quantity="1" typeName="Survey Scanner I" typeID="444" price="1113.09" clientID="90001413" clientName="Lucius Ventrell" stationID="60004516" stationName="Hek IV - Krusual tribe Bureau" transactionType="sell" transactionFor="personal" />
<row transactionDateTime="2010-11-13 05:57:00" transactionID="1613691609" quantity="2" typeName="Ship Scanner I" typeID="443" price="501.00" clientID="1612349330" clientName="Homer911" stationID="60004516" stationName="Hek IV - Krusual tribe Bureau" transactionType="sell" transactionFor="personal" />
<row transactionDateTime="2010-11-13 05:57:00" transactionID="1613691547" quantity="1" typeName="Rudimentary Ship Scanner I" typeID="6527" price="10.00" clientID="1551104262" clientName="Chloe TaTu" stationID="60004516" stationName="Hek IV - Krusual tribe Bureau" transactionType="sell" transactionFor="personal" />
<row transactionDateTime="2010-11-13 05:57:00" transactionID="1613691498" quantity="2" typeName="150mm Railgun I" typeID="565" price="5002.10" clientID="419113578" clientName="Rashim Xanadu" stationID="60004516" stationName="Hek IV - Krusual tribe Bureau" transactionType="sell" transactionFor="personal" />
<row transactionDateTime="2010-11-13 05:57:00" transactionID="1613691447" quantity="1" typeName="Small Hull Repairer I" typeID="524" price="14103.98" clientID="703468457" clientName="Orgazzmic" stationID="60004516" stationName="Hek IV - Krusual tribe Bureau" transactionType="sell" transactionFor="personal" />
<row transactionDateTime="2010-11-13 05:57:00" transactionID="1613691402" quantity="1" typeName="Dual Light Beam Laser I" typeID="452" price="3009.91" clientID="703468457" clientName="Orgazzmic" stationID="60004516" stationName="Hek IV - Krusual tribe Bureau" transactionType="sell" transactionFor="personal" />
<row transactionDateTime="2010-11-13 05:57:00" transactionID="1613691357" quantity="1" typeName="Small Nosferatu I" typeID="530" price="8106.77" clientID="703468457" clientName="Orgazzmic" stationID="60004516" stationName="Hek IV - Krusual tribe Bureau" transactionType="sell" transactionFor="personal" />
<row transactionDateTime="2010-11-13 05:56:00" transactionID="1613690953" quantity="4" typeName="Small Energy Transfer Array I" typeID="529" price="13511.27" clientID="703468457" clientName="Orgazzmic" stationID="60004516" stationName="Hek IV - Krusual tribe Bureau" transactionType="sell" transactionFor="personal" />
<row transactionDateTime="2010-11-13 05:56:00" transactionID="1613690800" quantity="7" typeName="Tripped Power Circuit" typeID="25598" price="90852.35" clientID="467910905" clientName="Galloway Gallegher" stationID="60004516" stationName="Hek IV - Krusual tribe Bureau" transactionType="sell" transactionFor="personal" />
<row transactionDateTime="2010-11-13 05:56:00" transactionID="1613690762" quantity="1" typeName="Tangled Power Conduit" typeID="25594" price="275.83" clientID="1271418001" clientName="Garthmanx" stationID="60004516" stationName="Hek IV - Krusual tribe Bureau" transactionType="sell" transactionFor="personal" />
</rowset>
</result>
<cachedUntil>2010-12-10 22:25:45</cachedUntil>
</eveapi>
我需要的对象需要拥有每一行中的所有字段(transactionDateTime,transactionID,quantity,typeName,typeID,price,clientID,clientName,stationID,stationName,transactionType,transactionFor),所以它看起来像什么像
public class WalletTransaction
{
public string TransactionDateTime { get; private set; }
public int TransactionID { get; private set; }
public int Quantity { get; private set; }
public string TypeName { get; private set; }
public int TypeID { get; private set; }
public decimal Price { get; private set; }
public int ClientID { get; private set; }
public string ClientName { get; private set; }
public int StationID { get; private set; }
public string StationName { get; private set; }
public Type TransactionType { get; private set; }
public Account TransactionFor { get; private set; }
public int JournalTransactionID { get; private set; }
public WalletTransaction(string transactionDateTime, int transactionID,
int quantity, string typeName, int typeID, decimal price, int clientID,
string clientName, int stationID, string stationName, Type transactionType,
Account transactionFor, int journalTransactionID)
{
TransactionDateTime = transactionDateTime;
TransactionID = transactionID;
Quantity = quantity;
TypeName = typeName;
TypeID = typeID;
Price = price;
ClientID = clientID;
ClientName = clientName;
StationID = stationID;
StationName = stationName;
TransactionType = transactionType;
TransactionFor = transactionFor;
JournalTransactionID = journalTransactionID;
}
public enum Type
{
Buy,
Sell
}
public enum Account
{
Personal,
Computer
}
}
正如您所看到的,这是一个非常重要的构造函数,但除非它具有从XML文件接收的所有信息,否则该对象将不会完全初始化。还有许多其他对象是这样的,需要以这种方式运行(不可变,有许多不同的字段)
所以问题就变成了:由于对象需要是不可变的,并且需要拥有所有这些不同的字段,我是否需要使用构造函数;或者在这种情况下对象初始化器会更好,只要让他们调用setter方法,如果他们真的想要,因为它对服务器没有任何影响?
答案 0 :(得分:1)
从数据源(SQL,文件,XML等)创建对象时,我最喜欢做的事情之一是构建一个获取数据源的构造函数。
在你的情况下,你可以创建一个构造函数或静态工厂方法,它接受一个XmlNode(或者你代表一个事务),并负责从源本身读取值。
两种方法的例子:
public class WalletTransaction
{
public string TransactionDateTime { get; private set; }
public int TransactionID { get; private set; }
public int Quantity { get; private set; }
// More omitted for length
// FACTORY METHOD (RECOMMENDED)
public static WalletTransaction Create(XmlNode node)
{
return new WalletTransaction()
{
TransactionDateTime = node.Attributes["transactionDateTime"].Value,
TransactionID = int.Parse(node.Attributes["transactionID"].Value),
Quantity = int.Parse(node.Attributes["quantity"].Value)
};
}
// CTOR METHOD
public WalletTransaction(XmlNode node)
{
TransactionDateTime = node.Attributes["transactionDateTime"].Value;
TransactionID = int.Parse(node.Attributes["transactionID"].Value);
Quantity = int.Parse(node.Attributes["quantity"].Value);
}
}
出于一个重要原因,静态工厂将是我的偏好。如果在构造函数中抛出异常(例如,int.Parse失败),则实际异常将包装在Type Initialization Exception中。静态工厂方法没有这个缺点,并使调试更容易。
你也可以在静态方法中抛弃字段初始化列表,而不是
var wt = new WalletTransaction();
wt.TransactionDateTime = node.Attributes["transactionDateTime"].Value;
//etc
return wt;
这样做的好处是能够进行更复杂的处理。也就是说,可能一个字段是可选的,或者仅当另一个字段设置为true时才存在。
答案 1 :(得分:0)
为了使用对象初始化器,您的属性必须具有公共setter,因此很难使它们不可变。如果你确实需要对象是不可变的,我建议使用构造函数。