Skip to main content
Forgot implementation
Source Link
JDT
  • 126
  • 3

I'm not really sure what you're trying to do but I think the purpose of your code is to check what kind of notifications are enabled and perform them accordingly. Assuming that the notification settings are on a per-user basis (as a setting for example) and not something that can be enabled/disabled application wide I would make the following changes:

Create a general INotification-interface and subclass from that interface

//depending on how different the messages are per type of notification
//you might only need one message
public interface INotification
{
    public String NotificationMailMessage { get; };
    public String NotificationPushMessage { get; };
    //add more types here
}

//you might only need one concrete implementation, dependings on how complex the logic is
//using the INotification-interface gives you the flexibility to create specific
//notification classes for more complex notifications
public class SimpleNotification: INotification
{
    //you could replace this with a general message and some parameters if applicable
    public SimpleNotification(String mailMessage, String pushMessage)
    {
        NotificationMailMessage = mailMessage;
        NotificationPushMessage = pushMessage;
    }

    //implement properties here
}

Something you could do is:

public class FavorAcceptedNotification : INotification
{
    private String _firstName;
    private String _lastName;
    private String _reqMemberName;

    public FavorAcceptedNotification(String firstName, String lastName, String reqMembername)
    {
        _firstName = firstName;
        //and so on
    }

    public String NotificationMailMessage
    {
        get
        {
            return MsgSuccess.GetMsg(Successes.FBPostFavorAccept).Replace("{%RecMemberName%}", _firstName + " " + _lastName).Replace("{%ReqMemberName%}", "you");
        }
    }
}

Which means you simplify the switch to:

case FavorBAL.Favor.Accepted:
    strTitle = "Accept";
    var notification = new FavorAcceptedNotification(objMemberBAL.FirstName,  objMemberBAL.LastName, Convert.ToString(dtblFavor.Rows[0]["ReqMemberName"]));
    var notificationSettings = new NotificationSettings();
    notificationSettings.Push(notification);

Your NotificationSettings class (might need to rename that one since it now does more then wrap settings) can deal with determining what notifications are enabled and what needs to be sent. Use Kane's suggestion to refactor with TryParseBool.

It all depends on what you're trying to achieve really.

I'm not really sure what you're trying to do but I think the purpose of your code is to check what kind of notifications are enabled and perform them accordingly. Assuming that the notification settings are on a per-user basis (as a setting for example) and not something that can be enabled/disabled application wide I would make the following changes:

Create a general INotification-interface and subclass from that interface

//depending on how different the messages are per type of notification
//you might only need one message
public interface INotification
{
    public String NotificationMailMessage { get; };
    public String NotificationPushMessage { get; };
    //add more types here
}

//you might only need one concrete implementation, dependings on how complex the logic is
//using the INotification-interface gives you the flexibility to create specific
//notification classes for more complex notifications
public class SimpleNotification: INotification
{
    //you could replace this with a general message and some parameters if applicable
    public SimpleNotification(String mailMessage, String pushMessage)
    {
        NotificationMailMessage = mailMessage;
        NotificationPushMessage = pushMessage;
    }

    //implement properties here
}

Something you could do is:

public class FavorAcceptedNotification
{
    private String _firstName;
    private String _lastName;
    private String _reqMemberName;

    public FavorAcceptedNotification(String firstName, String lastName, String reqMembername)
    {
        _firstName = firstName;
        //and so on
    }

    public String NotificationMailMessage
    {
        get
        {
            return MsgSuccess.GetMsg(Successes.FBPostFavorAccept).Replace("{%RecMemberName%}", _firstName + " " + _lastName).Replace("{%ReqMemberName%}", "you");
        }
    }
}

Which means you simplify the switch to:

case FavorBAL.Favor.Accepted:
    strTitle = "Accept";
    var notification = new FavorAcceptedNotification(objMemberBAL.FirstName,  objMemberBAL.LastName, Convert.ToString(dtblFavor.Rows[0]["ReqMemberName"]));
    var notificationSettings = new NotificationSettings();
    notificationSettings.Push(notification);

Your NotificationSettings class (might need to rename that one since it now does more then wrap settings) can deal with determining what notifications are enabled and what needs to be sent. Use Kane's suggestion to refactor with TryParseBool.

It all depends on what you're trying to achieve really.

I'm not really sure what you're trying to do but I think the purpose of your code is to check what kind of notifications are enabled and perform them accordingly. Assuming that the notification settings are on a per-user basis (as a setting for example) and not something that can be enabled/disabled application wide I would make the following changes:

Create a general INotification-interface and subclass from that interface

//depending on how different the messages are per type of notification
//you might only need one message
public interface INotification
{
    public String NotificationMailMessage { get; };
    public String NotificationPushMessage { get; };
    //add more types here
}

//you might only need one concrete implementation, dependings on how complex the logic is
//using the INotification-interface gives you the flexibility to create specific
//notification classes for more complex notifications
public class SimpleNotification: INotification
{
    //you could replace this with a general message and some parameters if applicable
    public SimpleNotification(String mailMessage, String pushMessage)
    {
        NotificationMailMessage = mailMessage;
        NotificationPushMessage = pushMessage;
    }

    //implement properties here
}

Something you could do is:

