Junghyun99 / Test

0 stars 0 forks source link

의존성 주입 #4

Closed Junghyun99 closed 3 weeks ago

Junghyun99 commented 1 month ago

클래스간 인스턴스를 의존성 주입 방식으로 만들어 결합도를 낮추자

상속관계가 있으면 추상화 클래스를 주입해서 자유롭게 변경가능

services/transaction_service.py

from models.transaction import Transaction from repository.base_repository import BaseRepository

class TransactionService: def init(self, repository: BaseRepository): self.repo = repository # 의존성 주입

def create_transaction(self, stock_name, transaction_type, quantity, price, date):
    transaction = Transaction(stock_name, transaction_type, quantity, price, date)
    self.repo.add_transaction(transaction)

def get_transactions(self, stock_name):
    return self.repo.get_transactions_by_stock(stock_name)

def close(self):
    self.repo.close()