傑夫,在C#中,並在其最基本的:
public class Report
{
// typical simple property in report
public string ReportUid { get; set; }
// object properties
public Header Header { get; set; }
public Body Body { get; set; }
public Footer Footer { get; set; }
public Report()
{
Header = new Header();
Body = new Body();
Footer = new Footer();
}
internal void CalculateFooterTotals()
{
// summerize the lineitems values in the footer
Footer.TotalItems = Body.LineItems
.Sum(x => x.Quantity);
Footer.TotalPrice = Body.LineItems
.Sum(x => x.Total);
}
}
public class Header
{
public string Name { get; set; }
public DateTime Date { get; set; }
}
public class Body
{
public IList<LineItem> LineItems { get; set; }
public Body()
{
LineItems = new List<LineItem>();
}
}
public class LineItem
{
public string PartNumber { get; set; }
public string PartDescription { get; set; }
public int Quantity { get; set; }
public float ItemPrice { get; set; }
// computed
public float Total
{
get { return Quantity * ItemPrice; }
}
}
public class Footer
{
// populated via report.CalculateFooterTotals()
public int TotalItems { get; internal set; }
public float TotalPrice { get; internal set; }
}
有些屬性是當然的計算,而不是獲取/套。
[編輯] - 認爲它會是很好的做法,增添幾分使用的,因爲我看到你問道格拉斯這個問題(不是從數據庫或其他來源可能更多):
// usage - set up report
var report = new Report {
ReportUid = Guid.NewGuid().ToString(),
Header =
{
Name = "My new report",
Date = DateTime.UtcNow
}};
// add lineitems to body (in real case, probably a loop)
report.Body.LineItems.Add(new LineItem()
{
Quantity = 1,
ItemPrice = 12.30f,
PartDescription = "New shoes",
PartNumber = "SHOE123"
});
report.Body.LineItems.Add(new LineItem()
{
Quantity = 3,
ItemPrice = 2.00f,
PartDescription = "Old shoes",
PartNumber = "SHOE999"
});
report.Body.LineItems.Add(new LineItem()
{
Quantity = 7,
ItemPrice = 0.25f,
PartDescription = "Classic Sox",
PartNumber = "SOX567"
});
// summerize the lineitems values in the footer
report.CalculateFooterTotals();
現在申請報告,以你的畫布表面(HTML等)
private static void DispalyData(Report report)
{
// set out the basics
Console.WriteLine("Header");
Console.WriteLine(report.ReportUid);
Console.WriteLine(report.Header.Date);
Console.WriteLine(report.Header.Name);
// now loop round the body items
Console.WriteLine("Items");
foreach (var lineItem in report.Body.LineItems)
{
Console.WriteLine("New Item---");
Console.WriteLine(lineItem.PartDescription);
Console.WriteLine(lineItem.Quantity);
Console.WriteLine(lineItem.ItemPrice);
Console.WriteLine(lineItem.PartNumber);
Console.WriteLine(lineItem.Total);
Console.WriteLine("End Item---");
}
// display footer items
Console.WriteLine("Footer");
Console.WriteLine(report.Footer.TotalItems);
Console.WriteLine(report.Footer.TotalPrice);
}
// called in code as:
DispalyData(report);
希望這將掃描好的...它推到社區維基(通過修改),因爲它是話題後普遍追捧。
[BTW] - 本書雖然你會知道C#來vb.net轉換器,我想這一個,它看起來非常有前途:http://www.developerfusion.com/tools/convert/csharp-to-vb
使其更加具體。這實際上是關於報告(序列關鍵)還是關於課程設計的更普遍? –
這聽起來像你有你的答案。具有其屬性和表示「LineItems」集合的屬性的標頭對象。這聽起來非常合理。 – Khan
任何具有類屬性的解決方案(如下所示)都只能存儲內容。任何佈局知識(比如:標題都在頂部)必須以其他方式存儲。 –