0

Maybe anti-pattern is a better word for this question. I do not know, but I mean a violation of DRY. Id like to reuse a partial view, and I can, but that partial view has other things in it I don't want in my div I'm populating.

For example,

I have a page with a name. I can browse to a view and it shows me information about that person. That's a regular View.

I have a different page that needs the name data, and I can call that same controller and get back that same "regular" View, but it has lots of things in it I don't want, like a form, menu, etc.

If I don't call that View I already have, I have to make another one. If I don't do that I have to do some custom rendering in my JavaScript callback and put that in my div.

Am I violating DRY? If I had tons of these it would get unwieldy, but I don't know what else to do other than special views.

I am using ASP.NET Core if that matters.

4
  • If you're copying a view and then modifying it to suit some other specific purpose, that's not really repeating yourself, is it? Besides, sometimes you need things to be two separate objects so that you can modify one thing without affecting the other. Commented Apr 19, 2018 at 20:46
  • It feels like it is.
    – johnny
    Commented Apr 19, 2018 at 20:47
  • @RobertHarvey Just a side question. I was thinking about creating my markup in the JavaScript and wondered if this is what is going on with things like Angular and React, albeit at a more sophisticated level.
    – johnny
    Commented Apr 19, 2018 at 20:51
  • Not really. Angular and React both have Data Binding, but that's not what you're referring to. Commented Apr 19, 2018 at 21:01

1 Answer 1

1

Technically you are probably repeating yourself.

But, at some point you have to decide whether its harder to create a new generic version of something that fulfils two or more purposes, or to create two versions of the same thing.

When it comes to HTML views I think most of us would opt for the latter. HTML just isn't as flexible as your server side code, trying to squeeze it into two shapes with css and javascript ends up being hacky unless its very simple.

You can minimise the duplication by using the same view model, and controller logic (move it into a method) for both. Just return a different view of the same viewmodel.

But there is a school of thought which says, one view per viewmodel. and on the whole I agree. But then I tend to have very light throwaway view models.

As you mention in the comments, you other option is to go for more javascript and just retrieve the viewmodels from the server. In which case your html 'views' will be moved to the client side. Maybe giving you better control, but I think you would end up with two separate templates in either case.

1
  • My other option was to put all the markup in the JavaScript, but I'm not real keen on the mixing concerns, though I see that in SPA libraries.
    – johnny
    Commented Apr 19, 2018 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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