商讯信箱
用户名: @
密  码:   注册|忘记密码
登录
个人用户经销商
您的位置:首页 > 技术频道 > 正文

附件----实现DTO数据装载的2种方式

//C#
namespace VisionTask.Training.ServicePattern.DataTransferObject.Raw
{
/// <summary>
/// 需要通过多次调用生产者服务“拼凑”的消息。
/// </summary>
struct Quote
{
public struct QuoteItem
{
public string ProductId;
public double UnitPrice;
}

public string Id;
public string Company;
public QuoteItem[] Items;
}

/// <summary>
/// 通过Property + Indexer 方式实现的DTO 操作方式。
/// </summary>
class DataTransferObjectA
{
private Quote quote = new Quote();

public string Id
{
get { return quote.Id; }
set { quote.Id = value; }
}

public string Company
{
get { return quote.Company; }
set { quote.Company = value; }
}

public Quote.QuoteItem this[int index]
{
get { return quote.Items[index]; }
set { quote.Items[index] = value; }
}
}

/// <summary>
/// 通过Get() + Set() 方式实现的DTO 操作方式。
/// </summary>
class DataTransferObjectB
{
private Quote quote = new Quote();

public string GetId()
{
return quote.Id;
}

public string GetCompany()
{
return quote.Company;
}

public void SetId(string id)
{
quote.Id = id;
}

public void SetCompany(string company)
{
quote.Company = company;
}

public double GetUnitPrice(int index)
{
return quote.Items[index].UnitPrice;
}

public void SetUnitPrice(int index, double unitPrice)
{
quote.Items[index].UnitPrice = unitPrice;
}

public string GetProductId(int index)
{
return quote.Items[index].ProductId;
}

public void SetProductId(int index, string productId)
{
quote.Items[index].ProductId = productId;
}
}
}
1 2 3 4 5
©版权所有。未经许可,不得转载。
[责任编辑:赵建凯]
[an error occurred while processing this directive]