views:

409

answers:

3

I am using the WMD markdown editor in a project for a large number of fields that correspond to a large number of properties in a large number of Entity classes. Some classes may have multiple properties that require the markdown.

I am storing the markdown itself since this makes it easier to edit the fields later. However, I need to convert the properties to HTML for display later on. The question is: is there some pattern that I can use to avoid writing markdown conversion code in all my entity classes?

I created a utility class with a method that accepts a markdown string and returns the HTML. I am using markdownj and this works fine.

The problem is for each property of each class that stores markdown I may need another method that converts to HTML:

public class Course{

     private String description;
     .
     .
     .
     public String getDescription(){
          return description;
     }

     public String getDescriptionAsHTML(){
          return MarkdownUtil.convert(getDescription());
     }
     .
     .
     .
 }

The problem there is that if the Course class has 2 more properties Tuition and Prerequisites say, that both need converters then I will have to write getTuitionAsHTML() and getPrerequisiteAsHTML().

I find that a bit ugly and would like a cleaner solution. The classes that require this are not part of a single inheritance hierarchy.

The other option I am considering is doing this in the controller rather than the model. What are your thoughts on this?

Thanks.

[EDIT]: New thoughts (Thanks Jasper). Since the project uses struts2 (I did not say this before) I could create a view component say that will convert the markdown for me. Then I use that wherever I need to display the value as HTML.

+2  A: 

The classes that require this are not part of a single inheritance hierarchy.

They should at least implement a common interface, otherwise coming up with a clean generic solution is going to be cumbersome.

The other option I am considering is doing this in the controller rather than the model. What are your thoughts on this?

This clearly is a responsibility of the View. The #1 MVC rule is that the Model doesn't care about its representation, the markdown in this case.

However, I feel that there is to little detail about your current architecture to give a meaningful answer to your question.

Jasper Bekkers
Though I am not seeing how a common interface helps here. I am using struts2 so maybe I will create a custom component that converts markdown to html and satisfy the "do it in the view" requirement.
Vincent Ramdhanie
The common interface will make it easier to process all items the same way. Say you had a Composite of MarkupEntities, you could simply traverse the composite and marking it up in a polymorphic way.However, the solution that uses a component for markup in the view seems best in this case :-)
Jasper Bekkers
+1  A: 

You do have one option for doing this if you can't use inheritance or an interface. I know, I know refactor but this is reality and *hit happens.

You can use reflection to iterate over your properties and apply the formatting to them. You could either tag them with an attribute or you could adopt a naming scheme (brittle, but still an option).

Justin Bozonier
I considered reflection but rejected it because as you pointed out too brittle.
Vincent Ramdhanie
With attributes I wouldn't say it's brittle, definitely not ideal though.
Justin Bozonier
+1  A: 

Ignoring the architectural problems, I think the simple answer could be:

public String getDescription(MarkDownUtil converter)
{
    if (converter == null) return description;
    else return MarkdownUtil.convert(description);
}

Even better would be to make MarkDownUtil implement IStringConverter, and you could have several different StringConverters for different jobs.

chris