简体   繁体   English

ASP.NET MVC帮助器的常用功能

[英]ASP.NET MVC Helpers for common functionality

I appologize if this is a possible duplication, I've looked everywhere and couldn't find an answer. 如果这是可能的重复,我深表歉意。我到处都是,找不到答案。

My question is more of a "best practice/convention"-sort of question. 我的问题更多是“最佳实践/惯例”之类的问题。

So here is my situation, I'm having an AccountController -> something like this: 所以这是我的情况,我有一个AccountController->像这样:

    public AccountController(IAuthenticationHelper authHelper, 
                             IAccountService accountService)
    {
        _authHelper     = authHelper;
        _accountService = accountService;
    }

In my _Layout view, I have a placeholder for the currently logged in account. 在_Layout视图中,我有一个用于当前登录帐户的占位符。 In order to get the currently logged in account, I'm retrieving the current user identity from the HttpContext (which I have in a wrapper class so I can unit test it) -> then I'm getting the account details from the DB. 为了获取当前登录的帐户,我从HttpContext(在包装类中拥有该用户,以便可以对其进行单元测试)中检索当前用户身份->然后,我从数据库中获取了帐户详细信息。

Now here is my question, I need this data In the _Layout, I could possibly do a partial view expecting an account model -> place it in the _Layout ... And here is where I get stuck, I don't like the idea of so many trips to the database, and I don't like the fact that I have to think about this small detail from within all Actions ? 现在这是我的问题,我需要此数据在_Layout中,我可能会做一个局部视图,以期建立一个帐户模型->将其放置在_Layout中...这就是我遇到的问题,我不喜欢这个想法如此多的数据库访问,我不喜欢这样一个事实,我必须从所有Actions中考虑这个小细节? Am I missing something here, am I thinking of this wrong ? 我在这里错过了什么吗,我在想这个错误吗? Did I got the concept wrong ? 我把这个概念弄错了吗? What's the right way to do this ? 什么是正确的方法? (In a testable manner, preferably). (最好以可测试的方式)。

Help is much appreciated ! 帮助非常感谢!

EDIT: I'll be happy to provide with more code, if required. 编辑:如果需要,我很乐意提供更多代码。

You could use Child actions to achieve that. 您可以使用“ Child actions来实现此目的。 For example you will have a specific controller dedicated to retrieving the currently logged user details and pass them to a partial view: 例如,您将有一个特定的控制器专用于检索当前记录的用户详细信息并将其传递给部分视图:

public class UserInfoController: Controller
{
    [ChildActionOnly]
    public ActionResult Index()
    {
        var model = new UserDetailsViewModel();
        if (User.Identity.IsAuthenticated)
        {
            string username = User.Identity.Name;
            // fetch the user info from the database and populate your view model
            // consider caching this information to avoid multiple round-trips 
            // to the database
        }

        return PartialView(model);
    }
}

Then of course you will have a corresponding partial view to display the user information. 当然,您将拥有相应的局部视图以显示用户信息。

And from your _Layout you could render this child action: 从_Layout可以呈现此子动作:

@Html.Action("Index", "UserInfo")

And of course to avoid multiple roundtrips to the database you could simple cache this information. 当然,为了避免多次往返数据库,您可以简单地缓存此信息。

Now your main actions and models don't have to worry about this common functionality. 现在,您的主要操作和模型不必担心此通用功能。 It is handled by the child action. 它由子动作处理。 Obviously you have the possibility to use Dependency Injection in the UserInfoController to make it perfectly unit testable. 显然,您可以在UserInfoController使用依赖注入,使其完全可单元测试。

Typically, after login, I store the relevant account details in the Session . 通常,登录后,我会将相关的帐户详细信息存储在Session Since the Session is available in all views, you can reference it from there. 由于Session在所有视图中都可用,因此您可以从那里引用它。 If you'd prefer, you can also derive from a common view model and have your controllers derive from a custom controller base. 如果您愿意,也可以从通用视图模型派生,并让控制器从自定义控制器库派生。 You can then use OnActionExecuted to populate the user-related portions of your common view model with the account details, either from the Session or directly from the database. 然后,您可以使用OnActionExecuted通过Session或直接从数据库中的帐户详细信息填充通用视图模型中与用户相关的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM