+1  A: 

Assuming that id represents the member's id, you could create 3 separate functions to handle each type, thus separating your concerns more than they are now.

Example:

[AcceptVerbs(HttpVerbs.Post)]
public JsonResult DeleteMusicStyleByMember(int id)
{
    if (!(Session["MemberLoggedIn"] is Member)) return Json(string.Empty);
    Member member = (Member)Session["MemberLoggedIn"];
    _memberService.DeleteMusicStyle(member, id);
    return SelectedMusicStyles();
}

[AcceptVerbs(HttpVerbs.Post)]
public JsonResult DeleteMusicStyleByBand(int id, int typeid)
{        
    Band band = _bandService.GetBand(typeid);
    _bandService.DeleteMusicStyle(band, id);
    return SelectedMusicStyles();
}

[AcceptVerbs(HttpVerbs.Post)]
public JsonResult DeleteMusicStyleByEvent
    (int id, int typeid)
{
    Event event = _eventService.GetEvent(typeid);
    _bandService.DeleteMusicStyle(event, id);
    return SelectedMusicStyles();
}

Then you would just modify your jquery post to go to the respective methods depending on what you're trying to do.

Joseph
I have thought of this but it would duplicate code even more which I'm trying to avoid. It does sound like an attractive solution and if there is no other logical way I am deffinitely doing it this way.
Peter
@Peter I'm not sure how this duplicates code, except perhaps having to have multiple method declarations. Aside from that, each method has their own behavior, even in this example. If you find that methods share certain behaviors after the fact then you can refactor again at that point and consolidate that particular behavior.
Joseph
@Joseph I must say I have to agree with you. This probably is the best solution. I'll just have to hold my js responsible for knowing which action to call.
Peter
+1  A: 

How would you refactor this code?

1) The code which checks the user is logged in should be moved:

 if (!(Session["MemberLoggedIn"] is Member)) return Json(string.Empty);
    Member member = (Member)Session["MemberLoggedIn"];

This is a cross cutting concern, which should be applied using a security framework, Spring pops to mind as an example.

2) I would avoid using a singleton pattern to represent this use-cases, they can quickly turn into a collection of scripts which when grow large can be difficult to know where to place code. Consider using the Command Pattern instead.

This pattern will allow you to return the results as JSON, XML or any other format based on the interfaces you which your command to conform too.

class DeleteMusicStyleByBandCommand : JsonResultModelCommand, XmlResultModelCommand {

  public DeleteMusicStyleByBand(int id, int typeid) {
     //set private members
  }

  public void execute() {
    ..
  }

  public JsonResult getJsonResult() { .. }

  public XmlResult getXmlResult() { .. }
}

The Command pattern IMHO is much better at representing use-cases than many methods in a Service..

JamesC
great idea! This will allow for good code separation. I'm going to implement this and see where I end up.
Peter
while implementing this I realised that I still have to create action methods in my controller to call these commands resulting in more code. In the end this would not improve readability nor maintainability. These functions (e.g. deletebandmusicstyle) are called from ajax.
Peter
You are a 100% correct on using a security framework though, I just didn't get time to go through the pro's and con's of the different frameworks out there in combination with asp.net mvc
Peter