In one of our project with 3 tier architecture (tightly coupled), there's a bad code smell and it doesn't follow the DRY principle. I want to refactor it with design possible design pattern. I don't want to write this project from scratch.
Here's one of the class in BLL:
namespace BAL
{
public class NotificationSettings
{
public readonly bool SignUPFBPost;
public readonly bool SignUPEmails;
public readonly bool SignUPPushNotification;
public readonly bool SignUPFCActivity;
public readonly bool SignUPBubbleNotification;
public readonly bool SignUPGlobeNotification;
public NotificationSettings()
{
NotificationSettingsBAL objNotificationSettings = new NotificationSettingsBAL();
DataTable dtblNotifList = objNotificationSettings.GetNotificationList();
SignUPFBPost = (Convert.ToInt16(dtblNotifList.Rows[0]["FBWallPost"]) == 1) ? true : false;
SignUPEmails = (Convert.ToInt16(dtblNotifList.Rows[0]["Emails"]) == 1) ? true : false;
SignUPPushNotification = (Convert.ToInt16(dtblNotifList.Rows[0]["PushNotification"]) == 1) ? true : false;
SignUPFCActivity = (Convert.ToInt16(dtblNotifList.Rows[0]["FCActivity"]) == 1) ? true : false;
SignUPBubbleNotification = (Convert.ToInt16(dtblNotifList.Rows[0]["BubbleNotification"]) == 1) ? true : false;
SignUPGlobeNotification = (Convert.ToInt16(dtblNotifList.Rows[0]["GlobNotification"]) == 1) ? true : false;
}
}
}
We're getting various user related settings db from database and checking at different places as per the need and displaying messages or sending push notification.
if (objSqlResult.IsSuccess)
{
FavorBAL.Favor objFavorStatus = (FavorBAL.Favor)objFavorBAL.Status;
switch (objFavorStatus)
{
case FavorBAL.Favor.Initiated:
break;
case FavorBAL.Favor.Accepted:
strTitle = "Accept";
strMessage = MsgSuccess.GetMsg(Successes.FBPostFavorAccept).Replace("{%RecMemberName%}", objMemberBAL.FirstName + " " + objMemberBAL.LastName).Replace("{%ReqMemberName%}", Convert.ToString(dtblFavor.Rows[0]["ReqMemberName"]));
strEmailMessage = MsgSuccess.GetMsg(Successes.FBPostFavorAccept).Replace("{%RecMemberName%}", objMemberBAL.FirstName + " " + objMemberBAL.LastName).Replace("{%ReqMemberName%}", "you");
Common.DisplayMessageFavor(divMsg, MsgSuccess.GetMsg(Successes.FriendFavorAccept), Common.ErrorMsgType.Success);
if (new NotificationSettings().FavorAcceptFBPost)
{
Response.Write(Javascript.ScriptStartTag + "PostOnFBWall(\"" + strMessage + "\",\"" + Config.FacebookAppUrl + "\",\"" + Config.WebSiteUrl + "images/logo.png\",\"Favor in Progress!\",\"\",\"Message\");" + Javascript.ScriptEndTag);
}
if (new NotificationSettings().FavorAcceptEmails)
{
IsNotificationMailAllowed = true;
}
if (new NotificationSettings().FavorAcceptPushNotification)
{
objPushNotification.Push(strMessage, strUDID);
}
break;
//Other Cases
}
}
How can we refector the code with or without a design pattern? There's replication of second code listing or similar to it. I am reading one of the article on interface to make it loosely coupled so that system can adapt changes easily.
(Convert.ToInt16(dtblNotifList.Rows[0]["GlobNotification"]) == 1) ? true : false
looks like a candidate for my favorite Extract method pattern – gnat Dec 30 '11 at 8:40TryParseBool
in an answer from Kane - "main focus being replacing theConvert.ToInt16
repetition with a method" – gnat Dec 30 '11 at 12:03