我不明白为什么这种方法的可维护性指数(在 Visual Studio 中计算)只有 40,我实际上必须删除除前两行之外的几乎所有行以达到 60 以上:
public void getNewPasswordDetails(Int32 newPasswordId)
{
int UserId = HttpContext.Current.User.Identity.GetUserId().ToInt();
bool userIsAdmin = HttpContext.Current.User.IsInRole("Administrator");
//get a list of userIds that have UserPassword records for this password
var UserIDList = DatabaseContext.UserPasswords.Where(up => up.PasswordId == newPasswordId).Select(up => up.Id).ToList();
//Retrive the password -if the user has access
Password newPassword = DatabaseContext.Passwords
.Include("Creator")
.Where(pass => !pass.Deleted
&& (
(UserIDList.Contains(UserId))
|| (userIsAdmin && ApplicationSettings.Default.AdminsHaveAccessToAllPasswords)
|| pass.Creator_Id == UserId)
)
.Include(p => p.Parent_UserPasswords.Select(up => up.UserPasswordUser))
.SingleOrDefault(p => p.PasswordId == newPasswordId);
if (newPassword != null)
{
//map new password to display view model
AutoMapper.Mapper.CreateMap<Password, PasswordItem>();
PasswordItem returnPasswordViewItem = AutoMapper.Mapper.Map<PasswordItem>(newPassword);
//generate a string based view of the new category
string passwordPartialView = RenderViewContent.RenderViewToString("Password", "_PasswordItem", returnPasswordViewItem);
//broadcast the new password details
PushNotifications.sendAddedPasswordDetails(passwordPartialView, returnPasswordViewItem.Parent_CategoryId, returnPasswordViewItem.PasswordId);
}
else
{
//we dont have access any more, so tell UI to remove the password
PushNotifications.sendRemovePasswordAccess(new PasswordDelete()
{
PasswordId = newPasswordId
});
}
}