1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

DIを利用してAWS Lambdaのコーディングする

Last updated at Posted at 2023-05-08

コードの可読性と保守性を向上させるために、DI(Dependency Injection)を利用することができます。
この記事では、Lambda関数の中でDIパターンを適用して、モジュールやサービスの依存関係を管理する方法を解説します。

目次

  1. Dependency Injectionとは
  2. AWS LambdaとDIの関連性
  3. 例:簡単なLambda関数でDIを利用する方法
  4. 実践:サードパーティのDIライブラリを使用する
  5. まとめ

Dependency Injectionとは

Dependency Injectionは、オブジェクト指向プログラミングにおいて、コンポーネント間の依存関係を外部から注入するデザインパターンです。DIを利用することで、以下の利点があります。

  • コードの可読性向上
  • テストの容易性向上
  • モジュール間の疎結合

AWS LambdaとDIの関連性

AWS Lambdaは、サーバーレスアーキテクチャを実現するためのFaaS(Function as a Service)サービスです。Lambda関数は短期間実行されるため、関数内の状態管理やリソースの管理が重要です。DIを利用することで、コードの品質を向上させ、保守性や拡張性を確保できます。

例:簡単なLambda関数でDIを利用する方法

今回はpython3.10で例を提示します。

class UserService:
    def __init__(self, repository):
        self.repository = repository

    def find_user(self, user_id):
        return self.repository.get_user(user_id)

class UserRepository:
    def get_user(self, user_id):
        # データソースからユーザー情報を取得する処理
        # 取得元はDBや外部API等
        pass

def handler(event, context):
    user_repository = UserRepository()
    user_service = UserService(user_repository)

    user_id = event["user_id"]
    user = user_service.find_user(user_id)
    return user

この例では、UserServiceとUserRepositoryの依存関係をコンストラクタで注入しています。これにより、依存関係の管理が容易になり、テストも行いやすくなります。
今回は実装していませんが、通常サービスクラスにロジックを記述し、リポジトリクラスにデータの取得や保存のロジックを記述します。

実践:サードパーティのDIライブラリを使用する

さきほどはDIがどのようなデザインなのかを説明するために、インスタンスを生成するコードを記述しました。
しかしこれでは、クラスの依存関係を意識しながらコーディングする必要があります。
それらを解決するサードパーティライブラリがいくつか存在します。ここでは、injectorライブラリを使用してDIを実装します。

まず、injectorライブラリをインストールします。

pip install injector

次に、injectorを使用してDIを実装する方法を見ていきましょう。

from injector import Injector, inject, Module, singleton , provider

class UserService:
    # このデコレータによって依存関係がinjectorによって解決されます
    @inject
    def __init__(self, repository: UserRepository):
        self.repository = repository

    def find_user(self, user_id):
        return self.repository.get_user(user_id)

class UserRepository:
    def get_user(self, user_id):
        # データソースからユーザー情報を取得する処理
        pass

def handler(event, context):
    injector = Injector()
    # injectorがクラスの依存関係を解決したうえで、インスタンスを提供する
    user_service = injector.get(UserService)

    user_id = event["user_id"]
    user = user_service.find_user(user_id)
    return user

この例では、injectorライブラリを使用して、UserServiceとUserRepositoryの依存関係を解決しています。handler関数内でインジェクタを作成し、UserServiceインスタンスを取得しています。

まとめ

この記事では、AWS LambdaでDIを利用してコーディングする方法を紹介しました。DIを適用することで、Lambda関数のコードの可読性や保守性が向上し、より品質の高いコードを実現できます。

今後、以下のようなコンテンツを予定しています。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?