views:

386

answers:

3

I have to build an xml output that represents a data structured for a flex chart placed in the view. I have several options:

  1. have the controller create the xml (using data from the DB), and return it to a view that actually does nothing, since everything is ready.

  2. have the view strongly typed to the data model from the DB, and render the xml declaratively in the view.

  3. create an Html extension method that will contain the logic to create the xml and use it on the view.

in terms of separation of concern, what would be the best option? in the future I don't expect many changes to the xml structure, maybe now and then. I tend to select option 1 as it is more testable, and I feel more comfortable with the controller preparing the xml data.

A: 

I'd go for option number 1, as I feel it fits the MVC pattern the best. It's not the responsiblity of the View to create an XML file based on some data model. That's business logic and is therefor better off in the controller.

And equally important like you say, if you have your controller create the xml file you can create a unit test for it that asserts that the output xml is valid, contains all necessary nodes, etcetera.

Razzie
I tend to agree with you on this. writing the logic as part of the controller is easy, testable, and generally I feel it fits the concept of controller.thanks.
Ami
A: 

Option 2 is the best. Your model has the data, your controller asks for it and offers it to the view. The view just has a tag to say where it goes. That to me is separation of concern.

Seeing Razzie's answer, I liked 1 as well, and I suppose that the model would have to provide some method for serializing some entity class (your chart results) into xml, in order for your strongly typed view to be able to make use of it.

Anyway I hope the answer helps, basically I don't think 3 is very good. :-)

Mark Dickinson
A: 

I would say it depends on what you are more comfortable with as both options 1 and 2 are viable. People will say that option 1 is good as you can use an XmlWriter to make sure you've got valid xml to be returned and people will say option 2 is valid as mvc is all about having complete control on what is rendered in your view (which can be xml).

However, I would personally go with a variation on option 1 to keep the functionality independent of the controller and have it as a standalone utility method which takes in the data and outputs the xml. This will be easier to test but also be available to be called from other places in your code if this is needed in the future. In addition to this it also keeps the code in your controller cleaner.

And I agree with Mark I don't think option 3 is a good way to go.

That's just my thoughts, hope this helps :-)

WestDiscGolf