I asked this question earlier about business logic and presentation logic, and it got me thinking.. I think it's easier to spot questionable practice when looking at code in a View because I'm automatically suspicious when I see it. Usually it's ok because it's开发者_如何学Python presentation logic, but I always tend to look closer.
But I don't look as close when it's in a HTML helper. In fact, I know I've done it before and I've told others to move the business logic into a Helper. But is that right?
My guess now is that it's not.. I think the job of the helper is the same as the job of the view. Presentation only. What do you guys think?
The HtmlHelper should be concerned only with outputting the relevant html into your view and should not have knowledge of any business rules. This should be done in your business layer / domain model layer.
The question you need to ask yourself is, "If I took the HtmlHelper out of the current application and put it into another MVC application, could it be reused without modification?". If the answer is no, there's a good chance it knows too much :)
I think you should better put your business logic into your domain model or into services. That way it's much more testable and reusable
Without wishing to sound obvious, the HTML in HTMLHelper
is a big clue as to the fact that this component is presentation focused. I would expect to find no business logic here, and purely HTML functionality (that, likely, could be reused in other projects?)
All your HTMLHelpers should allow being ported over to another assembly and be used across your org :)
精彩评论