我一直在研究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 }
要么
Login(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) }
我建议让Account对象直接使用AccountRepository,而不是AccountController从AccountRepository获取信息,然后将其传递给Account对象,例如
NerdDinnner风格:
1登录请求进来
2 AccountController使用AccountRepository根据请求获取登录详细信息
3 AccountController使用Account对象并传递步骤1中的信息
4帐户对象处理请求并通知AccountController
我在暗示:
1登录请求进来
2 AccountController使用Account对象根据请求处理登录
3 Account对象使用AccountRepository获取登录详细信息
4帐户对象处理请求并通知AccountController
后一种风格的原因是,在从AccountRepository返回登录详细信息之后,将遵循业务规则,例如,是帐户锁定,帐户验证等.如果使用第一个样式,获取详细信息然后使用它们的逻辑分为两个步骤.后一种风格将所有逻辑保持在一起,同时仍然使用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 }
我希望这是有道理的.
解决方法
mvc中的“模型”是表示模型,而不是域模型.您可以将MVC中的模型视为Controller使用的数据传输对象,以提供视图引擎. 控制器是与服务层连接的唯一actor.