ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ron Grabowski <rongrabow...@yahoo.com>
Subject Too many methods on a service object
Date Fri, 11 Nov 2005 23:12:20 GMT
I've been discussing design patterns (mainly for web sites) with a
colleague over the last couple days and my brain is fried with trying
to come up with quick answers. Here's some of the things that we've
talked about. Hopefully people will share their two cents on these
issues...

A common pattern for iBATIS is to use it in a service based
architecture. Suppose I have a Person and an Address object. Normally
there would be one or more static methods on the service object:

 Person person = PersonService.GetOne(personId); *
 bool success = PersonService.Update(personId);

My colleague brought up the point that this can get out of hand (i.e.
you start getting _a lot_ of methods):

 PersonService.GetMany();
 PersonService.GetOne(personId);
 PersonService.GetManyWithNonNullAddresses();
 PersonService.GetManyWithNonCityAddresses();
 PersonService.UpdateAddresses(personId, addresses);
 PersonService.UpdateIfPersonIsMale(person); 
 PersonService.UpdateIfPersonIsMarried(person);
 ...

My argument was that those methods wouldn't be written until they
needed to be used. In other words I wouldn't just be making up random
methods and never use them...each method would serve a purpose in the
system.

Another good point that I wasn't able to answer on the spot was the
idea that it would be complicated to populate related objects. I can't
think of a good example of this but suppose I wanted to get the city
names of a person's addresses:

 Person person = PersonService.GetOne(personId);
 AddressCollection addressCollection = 
  AddressService.GetManyByPerson(person);
 ...

Again, that's not the best example. Imagine the case where I have to
call through to many service objects until I get my final result. My
answer to him was that I would replace all those calls with a single
method that returns exactly what I wanted. Instead of calling multiple
service objects, I would simple encapsulate the calls into another
service object:

 IList cityNames = CityService.GetNamesByPersonId(personId);

My vague question is...is there a better way I can explain things?
"Adding methods when I need them" doesn't seem like the best answer.

Some other questions I was stumbling on: should the service layer be
defined through interfaces?

Should the service layer throw its own ServiceExceptions or just pass
through the data layer's exception? What happens if the service layer
just throw up the data exceptions to the presentation layer? The
presentation layer shouldn't know anything about the data layer...

Thanks,
Ron

* - Yes, I'm aware that I posted C# style examples to a Java list.

Mime
View raw message