我正在开发一个框架来从 C# 应用程序访问 EVE Online API。本质上,API 的工作原理是客户端向 EVE Online 服务器发送 GET 请求,然后服务器以 XML 文件的形式发送响应。我的框架会将这些 XML 文件中的信息解析为对象。无法修改来自请求的信息(即无法从客户端应用程序更改服务器数据)。因此,对象本身必须是不可变的。
现在,如果你看这里,你会看到有许多不同的字段总是随每个请求一起发送。例如,如果你要请求一个角色的钱包交易,你会请求
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 方法,因为它不会对服务器产生任何影响?