Design Question - static method or separate class

Design Question - static method or separate class

Old forum URL: forums.lhotka.net/forums/t/5157.aspx


SonOfPirate posted on Friday, July 25, 2008

I have a situation where I need to implement a basic search method for a business object.  In essence, the user needs to be able to lookup an item in the database and get a summary description of that item.  There will always be one and only one result returned from the search - at least at this point.

By default, my (root) business objects all have a factory method, such as GetProduct(productID), that accepts the guid id of the object to retrieve and returns a single instance of the object.  The object has a child collection of Accessories that are included with the product.  That list is populated with the root object.

What I need to accomplish for my service is a simple lookup by product number.  So, I could very easily create an overloaded GetProduct() method accepting the product number (as a string) and returning the same object.  However, my service method returns a ProductData object which is a stripped down version of the business object for sending the data over the network.

Thinking from an SOA perspective, perhaps the lookup feature should be a class of its own and maybe even return the ProductData object directly so that my service doesn't have to map the objects.  On the other hand, if I start going down this path, wouldn't I end up with separate business objects for each of my service methods because each one has its own behavior/purpose?

Furthermore, doesn't this parallel the Exists() method Rocky describes in the books?  If so, he puts that right on the original business object.  But, it only returns a boolean, not an instance of the class.  Does that/should that matter?

Finally, I also have it in the back of my head that there could be a case where the user may want to search based on more than one criteria.  So perhaps we ask for product number or the product's sku (not necessarily the same).  I'd want to set myself up to easily expand into this arrangement as well.

I'm open to suggestions about my approach before I get too far down any path.

Thanks in advance!

 

RockfordLhotka replied on Friday, July 25, 2008

Design your objects for each use case. And only reuse them if they are identical across use cases.

You have a Product object, which sounds like an editable root. And presumably is for an 'edit product data' use case.

Now you have a different use case were the user wants to 'display summary product data'. It should have its own object(s) - probably a read-only root from the sound of it.

JoeFallon1 replied on Friday, July 25, 2008

"But, it only returns a boolean, not an instance of the class."

That isn't quite right. The Command class is sent over the wire to the DP and is returned fully populated. SO the whole class is available, as are *any* of its properties. Rocky only happened to have 1 property (Exists) in the class - nothing stopping you from having more though.

Joe

 

SonOfPirate replied on Sunday, July 27, 2008

Ok, a couple quick follow-ups then...

If I create a ProductLookup class that assumes the responsibility of locating products based on whatever criteria (in this case, the product number), I am assuming that it makes sense to have the LookupProduct(product#) method return the non-BO ProductData class (as opposed to my full-blown Product business class), correct?

Does the ProductLookup class also assume the responsibility for authorization?  In other words, I need to make sure the user is allowed to perform a product lookup, so it makes sense to call ProductLookup.CanLookupProducts rather than Product.CanLookup, right?

 

RockfordLhotka replied on Sunday, July 27, 2008

Seems reasonable to me.

 

Rocky

 

From: SonOfPirate [mailto:cslanet@lhotka.net]
Sent: Sunday, July 27, 2008 3:23 PM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] Design Question - static method or separate class

 

Ok, quick follow-up then...

If I create a ProductLookup class that assumes the responsibility of locating products based on whatever criteria (in this case, the product number), I am assuming that it makes sense to have the LookupProduct(product#) method return the non-BO ProductData class (as opposed to my full-blown Product business class), correct?

 



Copyright (c) Marimer LLC