0

多くのネストされた条件に基づいてユーザーの評判を計算する方法があります。コードは機能しますが、あまりエレガントではありません。評判はいくつかのルールに基づいて与えられるため、ルールエンジンを使用すると問題が解決すると思いましたが、私が理解している限り条件が満たされた場合、ルール エンジンは TRUE を返し、それ以外の場合は False を返します。私の場合、2 つのオブジェクト User と Entries のコレクションを評価し、獲得ポイントを計算するために条件が何回真であるかを知る必要があります。

アルゴリズムを改善するためのアイデアはありますか?

    /// <summary>
    /// Calculates and updates the user reputation.
    /// </summary>
    /// <param name="user">The user</param>
    public void UpdateReputation(User user)
    {
        user.UserReputations.ToList()
            .ForEach(r => this.UnitOfWork.UserReputations.Delete(r));

        foreach (Entry entry in user.Entries)
        {
            user.UserReputations.Add(new UserReputation()
            {
                Points = 25,
                DateCreated = entry.DateCreated,
                ContentInfo = entry,
                User = user,
                Verb = (int)UserReputationVerbType.EntryPublished
            });

            foreach (Vote vote in entry.Votes)
            {
                if (vote.Positive)
                {
                    user.UserReputations.Add(new UserReputation()
                    {
                        Points = 2,
                        DateCreated = vote.DateCreated,
                        ContentInfo = vote.ContentInfo,
                        User = user,
                        Verb = (int)UserReputationVerbType.UserEntryVotedUp
                    });
                }
                else
                {
                    user.UserReputations.Add(new UserReputation()
                    {
                        Points = -1,
                        DateCreated = vote.DateCreated,
                        ContentInfo = vote.ContentInfo,
                        User = user,
                        Verb = (int)UserReputationVerbType.UserEntryVotedDown
                    });
                }
            }

            foreach (Favourite favouriteEntry in entry.Favourite)
            {
                user.UserReputations.Add(new UserReputation()
                {
                    Points = 5,
                    DateCreated = favouriteEntry.DateCreated,
                    ContentInfo = favouriteEntry.Entry,
                    User = user,
                    Verb = (int)UserReputationVerbType.UserEntryFavourited
                });
            }
        }

        foreach (Favourite favouriteEntry in user.Favourite)
        {
            user.UserReputations.Add(new UserReputation()
            {
                Points = 2,
                DateCreated = favouriteEntry.DateCreated,
                ContentInfo = favouriteEntry.Entry,
                User = user,
                Verb = (int)UserReputationVerbType.EntryFavourited
            });
        }

   (a lot of conditions ...)
4

0 に答える 0