サイト全体でソーシャルに使用されるオブジェクト(ソーシャルコラボレーションオブジェクト)の基本的な動作を定義する抽象クラスがあります。
internal abstract class SCO
{
public double HotScore { get; set; }
public double UpVotes { get; set; }
public double DownVotes { get; set; }
public double VoteTotal { get; set; }
public DateTime Created { get; set; }
public SCO(DBItem item, List<Vote> votes )
{
var voteMeta = InformationForThisVote(votes, item.ID);
UpVotes = voteMeta.UpVotes;
DownVotes = voteMeta.DownVotes;
VoteTotal = UpVotes - DownVotes;
HotScore = Calculation.HotScore(Convert.ToInt32(UpVotes), Convert.ToInt32(DownVotes), Convert.ToDateTime(item["Created"]));
Created = Convert.ToDateTime(item["Created"]);
}
private static VoteMeta InformationForThisVote(List<Vote> votes, int itemId)
{
// Loop through votes, find matches by id,
// and record number of upvotes and downvotes
}
private User GetCreatorFromItemValue(DBItem item)
{
// Cast User Object from property of DataBase information
}
}
継承されたオブジェクトのサンプルは次のとおりです。
class Post : SCO
{
public string Summary { get; set; }
public Uri Link { get; set; }
public Uri ImageUrl { get; set; }
public Post(DBItem item, List<Vote> votes)
: base(item, votes)
{
Summary = (string) item["Summary"];
Link = new UriBuilder((string) item["Link"]).Uri;
ImageUrl = new UriBuilder((string) item["ImageUrl"]).Uri;
}
}
これらのクラスに共通している他の点は、ほとんどの場合、ソートされたコレクションとして返されることです。ここでの難しさは、抽象クラス自体をインスタンス化する方法がないため、コレクションを抽象クラスに埋め込むことができないことです。
私がこれまでに取り組んできたのは、ここに示す要約の一部として、Sortメソッドを使用することです。
protected static List<ESCO> SortedItems(List<ESCO> escoList, ListSortType sortType)
{
switch (sortType)
{
case ListSortType.Hot:
escoList.Sort(delegate(ESCO p1, ESCO p2) { return p2.HotScore.CompareTo(p1.HotScore); });
return escoList;
case ListSortType.Top:
escoList.Sort(delegate(ESCO p1, ESCO p2) { return p2.VoteTotal.CompareTo(p1.VoteTotal); });
return escoList;
case ListSortType.Recent:
escoList.Sort(delegate(ESCO p1, ESCO p2) { return p2.Created.CompareTo(p1.Created); });
return escoList;
default:
throw new ArgumentOutOfRangeException("sortType");
}
}
これにより、継承された子のクラスでこれを使用できます。
public static List<Post> Posts(SPListItemCollection items, ListSortType sortType, List<Vote> votes)
{
var returnlist = new List<ESCO>();
for (int i = 0; i < items.Count; i++) { returnlist.Add(new Post(items[i], votes)); }
return SortedItems(returnlist, sortType).Cast<Post>().ToList();
}
これは機能しますが、少し不格好に感じます。私はまだサブクラスで多くのコードを繰り返しています、そしてそのキャストは不必要なパフォーマンスの控除であるように感じます。
同じプロパティで同じ方法で並べ替えられた抽象クラスに基づいて、並べ替えられたオブジェクトのリストを返す方法を提供するにはどうすればよいですか?