[英]Running the same method for different variables in a class
I have a class with about 10 or more different boolean values that record whether a user has done a particular action that will give them a specific reward (eg send a message to someone). 我有一类具有大约10个或更多个不同的布尔值的类,这些值记录用户是否执行了特定的操作,这些操作将给予他们特定的奖励(例如,向某人发送消息)。
here is the code for one method for ONE bool/action: 这是一种bool / action的一种方法的代码:
private ReqRewardResult setMsgSent(RewardClass reward, RewardInfo info)
{
if (reward.msgSent)
return ReqRewardResult.RewardAlreadyGiven;
reward.msgSent = true;
reward.earned += info.msgSentReward;
return ReqRewardResult.ReqSuccess;
}
I have tried to create a generic method for this but it seems you can't pass a class variable as a reference? 我试图为此创建一个通用方法,但似乎您不能将类变量作为引用传递?
private ReqRewardResult setRewardAction(ref bool bAction, RewardClass reward, int reward)
{
if (bAction)
return ReqRewardResult.RewardAlreadyGiven;
bAction = true;
reward.earnedTokens += reward;
return ReqRewardResult.ReqSuccess;
}
I have then looked at a couple of methods such as using a delegate function... but this is then kinda pointless as i'd have to repeat several lines again... 然后,我研究了几种方法,例如使用委托函数...但是这有点没有意义,因为我不得不再次重复几行...
I have also seen you could use Reflection... but this is really slow and as this is a web app i'd rather use more repeated code if it improves the overall speed... 我也看到过您可以使用Reflection ...,但这确实很慢,并且因为这是一个Web应用程序,所以我希望使用更多重复的代码,以提高整体速度...
The question: Is there anyway to have a class function that can repeat for several variables of the same type without any performance hit? 问题:总有没有一个类函数可以重复多个相同类型的变量而不会影响性能?
NOTE: This class is data that is loaded from a database and is unique to each user (there could be millions of users) 注意:此类是从数据库加载的数据,并且对于每个用户都是唯一的(可能有数百万个用户)
Many Thanks, 非常感谢,
Phil. 菲尔
class RewardCredit
{
public bool Rewarded { get; set; }
public int Points { get; set; }
}
You could use a Dictionary<string, RewardCredit>
, fill it with reward names as strings and have a function like 您可以使用
Dictionary<string, RewardCredit>
,将奖励名称作为字符串填充,并具有如下功能
void ApplyReward(string rewardName)
{
if (!rewards.ContainsKey(rewardName))
{
return;
}
RewardCredit credit = rewards[rewardName];
if (!credit.Rewarded)
{
tokens += credit.Points;
credit.Rewarded = true;
}
}
You'd then end up using it like so 然后您将最终像这样使用它
Dictionary<string, RewardCredit> rewards = new Dictionary<string, RewardCredit>
{
{ "Message", new RewardCredit { Points = 10 } },
};
ApplyReward("Message");
First it looks like you have a finish and known set of possible user actions that can be rewarded with "tokens" if done by a user. 首先,您似乎已经完成并知道了一组可能的用户操作,如果用户执行了这些操作,则可以使用“令牌”进行奖励。 This can be implemented with the following class:
可以使用以下类来实现:
public sealed class UserAction
{
public static UserAction SendMessage = new UserAction(3);
public static UserAction PhonedFriend = new UserAction(4);
public static UserAction LikedPost = new UserAction(1);
public static UserAction CommentedOnPost = new UserAction(2);
private UserAction(int tokens) => Tokens = tokens;
public int Tokens { get; }
}
Notes: 笔记:
Then, so far I understand, each user may have done each action (or not) but must be rewarded no more than once for each. 到目前为止,据我所知,每个用户可能已执行(或未执行)每个操作,但每个用户所获得的奖励不得超过一次。 This make me think that you need to store a set of these
UserAction
s into a User
. 这使我认为您需要将一组这些
UserAction
存储到User
。 You also need a way to add an action to a user (when he effectively does in on the UI, for example) and also be able to know how much tokens each user has earned. 您还需要一种向用户添加操作的方法(例如,当用户有效地在UI上操作时),并且还需要知道每个用户获得了多少令牌。 The following class implements such behavior:
下面的类实现了这种行为:
public sealed class User
{
private readonly HashSet<UserAction> _doneActions;
public User() => _doneActions = new HashSet<UserAction>();
public ReqRewardResult AddAction(UserAction action) => _doneActions.Add(action) ? ReqRewardResult.ReqSuccess : ReqRewardResult.RewardAlreadyGiven;
public int EarnedTokens => _doneActions.Sum(ua => ua.Tokens);
}
Note: 注意:
HashSet.Add()
's return value to determine if the action had already been done in the past or not HashSet.Add()
的返回值来确定该操作是否在过去已经完成 Advantages of this "object-thinky" solution: 这种“对象思维”解决方案的优点:
if
, which I believe makes your code more maintainable and clear if
,我相信这会使您的代码更易于维护和清晰 UserAction
(one LoC) UserAction
(一个LoC)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.