Skip to content

Commit aaa3be1

Browse files
committed
Use delegation instead of inheritance in Dao implementations.
Now the simplified constructor injection works fine. https://quarkus.io/guides/cdi-reference#simplified-constructor-injection
1 parent f57c366 commit aaa3be1

File tree

85 files changed

+2645
-1892
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+2645
-1892
lines changed

doma-core/src/main/java/org/seasar/doma/internal/jdbc/dao/AbstractDao.java renamed to doma-core/src/main/java/org/seasar/doma/internal/jdbc/dao/DaoImplSupport.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,19 @@
1515
import org.seasar.doma.jdbc.DaoMethodNotFoundException;
1616
import org.seasar.doma.jdbc.QueryImplementors;
1717

18-
public abstract class AbstractDao implements ConfigProvider {
18+
public class DaoImplSupport implements ConfigProvider {
1919

20-
protected final Config __config;
20+
private final Config __config;
2121

22-
protected AbstractDao() {
23-
__config = null;
24-
}
25-
26-
protected AbstractDao(Config config) {
22+
public DaoImplSupport(Config config) {
2723
if (config == null) {
2824
throw new DomaNullPointerException("config");
2925
}
3026
validateConfig(config, null);
3127
this.__config = new RuntimeConfig(config, config.getDataSource());
3228
}
3329

34-
protected AbstractDao(Config config, Connection connection) {
30+
public DaoImplSupport(Config config, Connection connection) {
3531
if (config == null) {
3632
throw new DomaNullPointerException("config");
3733
}
@@ -48,7 +44,7 @@ protected AbstractDao(Config config, Connection connection) {
4844
this.__config = new RuntimeConfig(config, dataSource);
4945
}
5046

51-
protected AbstractDao(Config config, DataSource dataSource) {
47+
public DaoImplSupport(Config config, DataSource dataSource) {
5248
if (config == null) {
5349
throw new DomaNullPointerException("config");
5450
}
@@ -108,27 +104,27 @@ public Config getConfig() {
108104
return __config;
109105
}
110106

111-
protected DataSource getDataSource() {
107+
public DataSource getDataSource() {
112108
return __config.getDataSource();
113109
}
114110

115-
protected CommandImplementors getCommandImplementors() {
111+
public CommandImplementors getCommandImplementors() {
116112
return __config.getCommandImplementors();
117113
}
118114

119-
protected QueryImplementors getQueryImplementors() {
115+
public QueryImplementors getQueryImplementors() {
120116
return __config.getQueryImplementors();
121117
}
122118

123-
protected void entering(String callerClassName, String callerMethodName, Object... args) {
119+
public void entering(String callerClassName, String callerMethodName, Object... args) {
124120
__config.getJdbcLogger().logDaoMethodEntering(callerClassName, callerMethodName, args);
125121
}
126122

127-
protected void exiting(String callerClassName, String callerMethodName, Object result) {
123+
public void exiting(String callerClassName, String callerMethodName, Object result) {
128124
__config.getJdbcLogger().logDaoMethodExiting(callerClassName, callerMethodName, result);
129125
}
130126

131-
protected void throwing(String callerClassName, String callerMethodName, RuntimeException e) {
127+
public void throwing(String callerClassName, String callerMethodName, RuntimeException e) {
132128
__config.getJdbcLogger().logDaoMethodThrowing(callerClassName, callerMethodName, e);
133129
}
134130

doma-core/src/test/java/example/dao/EmpDaoImpl.java

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
import org.seasar.doma.internal.jdbc.command.EntityResultListHandler;
1414
import org.seasar.doma.internal.jdbc.command.EntitySingleResultHandler;
1515
import org.seasar.doma.internal.jdbc.command.EntityStreamHandler;
16-
import org.seasar.doma.internal.jdbc.dao.AbstractDao;
16+
import org.seasar.doma.internal.jdbc.dao.DaoImplSupport;
1717
import org.seasar.doma.internal.jdbc.util.SqlFileUtil;
18+
import org.seasar.doma.jdbc.Config;
19+
import org.seasar.doma.jdbc.ConfigProvider;
1820
import org.seasar.doma.jdbc.IterationCallback;
1921
import org.seasar.doma.jdbc.SelectOptions;
2022
import org.seasar.doma.jdbc.command.DeleteCommand;
@@ -29,65 +31,78 @@
2931
import org.seasar.doma.jdbc.query.SqlFileSelectQuery;
3032

3133
@Generated("")
32-
public class EmpDaoImpl extends AbstractDao implements EmpDao {
34+
public class EmpDaoImpl implements EmpDao, ConfigProvider {
3335

3436
private static Method method0 =
35-
getDeclaredMethod(EmpDaoImpl.class, "selectById", Integer.class, SelectOptions.class);
37+
DaoImplSupport.getDeclaredMethod(
38+
EmpDaoImpl.class, "selectById", Integer.class, SelectOptions.class);
3639

3740
private static Method method1 =
38-
getDeclaredMethod(
41+
DaoImplSupport.getDeclaredMethod(
3942
EmpDaoImpl.class,
4043
"selectByNameAndSalary",
4144
String.class,
4245
BigDecimal.class,
4346
SelectOptions.class);
4447

45-
private static Method method2 = getDeclaredMethod(EmpDaoImpl.class, "selectByExample", Emp.class);
48+
private static Method method2 =
49+
DaoImplSupport.getDeclaredMethod(EmpDaoImpl.class, "selectByExample", Emp.class);
4650

47-
private static Method method3 = getDeclaredMethod(EmpDaoImpl.class, "insert", Emp.class);
51+
private static Method method3 =
52+
DaoImplSupport.getDeclaredMethod(EmpDaoImpl.class, "insert", Emp.class);
4853

49-
private static Method method4 = getDeclaredMethod(EmpDaoImpl.class, "update", Emp.class);
54+
private static Method method4 =
55+
DaoImplSupport.getDeclaredMethod(EmpDaoImpl.class, "update", Emp.class);
5056

51-
private static Method method5 = getDeclaredMethod(EmpDaoImpl.class, "delete", Emp.class);
57+
private static Method method5 =
58+
DaoImplSupport.getDeclaredMethod(EmpDaoImpl.class, "delete", Emp.class);
5259

5360
private static Method method6 =
54-
getDeclaredMethod(EmpDaoImpl.class, "iterate", IterationCallback.class);
61+
DaoImplSupport.getDeclaredMethod(EmpDaoImpl.class, "iterate", IterationCallback.class);
5562

56-
private static Method method7 = getDeclaredMethod(EmpDaoImpl.class, "execute");
63+
private static Method method7 = DaoImplSupport.getDeclaredMethod(EmpDaoImpl.class, "execute");
64+
65+
private final DaoImplSupport __support;
5766

5867
public EmpDaoImpl() {
59-
super(new ExampleConfig());
68+
__support = new DaoImplSupport(new ExampleConfig());
6069
}
6170

6271
public EmpDaoImpl(Connection connection) {
63-
super(new ExampleConfig(), connection);
72+
__support = new DaoImplSupport(new ExampleConfig(), connection);
6473
}
6574

6675
public EmpDaoImpl(DataSource dataSource) {
67-
super(new ExampleConfig(), dataSource);
76+
__support = new DaoImplSupport(new ExampleConfig(), dataSource);
77+
}
78+
79+
@Override
80+
public Config getConfig() {
81+
return __support.getConfig();
6882
}
6983

7084
@Override
7185
public Emp selectById(Integer id, SelectOptions option) {
72-
SqlFileSelectQuery query = getQueryImplementors().createSqlFileSelectQuery(method0);
73-
query.setConfig(__config);
86+
SqlFileSelectQuery query = __support.getQueryImplementors().createSqlFileSelectQuery(method0);
87+
query.setConfig(__support.getConfig());
7488
query.setSqlFilePath(SqlFileUtil.buildPath("example.dao.EmpDao", "selectById"));
7589
query.addParameter("id", Integer.class, id);
7690
query.setOptions(option);
7791
query.setCallerClassName("example.dao.EmpDao");
7892
query.setCallerMethodName("selectById");
7993
query.prepare();
8094
SelectCommand<Emp> command =
81-
getCommandImplementors()
95+
__support
96+
.getCommandImplementors()
8297
.createSelectCommand(
8398
method0, query, new EntitySingleResultHandler<Emp>(_Emp.getSingletonInternal()));
8499
return command.execute();
85100
}
86101

87102
@Override
88103
public List<Emp> selectByNameAndSalary(String name, BigDecimal salary, SelectOptions option) {
89-
SqlFileSelectQuery query = getQueryImplementors().createSqlFileSelectQuery(method1);
90-
query.setConfig(__config);
104+
SqlFileSelectQuery query = __support.getQueryImplementors().createSqlFileSelectQuery(method1);
105+
query.setConfig(__support.getConfig());
91106
query.setSqlFilePath(SqlFileUtil.buildPath("example.dao.EmpDao", "selectByNameAndSalary"));
92107
query.addParameter("name", String.class, name);
93108
query.addParameter("salary", BigDecimal.class, salary);
@@ -96,23 +111,25 @@ public List<Emp> selectByNameAndSalary(String name, BigDecimal salary, SelectOpt
96111
query.setCallerMethodName("selectByNameAndSalary");
97112
query.prepare();
98113
SelectCommand<List<Emp>> command =
99-
getCommandImplementors()
114+
__support
115+
.getCommandImplementors()
100116
.createSelectCommand(
101117
method1, query, new EntityResultListHandler<Emp>(_Emp.getSingletonInternal()));
102118
return command.execute();
103119
}
104120

105121
@Override
106122
public List<Emp> selectByExample(Emp emp) {
107-
SqlFileSelectQuery query = getQueryImplementors().createSqlFileSelectQuery(method2);
108-
query.setConfig(__config);
123+
SqlFileSelectQuery query = __support.getQueryImplementors().createSqlFileSelectQuery(method2);
124+
query.setConfig(__support.getConfig());
109125
query.setSqlFilePath(SqlFileUtil.buildPath("example.dao.EmpDao", "selectByNameAndSalary"));
110126
query.addParameter("emp", Emp.class, emp);
111127
query.setCallerClassName("example.dao.EmpDao");
112128
query.setCallerMethodName("selectByNameAndSalary");
113129
query.prepare();
114130
SelectCommand<List<Emp>> command =
115-
getCommandImplementors()
131+
__support
132+
.getCommandImplementors()
116133
.createSelectCommand(
117134
method2, query, new EntityResultListHandler<Emp>(_Emp.getSingletonInternal()));
118135
return command.execute();
@@ -121,52 +138,59 @@ public List<Emp> selectByExample(Emp emp) {
121138
@Override
122139
public int insert(Emp entity) {
123140
AutoInsertQuery<Emp> query =
124-
getQueryImplementors().createAutoInsertQuery(method3, _Emp.getSingletonInternal());
125-
query.setConfig(__config);
141+
__support
142+
.getQueryImplementors()
143+
.createAutoInsertQuery(method3, _Emp.getSingletonInternal());
144+
query.setConfig(__support.getConfig());
126145
query.setEntity(entity);
127146
query.setCallerClassName("example.dao.EmpDao");
128147
query.setCallerMethodName("insert");
129148
query.prepare();
130-
InsertCommand command = getCommandImplementors().createInsertCommand(method3, query);
149+
InsertCommand command = __support.getCommandImplementors().createInsertCommand(method3, query);
131150
return command.execute();
132151
}
133152

134153
@Override
135154
public int update(Emp entity) {
136155
AutoUpdateQuery<Emp> query =
137-
getQueryImplementors().createAutoUpdateQuery(method4, _Emp.getSingletonInternal());
138-
query.setConfig(__config);
156+
__support
157+
.getQueryImplementors()
158+
.createAutoUpdateQuery(method4, _Emp.getSingletonInternal());
159+
query.setConfig(__support.getConfig());
139160
query.setEntity(entity);
140161
query.setCallerClassName("example.dao.EmpDao");
141162
query.setCallerMethodName("update");
142163
query.prepare();
143-
UpdateCommand command = getCommandImplementors().createUpdateCommand(method4, query);
164+
UpdateCommand command = __support.getCommandImplementors().createUpdateCommand(method4, query);
144165
return command.execute();
145166
}
146167

147168
@Override
148169
public int delete(Emp entity) {
149170
AutoDeleteQuery<Emp> query =
150-
getQueryImplementors().createAutoDeleteQuery(method5, _Emp.getSingletonInternal());
151-
query.setConfig(__config);
171+
__support
172+
.getQueryImplementors()
173+
.createAutoDeleteQuery(method5, _Emp.getSingletonInternal());
174+
query.setConfig(__support.getConfig());
152175
query.setEntity(entity);
153176
query.setCallerClassName("example.dao.EmpDao");
154177
query.setCallerMethodName("delete");
155178
query.prepare();
156-
DeleteCommand command = getCommandImplementors().createDeleteCommand(method5, query);
179+
DeleteCommand command = __support.getCommandImplementors().createDeleteCommand(method5, query);
157180
return command.execute();
158181
}
159182

160183
@Override
161184
public Integer stream(Function<Stream<Emp>, Integer> mapper) {
162-
SqlFileSelectQuery query = getQueryImplementors().createSqlFileSelectQuery(method6);
163-
query.setConfig(__config);
185+
SqlFileSelectQuery query = __support.getQueryImplementors().createSqlFileSelectQuery(method6);
186+
query.setConfig(__support.getConfig());
164187
query.setSqlFilePath(SqlFileUtil.buildPath("example.dao.EmpDao", "iterate"));
165188
query.setCallerClassName("example.dao.EmpDao");
166189
query.setCallerMethodName("iterate");
167190
query.prepare();
168191
SelectCommand<Integer> command =
169-
getCommandImplementors()
192+
__support
193+
.getCommandImplementors()
170194
.createSelectCommand(
171195
method6,
172196
query,
@@ -176,13 +200,13 @@ public Integer stream(Function<Stream<Emp>, Integer> mapper) {
176200

177201
@Override
178202
public void execute() {
179-
SqlFileScriptQuery query = getQueryImplementors().createSqlFileScriptQuery(method7);
180-
query.setConfig(__config);
203+
SqlFileScriptQuery query = __support.getQueryImplementors().createSqlFileScriptQuery(method7);
204+
query.setConfig(__support.getConfig());
181205
query.setScriptFilePath(SqlFileUtil.buildPath("example.dao.EmpDao", "execute"));
182206
query.setCallerClassName("example.dao.EmpDao");
183207
query.setCallerMethodName("execute");
184208
query.prepare();
185-
ScriptCommand command = getCommandImplementors().createScriptCommand(method7, query);
209+
ScriptCommand command = __support.getCommandImplementors().createScriptCommand(method7, query);
186210
command.execute();
187211
}
188212
}

doma-core/src/test/java/org/seasar/doma/internal/jdbc/dao/AbstractDaoTest.java renamed to doma-core/src/test/java/org/seasar/doma/internal/jdbc/dao/DaoImplSupportTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
import org.seasar.doma.jdbc.Config;
1414

1515
/** @author backpaper0 */
16-
public class AbstractDaoTest {
16+
public class DaoImplSupportTest {
1717

1818
@Test
1919
public void testConstructorParameter1() throws Exception {
2020
Config config = null;
2121
try {
22-
new AbstractDao(config) {};
22+
new DaoImplSupport(config) {};
2323
fail();
2424
} catch (DomaNullPointerException expected) {
2525
assertEquals("config", expected.getParameterName());
@@ -31,7 +31,7 @@ public void testConstructorParameter2() throws Exception {
3131
Config config = null;
3232
Connection connection = mock(Connection.class);
3333
try {
34-
new AbstractDao(config, connection) {};
34+
new DaoImplSupport(config, connection) {};
3535
fail();
3636
} catch (DomaNullPointerException expected) {
3737
assertEquals("config", expected.getParameterName());
@@ -43,7 +43,7 @@ public void testConstructorParameter3() throws Exception {
4343
Config config = mock(Config.class);
4444
Connection connection = null;
4545
try {
46-
new AbstractDao(config, connection) {};
46+
new DaoImplSupport(config, connection) {};
4747
fail();
4848
} catch (DomaNullPointerException expected) {
4949
assertEquals("connection", expected.getParameterName());
@@ -55,7 +55,7 @@ public void testConstructorParameter4() throws Exception {
5555
Config config = null;
5656
DataSource dataSource = mock(DataSource.class);
5757
try {
58-
new AbstractDao(config, dataSource) {};
58+
new DaoImplSupport(config, dataSource) {};
5959
fail();
6060
} catch (DomaNullPointerException expected) {
6161
assertEquals("config", expected.getParameterName());
@@ -67,7 +67,7 @@ public void testConstructorParameter5() throws Exception {
6767
Config config = mock(Config.class);
6868
DataSource dataSource = null;
6969
try {
70-
new AbstractDao(config, dataSource) {};
70+
new DaoImplSupport(config, dataSource) {};
7171
fail();
7272
} catch (DomaNullPointerException expected) {
7373
assertEquals("dataSource", expected.getParameterName());

0 commit comments

Comments
 (0)