I have a C# class that represents a content type in a web content management system.
We have a field that allows a web content editor to enter an HTML template for how the object is displayed. It basically uses the handlebars syntax for substituting object property values into the HTML string:
<h1>{{Title}}</h1><p>{{Message}}</p>
From a class design perspective, should I expose the formatted HTML string (with substitution) as a property or method?
Example as property:
public class Example
{
private string _template;
public string Title { get; set; }
public string Message { get; set; }
public string Html
{
get
{
return this.ToHtml();
}
protected set { }
}
public Example(Content content)
{
this.Title = content.GetValue("title") as string;
this.Message = content.GetValue("message") as string;
_template = content.GetValue("template") as string;
}
private string ToHtml()
{
// Perform substitution and return formatted string.
}
}
Example as method:
public class Example
{
private string _template;
public string Title { get; set; }
public string Message { get; set; }
public Example(Content content)
{
this.Title = content.GetValue("title") as string;
this.Message = content.GetValue("message") as string;
_template = content.GetValue("template") as string;
}
public string ToHtml()
{
// Perform substitution and return formatted string.
}
}
I'm not sure from a design standpoint does it make a difference or are there reasons why one approach is better than the other?