Perhaps a way to keep your code clean would be to create an extension method for a Ilist? here is a blog post that creates a Ilist extension to randomize the contains of the list. http://www.sedodream.com/PermaLink,guid,e915ecf8-2189-416f-8ca1-e1602414c079.aspx
I don't think it would be too much work to alter it to what you want to do. Hope this helps you towards a solution Keith
Could you not create a HtmlHelper extension method that takes a parameter of type Post, called for instance RenderPostTags.
Within the extension method it can then return
post.Tags.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(t => new YTag { Name = t.Trim() }).ToList()
and you would call it with
<%= Html.RenderPostTags(post) %>
It seems like you are 90% of the way there if you understand how to create Html Helpers so maybe I've missed something.
You also may want to consider instead of a HtmlHelper using a simple DTO (or probably more correctly called presentation model) that you can pass to the view. The DTO would contain any properties you use from the Post and also the TagsCSV property that I
agree shouldn't be added to your domain model. You can then map the properties of your real domain object to this 'presentation' version usually via a class who's only responsibility is to map the properties from the domain model to the presentation model
(This class is usually called a mapper or assembler).
This extra option may not be the right thing for you but you should consider it, the advantage is that it reduces the amount of work occuring in the view, i.e. the view author does not have to call the html helper but can render the property directly.
Regards,
Paul
These links discuss the topic and provide some concrete examples
http://pabloblamirez.blogspot.com - When you ask a question, remember to click "mark as answered" when you get a reply which answers your question; this ensures the right forum member gets credit below for being helpful (and makes search more relevant too).
shapper
Contributor
3932 Points
3789 Posts
Display Data. The correct way ...
Dec 29, 2008 10:46 PM|LINK
Hello,
I have a class, Post, which has a property, Tags, of type IList<Tag>.
Tag is another class with two properties: Id and Name.
On a view I want to display the Post (Title, Body, ...) and also its tags in a CSV format (Example: Microsoft, ASP.NET, MVC).
I see three options:
TagsCSV could be filled from Tags when I get a Post using my BlogService.
Problem: I feel like I am adding redundancy in model.
- Convert the Tags to TagsCSV on my View using something like:
<%= MyViewData.Article.Tags.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(t => new YTag { Name = t.Trim() }).ToList();Problem: I think that using so much C# code in the view seems always strange
I know how to create an HtmlHelper. But I don't know how to create an HtmlHelper where I could replicate the previous code, i.e:
Specify that the CSV list should be created using the Name of each Tag in the list.
How can I do this?
Thanks.
Miguel
kstenson
Member
26 Points
8 Posts
Re: Display Data. The correct way ...
Dec 29, 2008 11:40 PM|LINK
shapper
Contributor
3932 Points
3789 Posts
Re: Display Data. The correct way ...
Dec 30, 2008 12:19 AM|LINK
Sorry,
I don't get it ... you mean that I should create an extension to convert the list to a CSV format?
But could this be generic? I mean, could I use for example a lambda expression to define what property in the list should be used to create the CSV?
Anyway, that wasn't really the original problem ...
... my problem is more how should I deliver that data to the view ...
PaulBlamire
Participant
1272 Points
227 Posts
Re: Display Data. The correct way ...
Dec 30, 2008 12:47 AM|LINK
Hi,
Could you not create a HtmlHelper extension method that takes a parameter of type Post, called for instance RenderPostTags.
Within the extension method it can then return
post.Tags.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(t => new YTag { Name = t.Trim() }).ToList()
and you would call it with
<%= Html.RenderPostTags(post) %>
It seems like you are 90% of the way there if you understand how to create Html Helpers so maybe I've missed something.
You also may want to consider instead of a HtmlHelper using a simple DTO (or probably more correctly called presentation model) that you can pass to the view. The DTO would contain any properties you use from the Post and also the TagsCSV property that I agree shouldn't be added to your domain model. You can then map the properties of your real domain object to this 'presentation' version usually via a class who's only responsibility is to map the properties from the domain model to the presentation model (This class is usually called a mapper or assembler).
This extra option may not be the right thing for you but you should consider it, the advantage is that it reduces the amount of work occuring in the view, i.e. the view author does not have to call the html helper but can render the property directly.
Regards,
Paul
These links discuss the topic and provide some concrete examples
http://codebetter.com/blogs/jean-paul_boodhoo/archive/2007/09/27/screen-bound-dto-s.aspx
http://blog.jpboodhoo.com/MappingEnumerable.aspx
http://blog.jpboodhoo.com/IMapper.aspx