微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net-mvc – 控制器或模型中的存储库?

我一直在研究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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