Jam*_*xon 2 architecture asp.net-mvc-2
我有一个我正在研究的ASP.NET MVC 2项目,我想知道我应该在哪里放置一些代码.
我目前有一个UsersModel,它由一堆针对我的数据上下文操作的静态方法组成.
这些方法包括诸如:UserExistsInDatabase,UserIsRegisteredForActivity,GetUserIdFromFacebookId等等等等.
这些方法应该在UsersModel类中,还是更适合模型上下文之外的用户帮助程序类?
欢呼任何指针.
不要使用静态方法.在存储库中抽象它们:
public interface IUsersRepository
{
bool UserExistsInDatabase(User user);
bool UserIsRegisteredForActivity(User user);
...
}
Run Code Online (Sandbox Code Playgroud)
然后针对某些数据存储实现:
public class UsersRepository : IUsersRepository
{
...
}
Run Code Online (Sandbox Code Playgroud)
最后给你的控制器一个这个存储库的实例,以便它可以与用户一起工作:
public class HomeController : Controller
{
private readonly IUsersRepository _repository;
public HomeController(IUsersRepository repository)
{
// the repository is injected into the controller by the DI framework
_repository = repository;
}
// ... some action methods that will use the repository
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
48 次 |
| 最近记录: |