I am using mvc to create a feed api for different integrations. For example, say I want to provide feed of questions posted on my site to others. The initial implementation was to pass the name of the third party as a parameter to an action method on the feeds controller.
public XmlResult Get(string partner)
{
//Call the business layer and get the List<Questions>
if (!string.IsNullOrEmpty(partner))
{
partner = partner.ToLower();
switch (partner)
{
case "org1":
Org1XMLFeedViewModel org1FeedViewModel = new Org1XMLFeedViewModel();
org1FeedViewModel.LastBuildDate = DateTime.Now;
foreach (QuestionInfo Question in QuestionsForFeed)
{
Org1XMLFeedQuestionViewModel QuestionForFeed = new Org1XMLFeedQuestionViewModel(Question);
Org1FeedViewModel.Questions.Add(QuestionForFeed);
}
return new XmlResult(Org1FeedViewModel);
case "org2":
//similar to org1, use org2 ViewModel
default:
return new XmlResult(QuestionsForFeed);
}
}
return new XmlResult(QuestionsForFeed);
}
However, now there is a thought that perhaps different view models should not be within the same action method. Thus instead of passing the partner as a parameter have different action methods for different partners.
Which one is better and why? Or is there a better pattern that comes to your mind?
Holy smokes that controller needs to go on a diet 😉
OT: Personally, I would recommend that you split them into different action methods, kind of separating it out so that each Action can only return one type of controller because it’s that type of Action.
Doing it this way also means that you can abstract away that logic out of the Controller, as the best practice for MVC is thin Controller, fat Model, you wouldn’t be able to adhere to that that here because it’s your controller that has to determine the model type in this case.