public class FavorAcceptedNotification : INotification
{
    private String _firstName;
    private String _lastName;
    private String _reqMemberName;

    public FavorAcceptedNotification(String firstName, String lastName, String reqMembername)
    {
        _firstName = firstName;
        //and so on
    }

    public String NotificationMailMessage
    {
        get
        {
            return MsgSuccess.GetMsg(Successes.FBPostFavorAccept).Replace("{%RecMemberName%}", _firstName + " " + _lastName).Replace("{%ReqMemberName%}", "you");
        }
    }
}

Which means you simplify the switch to:

case FavorBAL.Favor.Accepted:
    strTitle = "Accept";
    var notification = new FavorAcceptedNotification(objMemberBAL.FirstName,  objMemberBAL.LastName, Convert.ToString(dtblFavor.Rows[0]["ReqMemberName"]));
    var notificationSettings = new NotificationSettings();
    notificationSettings.Push(notification);

Your NotificationSettings class (might need to rename that one since it now does more then wrap settings) can deal with determining what notifications are enabled and what needs to be sent. Use Kane's suggestion to refactor with TryParseBool.

It all depends on what you're trying to achieve really.

Added example with concrete type
Source Link
JDT
  • 126
  • 3

Adapt the switch toSomething you could do the followingis:

casepublic FavorBAL.Favor.Accepted:class FavorAcceptedNotification
{
    strTitleprivate =String "Accept";_firstName;
    strMessageprivate =String MsgSuccess.GetMsg_lastName;
    private String _reqMemberName;

    public FavorAcceptedNotification(Successes.FBPostFavorAcceptString firstName, String lastName, String reqMembername).Replace("
    {%RecMemberName%
        _firstName = firstName;
        //and so on
    }", 

 objMemberBAL.FirstName + " "public +String objMemberBAL.LastName).Replace("NotificationMailMessage
    {%ReqMemberName%}", 
 Convert.ToString(dtblFavor.Rows[0]["ReqMemberName"]));       get
    strEmailMessage =   {
            return MsgSuccess.GetMsg(Successes.FBPostFavorAccept).Replace("{%RecMemberName%}", objMemberBAL.FirstName_firstName + " " + objMemberBAL.LastName_lastName).Replace("{%ReqMemberName%}", "you");
        }
    }
}

Which means you simplify the switch to:

case FavorBAL.Favor.Accepted:
    strTitle = "Accept";
    var notification = new SimpleNotificationFavorAcceptedNotification(strEmailMessageobjMemberBAL.FirstName, strMessage objMemberBAL.LastName, Convert.ToString(dtblFavor.Rows[0]["ReqMemberName"]));
    var notificationSettings = new NotificationSettings();
    notificationSettings.Push(notification);

Adapt the switch to do the following:

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");
    
    var notification = new SimpleNotification(strEmailMessage, strMessage);
    var notificationSettings = new NotificationSettings();
    notificationSettings.Push(notification);

Something you could do is:

public class FavorAcceptedNotification
{
    private String _firstName;
    private String _lastName;
    private String _reqMemberName;

    public FavorAcceptedNotification(String firstName, String lastName, String reqMembername)
    {
        _firstName = firstName;
        //and so on
    } 

    public String NotificationMailMessage
    { 
        get
        {
            return MsgSuccess.GetMsg(Successes.FBPostFavorAccept).Replace("{%RecMemberName%}", _firstName + " " + _lastName).Replace("{%ReqMemberName%}", "you");
        }
    }
}

Which means you simplify the switch to:

case FavorBAL.Favor.Accepted:
    strTitle = "Accept";
    var notification = new FavorAcceptedNotification(objMemberBAL.FirstName,  objMemberBAL.LastName, Convert.ToString(dtblFavor.Rows[0]["ReqMemberName"]));
    var notificationSettings = new NotificationSettings();
    notificationSettings.Push(notification);
Source Link
JDT
  • 126
  • 3

I'm not really sure what you're trying to do but I think the purpose of your code is to check what kind of notifications are enabled and perform them accordingly. Assuming that the notification settings are on a per-user basis (as a setting for example) and not something that can be enabled/disabled application wide I would make the following changes:

Create a general INotification-interface and subclass from that interface

//depending on how different the messages are per type of notification
//you might only need one message
public interface INotification
{
    public String NotificationMailMessage { get; };
    public String NotificationPushMessage { get; };
    //add more types here
}

//you might only need one concrete implementation, dependings on how complex the logic is
//using the INotification-interface gives you the flexibility to create specific
//notification classes for more complex notifications
public class SimpleNotification: INotification
{
    //you could replace this with a general message and some parameters if applicable
    public SimpleNotification(String mailMessage, String pushMessage)
    {
        NotificationMailMessage = mailMessage;
        NotificationPushMessage = pushMessage;
    }

    //implement properties here
}

Adapt the switch to do the following:

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");
    
    var notification = new SimpleNotification(strEmailMessage, strMessage);
    var notificationSettings = new NotificationSettings();
    notificationSettings.Push(notification);

Your NotificationSettings class (might need to rename that one since it now does more then wrap settings) can deal with determining what notifications are enabled and what needs to be sent. Use Kane's suggestion to refactor with TryParseBool.

It all depends on what you're trying to achieve really.