atw*_*ok8 5 asp.net-mvc controller model repository-pattern
我一直在研究NerdDinner教程,其中大部分都是有道理的.我不确定的是为什么Repository直接在Controller中使用而不是Model对象.例如,如果我们想要实现我们自己的会员系统并且它有一个带有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
}
Run Code Online (Sandbox Code Playgroud)
要么
Login(username,password){
repo = AccountRepository
account = repo.getLogin(username,password)
account.login() //business logic is handled in Account (Model)
}
Run Code Online (Sandbox Code Playgroud)
要么
Login(username,password){
//no reference to repository
account = Account
//Account (Model) uses repository and handles business logic
account.login(username,password)
}
Run Code Online (Sandbox Code Playgroud)
我建议让Account对象直接使用AccountRepository,而不是AccountController从AccountRepository获取信息,然后将其传递给Account对象,例如
NerdDinnner风格:
1登录请求
2 AccountController使用AccountRepository根据请求获取登录详细信息
3 AccountController使用Account对象并传入步骤1中的信息
4帐户对象处理请求并通知AccountController
我在暗示:
1登录请求
2 AccountController使用Account对象根据请求处理登录
3帐户对象使用AccountRepository获取登录详细信息
4帐户对象处理请求并通知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
}
Run Code Online (Sandbox Code Playgroud)
我希望这是有道理的.
| 归档时间: |
|
| 查看次数: |
3184 次 |
| 最近记录: |