2

I've got a Java application that is using a layered architecture, we have: presentation layer, business layer and data access layer. Our business layer uses the models to process the "requests". We are using the concept of "ViewModel", so every View-Action relation has its View. What is happening is that we have a lot of ViewModels. The number of files are kind of confusing but the real problem is that we are facing a name conflict problem.

This raised some questions:

  1. In this case we are using ViewModels in a layered architecture. Is this approach OK?
  2. Is it normal to face a high number of files?
  3. Is there any naming convention you use to make it clear to which View the model "belong" to?
  4. If a model is exactly the same for two views, should they be shared or should I have two different models that have exactly the same content?
1

Yes, it is normal to have lots of ViewModels, and yes, naming and namespacing is important. In my apps, I typically create a hierarchy of ViewModels to match my controllers/presenters and actions. For example:

  • Controllers
    • ProductsController
    • OrdersController
  • ViewModels
    • Products
      • ListProductViewModel (assumes a "list" View)
      • EditProductViewModel (assumes a "edit" View)
    • Orders
      • ShowOrderViewModel
      • CreateOrderViewModel

ViewModels are almost always only data structures with no behavior, and as such, it doesn't matter so much if some of the code is duplicated. To me, it makes it that much easier for someone else to understand and modify than if a ViewModel is shared across views/actions.

  • I feel relieved that it is normal. I like your separations ideas, I will propose to our team, thanks :D – JSBach Sep 5 '14 at 16:19

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.