JSF managed bean naming conventions
Asked Answered
P

1

20

These days I used to work with JSF, but there's a "convention" I'm in doubt if I should use. While working with managed beans, people used to name it as XxxxxManagedBean where the prefix can be any name related to your business.

Have you worked like that? Particularly, I don't like that much despite makes search easy. Are you using another convention?

Thanks for answering this simple doubt.

Physicality answered 18/4, 2011 at 0:7 Comment(0)
S
27

There is no strict convention specified by JSF itself. I've seen the following conventions:

  • FooBean
  • FooBacking
  • FooManager
  • FooController
  • FooManagedBean

Or even just Foo which is then placed in a specific package like com.example.controller, com.example.backing or even com.example.view, etc.

I myself tend to use FooManager for application and session scoped beans (e.g. DataManager, UserManager, LocaleManager, etc) and just Foo, or as mandated by my current project, FooBacking (e.g. Login or LoginBacking) for request and view scoped beans, which are each usually tied to a specific <h:form> and/or view.

FooBean is too vague. Really a lot of classes can be marked as javabeans. JSF managed beans, JPA entities, EJBs, data transfer objects, value objects, etc. The Bean naming does not indicate the real responsibility of the class in any way. True, I use often public class Bean or MyBean in my generic code examples in blogs or forum/Q&A answers, but in real world you should avoid that.

FooManagedBean is IMO a poor name, it's not only too long and ugly, but technically, a managed bean is an instance of a backing bean which is managed by some framework (JSF in this case). The class definition itself is really a backing bean, not a managed bean. So a FooBackingBean is technically more correct, but it's still too long and the Bean part is a bit itchy.

Anyway, this is a pretty subjective question which can hardly be answered objectively with The One And Correct answer. It really doesn't matter that much to me or anyone else what you makes of it, as long as you're consistent with it throughout the entire project.

Sargeant answered 18/4, 2011 at 2:9 Comment(3)
I have seen people use FooMB as a shortcut for FooManagedBeanPokeweed
As a rule of thumb, try to use non-technical names. It's obvious from the context your bean is a managed bean, so don't call it xxxManagedBean. Or rather: don't concentrate on the second part of the name. The important part is xxx. SomeFormBean is a worse name than PersonFormBean, which is worse than CreditUserBean. You get the idea: the latter name tells you something about what the bean is all about. And that's what helps you on the long run.Formative
could we name FooView as primefaces do? or am I wrong?Paternoster

© 2022 - 2024 — McMap. All rights reserved.