Hi Devin,
I think you should introduce parametric polymorphism here, just like
org.jooq.DAO. Instead of your version, I'd write:
public abstract class BaseService<P, R extends Record> {
protected void create(P tableObject) {
R g = buildRecordObject(tableObject);
g.store();
}
protected void update(P tableObject) {
R g = buildGenericRecord(tableObject);
g.store();
}
protected void delete(P tableObject) {
R g = buildGenericRecord(tableObject);
g.delete();
}
protected P find(String slug) {
return dao().fetchOneBySlug(slug);
}
public List<R> list(int pageNumber) {
pager = new Page(count());
TABLE table = table();
RecordMapper genericMapper = mapper();
return sql.select()
.from(table)
.orderBy(table.CREATED_ON.desc())
.limit(pageSize)
.offset(pager.offsetFromPage(pageNumber))
.fetch(genericMapper);
}
// these would need to be provided by the subclass
protected abstract R buildRecordObject(P tableObject);
protected abstract TABLE table();
protected abstract BaseDao dao();
protected abstract RecordMapper mapper();
}
But then again, why not just use the org.jooq.DAO from jOOQ, which already
does most of this? Quite possibly, though, the existing DAOs are lacking
features, so I'm very open to discussing potential new features that we
could add to them to make your life easier.
Hope this helps,
Lukas
2016-05-16 20:53 GMT+02:00 Devin Austin <[email protected]>:
> Hi all,
>
> Currently, I'm generating my DAOs on build through maven with the jooq
> code generator. This saves me a lot of work on getting the methods I need
> for DAOs created. However, I have a "service" layer on top of those that I
> like to use for CRUD operations that calls either the appropriate DAO
> methods or if I need a little more granularity, dropping down into straight
> jooq queries. Most of the CRUD stuff is exactly the same for each of these
> classes, so it would be nice to have a base service class that implements
> these methods and leaves service layer specific queries to be defined in
> their respective classes.
>
> I'm relatively new to Java, but I've been programming for several years
> professionally. I'm struggling to determine how to create such a base
> class with the appropriate DAO/table class types such that they can be
> called in a subclass without much, or any, instantiation/modification.
> Here is a basic pseudo-example of what I'm talking about:
> https://gist.github.com/dhoss/539650094085c2ef152acab6d5884a0d
>
> I hope what I'm saying makes sense. Thanks in advance, and please let me
> know if I can provide anything else.
>
> -Devin
>
> --
> You received this message because you are subscribed to the Google Groups
> "jOOQ User Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups "jOOQ
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.