1

サイト全体でソーシャルに使用されるオブジェクト(ソーシャルコラボレーションオブジェクト)の基本的な動作を定義する抽象クラスがあります。

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();
    }

これは機能しますが、少し不格好に感じます。私はまだサブクラスで多くのコードを繰り返しています、そしてそのキャストは不必要なパフォーマンスの控除であるように感じます。

同じプロパティで同じ方法で並べ替えられた抽象クラスに基づいて、並べ替えられたオブジェクトのリストを返す方法を提供するにはどうすればよいですか?

4

1 に答える 1

3

抽象メンバーがないため、抽象クラスは必要ないようです。具体的な基本クラスを使用する(必要に応じて仮想メンバーを追加する)のが最善ではないでしょうか。

さらに良いことに、並べ替えに必要なすべてのメンバー(スコア、投票など)を含むインターフェースを使用します。インターフェイスのコレクションをsortメソッドに渡します。

編集ここに簡単な例があります:

internal interface ISCO
{
    double HotScore { get; set; }
}

class SCO : ISCO
{
    public double HotScore { get; set; }

    public static IEnumerable<T> Sort<T>(IEnumerable<T> items) where T : ISCO
    {
        var sorted = items.ToList();
        sorted.Sort();
        return sorted;
    }
}

次に、派生クラスごとにその単一のソートメソッドを使用できます。

List<Post> PostItems = // ...
IEnumerable<Post> sorted = SCO.Sort<Post>(PostItems);
于 2012-08-24T04:32:59.927 に答える