You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+82-67Lines changed: 82 additions & 67 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -51,7 +51,7 @@ The examples of declaring that repository and the dependency on this package in
51
51
52
52
### For Maven
53
53
54
-
```
54
+
```xml
55
55
<dependencies>
56
56
<dependency>
57
57
<groupId>io.github.sttk</groupId>
@@ -63,7 +63,7 @@ The examples of declaring that repository and the dependency on this package in
63
63
64
64
### For Gradle
65
65
66
-
```
66
+
```groovy
67
67
repositories {
68
68
mavenCentral()
69
69
}
@@ -74,103 +74,107 @@ dependencies {
74
74
75
75
## Usage
76
76
77
-
### 1. Implementing DataSrc and DataConn
78
-
79
-
First, you'll define `DataSrc` which manages connections to external data services and creates `DataConn`. Then, you'll define `DataConn` which represents a session-specific connection and implements transactional operations.
### 2. Implementing logic functions and data interfaces
77
+
### 1. Implementing a logic function and a data access interface
113
78
114
-
Define interfaces and functions that express your application logic. These interfaces are independent of specific data source implementations, improving testability.
79
+
First, define a functional interface that represents your application logic, along with its dedicated data access interface. This interface is independent of specific data source implementations, improving testability.
115
80
116
81
```java
117
82
importcom.github.sttk.errs.Err;
118
-
importcom.github.sttk.sabi.Logic;
119
83
120
84
interfaceMyData {
121
85
StringgetText() throwsErr;
122
86
voidsetText(Stringtext) throwsErr;
123
87
}
88
+
```
89
+
```java
90
+
importcom.github.sttk.errs.Err;
124
91
125
92
classMyLogicimplementsLogic<MyData> {
126
-
@Overridepublicvoidrun(MyDatadata) throwsErr {
93
+
@Override
94
+
publicvoidrun(MyDatadata) throwsErr {
127
95
String text = data.getText();
128
96
data.setText(text);
129
97
}
130
98
}
131
99
```
132
100
133
-
### 3. Implementing DataAcc derived classes
101
+
### 2. Implementing DataAcc derived interfaces
134
102
135
-
The `DataAcc` interface abstracts access to data connections. The methods defined here will be used to obtain data connections via `DataHub` and perform actual data operations.
103
+
The `DataAcc` interface provides a simple mechanism to retrieve `DataConn` objects. However, it's the derived interfaces (like `GettingDataAcc` and `SettingDataAcc` in this example) that define the application-specific methods for accessing data. These methods then use `DataHub#getDataConn` to obtain the appropriate `DataConn` and perform the actual data operations.
var stdioDc = getDataConn("stdio", StdioDataConn.class);
149
+
stdioDc.addPostCommit((InputStream in, PrintStream out, PrintStream err) -> {
150
+
out.printf("%s", text);
151
+
});
153
152
}
154
153
}
155
154
```
156
155
157
-
### 4. Integrating data interfaces and DataAcc derived classes into `DataHub`
156
+
### 3. Integrating data interfaces and DataAcc derived interfaces into DataHub
158
157
159
-
The `DataHub` is the central component that manages all `DataSrc` and `DataConn`, providing access to them for your application logic. By implementing the data interface (`MyData`) from step 2 and the `DataAcc` class from step 3 on `DataHub`, you integrate them.
158
+
The `DataHub` is the central component that manages all `DataSrc` and `DataConn`,
159
+
providing access to them for your application logic.
160
+
By implementing the data interface (`MyData`) from step 1. and the `DataAcc` classes
Inside your init function, register your global `DataSrc`. Next, main function calls run function, and inside run function, setup the sabi framework. Then, create an instance of `DataHub` and register the necessary local `DataSrc` using the Uses method. Finally, use the txn method of`DataHub`to execute your defined application logic function (`MyLogic`) within a transaction. This automatically handles transaction commits and rollbacks.
173
+
Inside your `static` block, register your global `DataSrc`. Next, `main` function calls `run` function, and inside `run` function, set up the sabi framework. Then, create an instance of `DataHub` and register the necessary local `DataSrc` using the `Sabi.uses` method. Finally, use the `DataHub#run` method or`DataHub#txn` method to execute your defined application logic function (`MyLogic`) without or within a transaction.
171
174
172
175
```java
173
176
importcom.github.sttk.errs.Err;
177
+
importcom.github.sttk.sabi.DataHub;
174
178
importcom.github.sttk.sabi.Sabi;
175
179
176
180
publicclassMain {
@@ -179,22 +183,33 @@ public class Main {
179
183
Sabi.uses("foo", newFooDataSrc());
180
184
}
181
185
186
+
staticMyLogic myLogic =newMyLogic();
187
+
182
188
publicstaticvoidmain(String[] args) {
183
-
// Set up the sabi framework.
184
-
try (var ac =Sabi.setup()) {
189
+
try {
190
+
run();
191
+
} catch (Exception e) {
192
+
System.err.println(e.toString());
193
+
System.exit(1);
194
+
}
195
+
}
185
196
197
+
staticvoidrun() throwsErr {
198
+
// Set up the sabi framework
199
+
try (var ac =Sabi.setup()) {
200
+
186
201
// Creates a new instance of DataHub.
187
-
varhub=newMyDataHub();
202
+
try (vardata=newMyDataHub()) {
188
203
189
-
// Register session-local DataSrc to DataHub.
190
-
hub.uses("bar", newBarDataSrc());
204
+
// Register session-local DataSrc with DataHub
205
+
data.uses("bar", newBarDataSrc());
191
206
192
-
// Execute application logic within a transaction.
193
-
// MyLogic performs data operations via DataHub.
194
-
hub.txn(newMyLogic());
207
+
// Execute application logic without a transaction control.
208
+
data.run(myLogic);
195
209
196
-
} catch (Exception e) {
197
-
System.exit(1);
210
+
// If you need to execute logic within a transaction, use the `txn` method instead of `run`
0 commit comments