GithubHelp home page GithubHelp logo

rails-domain's Introduction

About

Usecase/Interactor を利用した Rails-DDD Todo アプリケーション

Interactorオブジェクト

Interactorオブジェクトとは「デザインパターンのひとつで、ビジネスロジックをカプセル化するためのモデル層に属するクラス群」。

ひとつのInteractorオブジェクトはひとつの責務をもつ。

※「ひとつの責務」とは、たとえば「記事を投稿する」「決済を行う」という、それ以上分割できない責務。

下記は、「ユーザーを見つける」Interactor です。

module UserDomain
  module Usecases
    module Interactors
      class FindUserInteractor < ApplicationInteractor
        delegate :params, to: :context, private: true

        def call
          user = Models::User.find_by(email: params[:email])
          
          if user.nil?
            context.fail!(message: 'ユーザーが見つかりません。')
          else
            context[:user] = user
          end
        end

      end
    end
  end
end

Usecaseオブジェクト

Usecaseオブジェクトとは複数のInteractorを構成するクラスです。

「ユーザーを見つける」「パスワードを検証する」「セッションに保存する」の Interactor の使用を定義します。

module UserDomain
  module Usecases
    class SignIn < ApplicationUsecase
      include InteractorTransactional

      organize Interactors::FindUserInteractor,
                Interactors::PasswordVerificationInteractor,
                Interactors::UpdateSessionInteractor
    end
  end
end

参考サイト

rails-domain's People

Contributors

takeruun avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.