私は NerdDinner チュートリアルに取り組んできましたが、そのほとんどは理にかなっています。私がよくわからないのは、モデルオブジェクトではなく、リポジトリがコントローラーで直接使用される理由です。たとえば、独自のメンバーシップ システムを実装する必要があり、Login メソッドを含む AccountController がある場合、それを接続するための最適なソリューションは何でしょうか? 例えば
Login(username,password){
repo = AccountRepository
account = repo.getLogin(username,password)
//check account isn't locked
//check account has been verified etc etc
//throw error or proceed to secure area
}
また
Login(username,password){
repo = AccountRepository
account = repo.getLogin(username,password)
account.login() //business logic is handled in Account (Model)
}
また
Login(username,password){
//no reference to repository
account = Account
//Account (Model) uses repository and handles business logic
account.login(username,password)
}
AccountController が AccountRepository から情報を取得して Account オブジェクトに渡すのではなく、Account オブジェクトで AccountRepository を直接使用することをお勧めします。
NerdDinner スタイル:
1 ログイン要求が受信される
2 AccountController は AccountRepository を使用して要求に基づいてログインの詳細を取得する
3 AccountController は Account オブジェクトを使用し、ステップ 1 からの情報を渡す
4 Account オブジェクトは要求を処理し、AccountController に通知する
私が提案していること:
1 ログイン要求が受信される
2 AccountController は Account オブジェクトを使用して要求に基づいてログインを処理する
3 Account オブジェクトは AccountRepository を使用してログインの詳細を取得する
4 Account オブジェクトは要求を処理し、AccountController に通知する
後者のスタイルの理由は、AccountRepository からログインの詳細が返された後、従うべきビジネス ルールがあるためです。たとえば、アカウントがロックされているか、アカウントが検証されているかなどです。 2段以上。後者のスタイルでは、AccountRepository を使用しながらすべてのロジックをまとめます。
Account.Login(username,password){
repo = AccountRepository
account = repo.GetLogin(username,password)
//check account isn't locked
//check account has been verified etc etc
//throw error or proceed to secure area
}
それが理にかなっていることを願っています。