Every project comes with it own application data: the minimal data that is needed to actually start running the application. Typically their is some type or code object that is used throughout the application. When using an ORM tool, it’s not always straightforward to select the right code.
Typically, I’d advice and create some TypeEnum which has a code that uniquely identifies the record in the database. The disadvantage against such an approach is that everywhere you need these static objects, you need to retrieve them. This clutters the code and poses (a ver minor) extra load on the server.
A solution:
Static Application Data
Or as I like to call it: SAD
kuch
Anyway, the solution to this problem is easy and elegant. At startup, the application initialises a SAD object, which is nothing more then a bean that contains some public static final fields that are filled in when creating the SAD singleton.
Typically you’ll get something like this:
public class SADFactory {
public final MyCodeObject myCodeObject1;
public final MyCodeObject myCodeObject2;
public SADFactory(MyService service) {
myCodeObject1 = service.findById(1L);
myCodeObject2 = service.findById(2L);
}
}
Now we are set! We can use our special static objects anywhere in our application.
Using Spring, you can inject the SADFactory where required (services, managed beans,…). In this fashion the business logic of selecting the right application from the database is contained in a single class.
Way better then using an enum to do the selecting…
Wiith thanks to Yoeri Roels for coming up with this great idea!
Andries
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.