0

我有一种基于许多嵌套条件来计算用户声誉的方法,代码有效,但不是很优雅,声誉是根据一些规则给出的,所以我认为使用规则引擎可以解决问题,但据我所知如果满足条件,规则引擎将返回 TRUE,否则返回 False。在我的情况下,我需要评估两个对象 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