What’s a guy to do?
Well, I though it might be nice to prototype a very basic DI factory which is capable of lazy loading, is lightweight enough to use for all of your objects and which uses an included config file for people who swing that way. I wrote this in under three hours and it depends on a base class. It isn’t pretty, but I thought it was kinda fun. The code is up and running on a test project so it works.
Lets start with a simple User object where you'd like to add a full name attribute based on first name and last name properties. You'd simply add a custom getFullName() method to your custom user object (which extends the base object) with the following code:
I love dynamic programming patterns. All other things being equal, the less code in your application, the eaiser it is to code, debug and maintain. Thoughtfully applied dynamic programming can drastically cut the amount of code in your application, making it much more maintainable and flexible. Imagine having a single generic base getter for all of your objects that would support information hiding, remove the need to test for existance, and that would seamlessly support overloading by custom getters where required. That's what I'm playing with here and it seems (so far) to be working very well . . .
This post looks at how to create and load such an object. Basically you just use a createobject() and then the loadQuery() method of the object, parameterized with a recordset returned from a DAO (my DAO's handle bulk operations as well as single records).
As an example, here is a very naive getbyUniqueAttribute() method from a base service object that allows you to create, load and return a bean using any unique attribute and a comma delimited list of one or more values. Please note that this is not a "safe" function, so it is a private method and just allows more specific methods such as getbyID() (another base method) or getbySKU() (a product specific method) or getbyEmail() (a user specific method) . . .
Please see my earlier post for the benefits an iterating business object can provide built up from first principles . . .
Some of it will be very prosaic like the "SSL and the front controller" snippet I'll post tonight, but look out for a whole series on the business iterator and the other patterns I've been blogging about starting tomorrow morning (they're queued up and ready to go!).