我正在使用 .NET Web API,当然还返回序列化为 JSON 的类。到目前为止,我还没有为任何类使用 Data Contract 属性,但是对于下面的这个类,我这样做了,我不知道为什么。Intellitrace 只是说该类无法序列化并尝试添加 DataContract 属性。我会但想知道为什么。
public class Card : BaseGridVM
{
private IEnumerable<Pc> _pcCards;
private IEnumerable<Pt> _ptCards;
private IEnumerable<MembershipCard> _membershipCards;
public Grid.Result Pt
{
get { return GetPtCardGrid(); }
}
public Grid.Result Pc
{
get { return GetPcCardGrid(); }
}
public Grid.Result Membership
{
get { return GetMembershipCardGrid(); }
}
public Card(IEnumerable<Pc> pcCards, IEnumerable<Pt> ptCards, IEnumerable<MembershipCard> membershipCards)
{
_pcCards = pcCards;
_ptCards = ptCards;
_membershipCards = membershipCards;
}
private Grid.Result GetPtCardGrid()
{
var headers = new List<Grid.Header>
{
new Grid.Header() {label = "Card Name", width = 250},
new Grid.Header() {label = "Pts", width = 50},
new Grid.Header() {label = "Activation Date", width = 50}
};
var rows = new List<Grid.Row>();
foreach (var card in _ptCards)
{
var row = new Grid.Row
{
id = card.id,
enabled = card.active.HasValue && (bool)card.active,
cell = new string[3]
};
row.cell[0] = card.cardName;
row.cell[1] = card.pts.HasValue ? card.pts.ToString() : "0";
row.cell[2] = card.activationDate.HasValue ? card.activationDate.ToString() : "-";
rows.Add(row);
}
return buildGrid(rows, headers, 1, 3);
}
private Grid.Result GetPcCardGrid()
{
var headers = new List<Grid.Header>
{
new Grid.Header() {label = "Offer Title", width = 200},
new Grid.Header() {label = "Pces Required", width = 70},
new Grid.Header() {label = "Activation Date", width = 50}
};
var rows = new List<Grid.Row>();
foreach (var card in _pcCards)
{
var row = new Grid.Row
{
id = card.id,
enabled = card.active.HasValue && (bool)card.active,
cell = new string[3]
};
row.cell[0] = card.cardName;
row.cell[1] = card.pces.HasValue ? card.pces.ToString() : "0";
row.cell[2] = card.creationDate.HasValue ? card.creationDate.ToString() : "-";
rows.Add(row);
}
return buildGrid(rows, headers, 1, 3);
}
private Grid.Result GetMembershipCardGrid()
{
var headers = new List<Grid.Header>
{
new Grid.Header() {label = "Card Name", width = 200},
new Grid.Header() {label = "Members", width = 70}
};
var rows = new List<Grid.Row>();
foreach (var card in _membershipCards)
{
var row = new Grid.Row
{
id = card.id,
enabled = card.active.HasValue && (bool)card.active,
cell = new string[2]
};
row.cell[0] = card.cardName;
row.cell[1] = card.membersCount.HasValue ? card.membersCount.ToString() : "0";
rows.Add(row);
}
return buildGrid(rows, headers, 1, 3);
}
}
这是基础 GridVM
public abstract class BaseGridVM
{
protected static Grid.Result buildGrid(IEnumerable<Grid.Row> rows, IEnumerable<Grid.Header> headers, int page, int steps)
{
var row_array = rows.ToArray();
var result = new Grid.Result
{
rows = row_array,
page = page,
records = row_array.Count(),
steps = steps,
headers = headers.ToArray()
};
return result;
}
}
这是另一个不需要数据合同的类
公共类仪表板VM:IDashboardVM {
public IResults_Dashboard results { get; private set; }
public Grid.Result topCompaniesGrid
{
get { return BuildTopCompanies(); }
}
public Grid.Result topAdsGrid
{
get { return BuildTopAds(); }
}
public DashboardVM(IResults_Dashboard results)
{
this.results = results;
}
private static Grid.Result buildGrid(IEnumerable<Grid.Row> rows, IEnumerable<Grid.Header> headers, int page, int steps)
{
var row_array = rows.ToArray();
var result = new Grid.Result
{
rows = row_array,
page = page,
records = row_array.Count(),
steps = steps,
headers = headers.ToArray()
};
return result;
}
private Grid.Result BuildTopCompanies()
{
var headers = new List<Grid.Header>
{
new Grid.Header() {label = "Company Name", width = 150, click = true},
new Grid.Header() {label = "Coupon Views", width = 50, click = true},
new Grid.Header() {label = "Coupon Clicks", width = 50},
new Grid.Header() {label = "Coupon Redemptions", width = 50},
new Grid.Header() {label = "Ad Views", width = 50, click = true},
new Grid.Header() {label = "Ad Clicks", width = 50},
new Grid.Header() {label = "Reward Cards", width = 50},
new Grid.Header() {label = "Fees", width = 50}
};
var rows = new List<Grid.Row>();
foreach (var company in results.companies)
{
var row = new Grid.Row {id = Convert.ToInt32(company.companyId), cell = new string[8]};
row.cell[0] = company.companyName;
row.cell[1] = company.couponViews.ToString();
row.cell[2] = company.couponClicks.ToString();
row.cell[3] = company.couponRedemptions.ToString();
row.cell[4] = company.adViews.ToString();
row.cell[5] = company.adClicks.ToString();
row.cell[6] = company.rewardCards.ToString();
row.cell[7] = company.revenue.ToString();
rows.Add(row);
}
return buildGrid(rows, headers, 1, 3);
}
private Grid.Result BuildTopAds()
{
var headers = new List<Grid.Header>
{
new Grid.Header() {label = "Company Name", width = 150, click = true},
new Grid.Header() {label = "Ad Name", width = 150, click = true},
new Grid.Header() {label = "Views", width = 50},
new Grid.Header() {label = "Clicks", width = 50},
new Grid.Header() {label = "Fees", width = 50}
};
var rows = new List<Grid.Row>();
foreach (var ad in results.ads)
{
var row = new Grid.Row {id = Convert.ToInt32(ad.Id), cell = new string[5]};
row.cell[0] = ad.companyName;
row.cell[1] = ad.name;
row.cell[2] = ad.views.ToString();
row.cell[3] = ad.clicks.ToString();
row.cell[4] = ad.fees.ToString();
rows.Add(row);
}
return buildGrid(rows, headers, 1, 3);
}
}