18

I believe I structure my projects like many people do. You have a data layer (DAO's), service layer (services) and presentation layer (Spring MVC, Wicket, ...).

Usually a service starts of being quite simple and 'short'. Gradually however the service must support more and more use cases until after a while it becomes one huge class with many lines and methods and difficult to read and maintain. At that time you can either decide to stick with it or start refactoring which is a cumbersome and 'dangerous' job that can take a lot of work.

I am looking for a solution in how to prevent the need for any future refactoring.
One approach could be splitting up your services in several sub-services and make your original service a service façade. So, for instance, instead of a big UserService you could have a UserServiceFacade which delegates the calls to PasswordService, RegistrationService, ... .

It's not a bad solution I think but I'm not too enthusiastic about it because:

  1. difficult to define in advance in which subservices to split the work; if you missjudged, you might still need te refactor aftwards or have a Service with only one method for instance
  2. reuse of Business Logic can be more difficult if for instance PasswordService and RegistrationService require common functionality

Another solution might be using Business Objects which (in my understanding) can also be seen a subservices but then one per specific UseCase, so you might have BO's like CreateUserBO, CheckPasswordBO, DeleteUserBO, ... .

I'm a bit more enthusiastic about this approach because, in my opnion, it offers quite some advantages:

  1. the BO itself is very readable and only does with it's contract requires it to do; all the rest can be delegated to other BO's, a single BO will be short and to the point
  2. Easy to reuse functionality
  3. Easier to change/switch the implementation of a certain UseCase: just inject another implementation with Spring
  4. Easier to test: only need to test to specific UseCase, delegations to other BO's can be mocked
  5. Collaboration: less conflicts if several developers work on different BO's then when they work on the same service

I do however also see some possible downsides:

  1. a bit extra work (in the sort term at least)
  2. Much more classes which could decrease the readability of your project?
  3. one more abstraction layer: an extra step is required to find the actual implementation of a UseCase

The question or rather questions (sorry) are:

  1. Are Business Objects the ideal solution for this problem?
  2. Do you agree with the advantages/disadvantages of BO's that I list above and/or do you see other?
  3. Are there other (good) solutions to prevent mega services ruining your day?
0

3 Answers 3

6

I would recommend you to look at this article on Domain Driven Design if your application is anything serious than college assignment. The basic premise is structure everything around your entities and have a strong domain model. Differentiate between services that provide infrastructure related things (like sending email, persisting data) and services that actually do things that are your core business requirments.

I would also suggest to explore Spring Roo - which brings in some revolutionary stuff in as far as strcuturing your layers are concerned like not having to need DAO layers etc.

Hope that helps.

2
  • Our projects are DDD at least up to a certain level; we have split up our domains and they are loosely coupled. However, could you explain how DDD relates to the question of how to split up your services into BO's or subservices? Are BO's part of the DDD? Mar 17, 2011 at 14:20
  • Depends on your perspective. If yours is a tiered application and not a complex one then BO's may not truely represent anything in DDD. However if you have rich domain model and logic is crunched by nothing but domain objects then I would question the presence of lot of Business Objects. If they are not crunching any logic then why are they named well "Business Objects"?
    – Nilesh
    Mar 22, 2011 at 8:09
0

With the first approach you mention, instead of creating a 'facade', why not just extend that service? In that sort of situation you'd be able to reuse code from the super / original class.

I think if one organizes their package structure in a readable fashion, then having a number of smaller classes is certainly preferable in any case to have large classes that take on a lot of functionality and as such are more at risk for changes.

In the end I think both approaches are pretty similar, with either you can end up with a high degree of code reuse, and if you need to update something structure-wise, then its pretty easy to make global change (relatively speaking), and also easy to make specific implementation changes.

3
  • So, keep the UserService but also create for instance a PasswordService that extends UserService? Mar 17, 2011 at 12:58
  • Indeed, both solutions are similar, it's a question of granularity. Although, with the BO approach you don't need to think too much about how to organize your UseCases; you just take it one UseCase at the time (per class). Mar 17, 2011 at 13:00
  • 2
    I'd try to avoid using inheritance if the only goal is code reuse. if possible i'd prefer extract the common code in an helper class/object and use composition instead. Mar 17, 2011 at 13:06
0

What I personnally do is that I always use a facade for my services. The logic is in internal classes of the service, but I provide an interface that client of that service can call.

I try to avoid from the begining to put any logic in the facade. It is just boiler plate code that redirect to the proper code logic.

So even if your service has many features, the facade is quite maintenable.

But if you control your whole source base, you should not hesitate to refactor and subdivide if you start to have too many features in one service. Even if you use several classes to do the job, clearly separate your service will be better in the long run.

2
  • Tx, a service facade is certainly a good approach but this facade would be there in both of the above solutions. However, refactoring and subdividing once a service becomes too big is exactly what I want to avoid. Mar 17, 2011 at 14:24
  • If you subdivide in advence, you may overdesign. If you start simple, to stay in good health you'll have to refactor some of your code later. There is no generic silver bullet solution. Mar 17, 2011 at 20:56

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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