I have a model class, let's say it's a Book. I need it to be displayed on the screen. I want to have an escaped version of it, so I do not need to escape fields manually.

What should I do? I can

  • add a wrapper over the Book that returns escaped values (e.g. EscapedBook); but every time when I change the book, i need to change the wrapper - at least to add/remove methods.

  • run the model through some AOP and generate proxy before it is going to be displayed on the screen.

  • or run the model through some util that is going to escape all string properties.

IMHO this is not quite a business behavior, so I would like to keep this on the UI layer.


A wrapper is the right way to go here -- you really want the UI part of the UI layer to just reflect what is given and not be responsible for more than very technical bits of filtering content. I would run with the first option you are looking at and create a specific subclass for the UI layer to handle this task.

The real cost from a writing code perspective isn't having to update your EscapedBook whenever the underlying class changes -- underlying classes changing is going to change a lot of things down the logical line. You'd be better off having the internal representation talking to a single class rather than speaking directly to a dozen different things in the front end application.

This front-end specific class could very well be generated via a proxy or AOP process -- that is really a tactical decision depending on what the stack is and what other dependencies you have. You might mean having a straight-through dedicated proxy rather than a separate class. This isn't a horrible idea on the surface but I would prefer the dedciated class concept in the end -- it is more flexible to separate the problems in the end.

The utility class is best avoided here -- you will end up with things like ContentUtil.CleanPage(book.Pages[1]) all over the UI layer and that will hurt to fix when you re-think things. Globals are bad, even when globals are stateless utility functions.

One thing I would not throw out is having this be generated in the business layer -- generating properly escaped content is a testable business requirement of the application I'd rather test against the business layer versus trying to write automated tests of the UI.

  • Hey, @Wyatt Barnett about: "having this be generated in the business layer". Do you mean code generation, e.g. to generate wrappers code from current models? (just to clarify) – lawpert Oct 28 '14 at 23:07
  • Generated in the non pejorative "created/associated by/with" sense. No implication about generated code. – Wyatt Barnett Oct 28 '14 at 23:44
  • If Book has few (a list of) Authors, how would you convert from 'unescaped' to 'escaped' model? You would need to not only to wrap a book, but all it's authors. Basically, every aggregate that is going to be displayed would have its own 'wrapped' version, right? – lawpert Oct 29 '14 at 9:00
  • Perhaps -- really depends on the objects. I'd expect a major player like authors would need some love. But maybe something simpler like prices would not. – Wyatt Barnett Oct 29 '14 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.