views:

736

answers:

6

I'm stuck with figuring out a good naming convention for the service layer in a spring application. For each class in the service layer I first write the interface it should implement and then the actual class. So for example I have the following interface:

public interface UserAccountManager{
   public void registerUser(UserAccount newUserAccount);
   public void resetPassword(UserAccount userAccount);
...
}

And then the implementation class...

What bugs me here is UserAccountManager is a good name for the implementation class so I'm forced into giving it a stupid name like SimpleUserAccountManager or UserAccountDbManager. What are some of the conventions you've used so far? Is it a good idea to put the implementation classes in a different package and give them the same names as the interfaces? Also what are your thoughts on using names ending with Manager over names ending with Service?

+5  A: 

Spring itself gives interfaces generic names and then names the classes based on the details of the implementation. This is one example that comes in mind:

interface: Controller
abstract classes: AbstractController, AbstractCommandController, 
                  SimpleFormController, MultiActionController

I don't think names like SimpleUserAccountManager or UserAccountDbManager are stupid, since they convey some information regarding the implementation of the manager/service.

What I find stupid is the common convention to add the "Impl" suffix at the implementation classes:

my/package/UserAccountManager
my/package/impl/UserAccountManagerImpl

Some people prefer this though.

kgiannakakis
+1  A: 

I feel that the Service vs. Manager naming suffix is purely a preference. The only time where "Service" has ever cause confusion for us is when we also have Web services sitting on top of our service layer. On some projects, we simply referred to the Web service classes as brokers as all they did was serve to translate or broker the Web service call into a call to our service tier.

I agree with kgiannakakis that suffixing your implementations with "Impl" is not a good approach. I have also come across coding best practices that mention not to do this. Naming the interface after the abstraction is the generally accepted best practice. Naming the implementation class after the interface with some indicator of its purpose or type, as kgiannakakis suggested, seems to be the generally accepted approach.

When we have Web service based DAOs and ORM based DAOs, we use both packages and class names to differentiate the implementation classes from their interfaces and each other. I think putting the implementations in different packages comes down to how many classes you have in the package, how differently they are implemented, and how much you desire to split things up.

DavidValeri
That's why I figured Manager would cause less confusion since the frontend for the application is in Flex and there will be AMF services sitting on top of the service layer. Good point.
Vasil
+1  A: 

For the example you give, I would use implementation names that reflect how the class performs the operations, like HibernateUserAccountManager, or JPAUserAccountManager, or JDBCUserAccountManager, etc, or perhaps just UserAccountManagerDAO.

skaffman
+1  A: 

You could also name the interface IUserAccountManager (this convention is used in Eclipse RCP, for instance) and then use UserAccountManager for the default implementation.

Ilya Boyandin
A: 

I like to use the "Service" suffix, because it matches nicely with the Spring @Service annotation.

I also use a "ServiceImpl" suffix for implementing classes. Java and Spring force us to mostly have one interface per class, so why shouldn't the naming reflect that standard?

I agree with you - it seems more stupid to try and think of a unique name for every implementing class like SimpleUserAccountManager or UserAccountDbManager. Usually there is only one implementing class and it is the default implementation anyway - so calling it *Impl conveys more information than some arbitrary name you've thought up.

Daniel Alexiuc
+2  A: 

Here is what we use:

  • XxxDAO (Data Access Object) - Responsible for interacting directly with the EntityManager , JDBC DataSource , file system, etc. Should contain only persistence logic, such as SQL or JPA-QL, but not (or as little as possible) business logic. Should be accessed only from Managers.
  • XxxManager - Manages entites at the business level, usually performs CRUD operations, but adds the required business logic.
  • XxxService - The layer where the business logic resides. Should "speak" in simple objects - Strings, ints, etc. - as much as possible.
  • XxxController - The UI interaction layer. Should speak to Services only.
  • XxxUtilities/XxxUtils - Helper stateless methods, should not depend on any service in the system. If you need such sependency, either convert the utility class to a service or add the service result as a parameter.

For the implementation we add the Impl Suffix (XxxServiceImpl), to distinct it from the interface, and if there are several implementations or we want to add additional information we add it as prefix (JdbcXxxDaoImpl, GoogleMapsGeocodingServiceImpl, etc.). The classes names become a bit long this way, but they are very descriptive and self documenting.

David Rabinowitz
Can you clarify the responsibilities of XxxManager and XxxService? Thanks.
gmoore