Skip to content

Commit 090656e

Browse files
authored
Add guide for BaseUseCase.. (#18)
1 parent 37cf489 commit 090656e

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

README.ko.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ from typing import Optional
177177

178178
class MemoRepository(AsyncRepository[MemoEntity, int]):
179179
def __init__(self, session: Optional[AsyncSession]):
180-
self.session = session
180+
self._session = session
181181

182182
@async_transactional(read_only=True)
183183
async def find_by_title(title: str) -> MemoEntity:
@@ -202,6 +202,7 @@ SQLAlchemy에서 기본적으로 제공하는 작업 단위 패턴 외에 직접
202202
from sqlalchemy.engine import Engine
203203
from sqlalchemy.ext.asyncio import AsyncEngine
204204

205+
from pymfdata.common.usecase import BaseUseCase
205206
from pymfdata.rdb.usecase import AsyncSQLAlchemyUnitOfWork, SyncSQLAlchemyUnitOfWork
206207
from pymfdata.rdb.transaction import async_transactional
207208

@@ -216,9 +217,9 @@ class AsyncMemoUseCaseUnitOfWork(AsyncSQLAlchemyUnitOfWork):
216217
self.memo_repository: MemoRepository = MemoRepository(self.session)
217218

218219

219-
class MemoUseCase:
220+
class MemoUseCase(BaseUseCase):
220221
def __init__(self, uow: AsyncMemoUseCaseUnitOfWork) -> None:
221-
self.uow = uow
222+
self._uow = uow
222223

223224
@async_transactional(read_only=True)
224225
async def find_by_id(self, item_id: int):
@@ -227,4 +228,6 @@ class MemoUseCase:
227228

228229
작업 단위 패턴 또한 비동기, 동기에 따라 클래스가 별도로 구현되어 있습니다. 생성한 커넥션에 맞춰 사용하시면 됩니다.
229230

230-
이렇게 만들어진 작업 단위 클래스는 애플리케이션의 비즈니스 로직을 정의할 ***UseCase*** 클래스에 담아 사용할 수 있습니다. 사실상 비즈니스 로직에서 트랜잭션이 필요로 하는 경우의 코드이기 때문에 작업 단위 패턴에 있는 메서드에 ```transactional``` 데코레이터를 사용하여 트랜잭션을 처리할 수 있습니다.
231+
이렇게 만들어진 작업 단위 클래스는 애플리케이션의 비즈니스 로직을 정의할 ***UseCase*** 클래스에 담아 사용할 수 있습니다. 사실상 비즈니스 로직에서 트랜잭션이 필요로 하는 경우의 코드이기 때문에 작업 단위 패턴에 있는 메서드에 ```transactional``` 데코레이터를 사용하여 트랜잭션을 처리할 수 있습니다.
232+
233+
(```transactional``` 데코레이터를 사용하는 경우, pymfdata에서 제공하는 ```BaseUseCase``` 클래스를 상속하여 사용해주십시오)

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,13 @@ Since the repository of ```pymfdata``` uses the Python [Protocol](https://www.py
175175

176176
```python
177177
from pymfdata.rdb.repository import AsyncRepository, AsyncSession
178+
from pymfdata.rdb.transaction import async_transactional
178179
from typing import Optional
179180

180181

181182
class MemoRepository(AsyncRepository[MemoEntity, int]):
182183
def __init__(self, session: Optional[AsyncSession]):
183-
self.session = session
184+
self._session = session
184185

185186
@async_transactional(read_only=True)
186187
async def find_by_title(title: str) -> MemoEntity:
@@ -203,6 +204,7 @@ SQLAlchemy uses the unit of work pattern by default, but if you want to define y
203204
from sqlalchemy.engine import Engine
204205
from sqlalchemy.ext.asyncio import AsyncEngine
205206

207+
from pymfdata.common.usecase import BaseUseCase
206208
from pymfdata.rdb.usecase import AsyncSQLAlchemyUnitOfWork, SyncSQLAlchemyUnitOfWork
207209
from pymfdata.rdb.transaction import async_transactional
208210

@@ -217,9 +219,9 @@ class AsyncMemoUseCaseUnitOfWork(AsyncSQLAlchemyUnitOfWork):
217219
self.memo_repository: MemoRepository = MemoRepository(self.session)
218220

219221

220-
class MemoUseCase:
222+
class MemoUseCase(BaseUseCase):
221223
def __init__(self, uow: AsyncMemoUseCaseUnitOfWork) -> None:
222-
self.uow = uow
224+
self._uow = uow
223225

224226
@async_transactional(read_only=True)
225227
async def find_by_id(self, item_id: int):
@@ -231,3 +233,5 @@ The unit of work pattern is also divided into asynchronous and synchronous class
231233
The created unit of work class can be used in the class containing business logic, and we define them as **UseCase**. This class contains the business logic of the application.
232234

233235
When using the unit of work pattern, use the transactional decorator on business logic methods to handle transaction processing.
236+
237+
(If you are using ```transactional``` decorators, please use the ```BaseUseCase``` class provided by pymfdata.)

0 commit comments

Comments
 (0)