I am currently struggling with whether or not I’ve achieved a good level of separation, or if I’ve missed the point somewhere, as I am relatively new to learning the disciplined side of development…
My goal when I started was to create a layer that was agnostic of any persistence mechanism – I called this data-api. I then implemented these interfaces using JDO, and called this project data-jdo. The logic layer ideally talks only is aware of data-api.
This is the point where I’m not sure what makes sense. The business logic layer has to be invoked somehow, right? So is the expectation that the implementation of the data-api (data-jdo, or something else depending on experimentation) is provided (appropriate to say/do injected?) by the invoker?
So the goal would be to (largely for experience and not for productivity) maybe, implement a data-jpa package that could be substituted in place of data-jdo. So the topmost layer (a web service, generic main method as part of a tool, unit tests, whatever) are the ones to make the choice which implementation to use.
Should I be using some framework like Spring to allow me to choose which implementation of my data-api is used, via XML?
Sorry if that’s a little vague… I guess the root question is, at what point does the consumer of an API depend on, supply, or become paired with, the implementation of that API? If the answer is or should be “never” then what is used to make sure everything is available at runtime and how does the consumer get an instance of whatever the “API” is describing with only interfaces?
I come from a .net background – not a Java one, so I’m afraid I can’t help you with Java specifics.
Yes. In the .Net world I use a Factory (as in an instance of the Factory Pattern) that dynamically returns the data provider implementation (which one of those to use is set by config). The data provider is returned by the factory as an ‘object’ and it’s up to the calling business logic code to cast it to the correct type – as specificed by the interface that the business logic is working against.
I’v egot (another!) article on Dependency Injection for .Net which might help explain with some of the issues, but I’m sure there are good java based ones around somewhere.
Probably. I’d say spend your time getting to grips with the concepts first, worry about “best practice” after that. FYI, I learnt AJAX the hard way – by writting all the code myself. These days I’d run straight to a good framework, but I only think I have the confidence to do that after having really grokked the basics by doing some hard graft at the coal-face 🙂
Yeah – it’s never. Use a Factory.