如何改进从用户配置文件对象获取过滤数据的方法?
我将以下代码组合在一起,以从用户的个人资料数据中获取一些详细信息。 它可以工作,但不太漂亮。
我有一个保存在用户配置文件中的 UserPreferences
对象。它具有关于是否需要 SMS 警报消息以及警报类型的属性。短信将作为电子邮件发送。创建警报时,我想要获取应收到该警报类型通知的地址的逗号分隔字符串。
用户拥有手机号码和运营商。运营商的 SMS 电子邮件格式类似于 “{number}@ {domain}
,我将这些字段替换为用户的号码和运营商的域。
正如我所说,这确实有效,但是我不关心它是两种方法或者它看起来有多混乱是否有更紧凑的方式将其编写为一种方法?
public static string GetSMSAlertSubscribers(int alertTypeId) {
// end result I want is like: "[email protected],[email protected]"
return String.Join(",", GetSMSAlertSubscribersEnumerable(alertTypeId));
}
public static IEnumerable<string> GetSMSAlertSubscribersEnumerable(int alertTypeId) {
var prefs = Membership.GetAllUsers()
.Cast<MembershipUser>()
.Where(u => WebProfile.GetProfile(u.UserName).Preferences.SendAlertsToSMS.Equals(true)
&& WebProfile.GetProfile(u.UserName).Preferences.SMSAlertTypeIds.Contains(alertTypeId))
.Select(u => WebProfile.GetProfile(u.UserName).Preferences);
var carriers = new AlertRepository().FindAllMobileCarriers().ToList();
foreach (UserPreferences p in prefs) {
yield return carriers.Where(c => c.Id.Equals(p.MobileCarrierId))
.Select(c => c.SMSEmailAddressFormat.Replace("{domain}", c.SMSEmailAddressDomain).Replace("{number}", p.MobileNumber))
.SingleOrDefault();
}
}
我会这样称呼它:
var emailTo = GetSMSAlertSubscribers(3);
>更新:我重构了我的“prefs”查询,以减少 GetProfile()
的重复使用,如下所示:
var prefs = Membership.GetAllUsers()
.OfType<MembershipUser>()
.Select(u => WebProfile.GetProfile(u.UserName).Preferences)
.Where(p => p.SendAlertsToSMS && p.SMSAlertTypeIds.Contains(alertTypeId));
I've hacked the following code together to get some details from my users' profile data. It works, but isn't pretty.
I have a UserPreferences
object that is saved in the users' profiles. It has properties for whether SMS alert messages are desired and for which alert types. The SMS will be sent as an email. When an alert is created, I want to get a comma-delimited string of the addresses that should get notified for that alert type.
The user has a mobile number and a carrier. The carriers have an SMS email format like "{number}@ {domain}
and I'm replacing those fields with the user's number and the carrier's domain.
As I said, this does work, but I don't care for it being two methods or how messy it seems. Is there a more compact way of writing this as one method?
public static string GetSMSAlertSubscribers(int alertTypeId) {
// end result I want is like: "[email protected],[email protected]"
return String.Join(",", GetSMSAlertSubscribersEnumerable(alertTypeId));
}
public static IEnumerable<string> GetSMSAlertSubscribersEnumerable(int alertTypeId) {
var prefs = Membership.GetAllUsers()
.Cast<MembershipUser>()
.Where(u => WebProfile.GetProfile(u.UserName).Preferences.SendAlertsToSMS.Equals(true)
&& WebProfile.GetProfile(u.UserName).Preferences.SMSAlertTypeIds.Contains(alertTypeId))
.Select(u => WebProfile.GetProfile(u.UserName).Preferences);
var carriers = new AlertRepository().FindAllMobileCarriers().ToList();
foreach (UserPreferences p in prefs) {
yield return carriers.Where(c => c.Id.Equals(p.MobileCarrierId))
.Select(c => c.SMSEmailAddressFormat.Replace("{domain}", c.SMSEmailAddressDomain).Replace("{number}", p.MobileNumber))
.SingleOrDefault();
}
}
I'd call it like:
var emailTo = GetSMSAlertSubscribers(3);
UPDATE: I've refactored my "prefs" query to reduce the repetitive use of GetProfile()
as:
var prefs = Membership.GetAllUsers()
.OfType<MembershipUser>()
.Select(u => WebProfile.GetProfile(u.UserName).Preferences)
.Where(p => p.SendAlertsToSMS && p.SMSAlertTypeIds.Contains(alertTypeId));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,这样怎么样:
然后在
PopulateAddress
中进行替换。现在对我来说看起来不那么混乱了。将所有内容塞进一个 Linq 查询中不一定是最好的做法。Mh, how about this:
And do the replace stuff in
PopulateAddress
. Looks less messy to me now. Cramming everything into one Linq query is not neccessarily the best thing to do.