通过构造函数访问 AccountController 中的存储库

发布于 2024-10-20 02:31:59 字数 946 浏览 0 评论 0原文

我正在使用实现 ASP.NET 成员资格提供程序的 MVC AccountController。 我有一个包含所有数据库访问权限的存储库,其中我添加了一个返回国家/地区列表的国家/地区属性。我想在注册页面添加一个国家/地区下拉列表,因此我需要能够从我的存储库获取此数据并将其传递到视图。 我一直在其他控制器中使用构造函数注入,但我不知道如何将其应用于现有的 AccountController。

        // This constructor is used by the MVC framework to instantiate the controller using
    // the default forms authentication and membership providers.

    public AccountController()
        : this(null, null)
    {
    }

    // This constructor is not used by the MVC framework but is instead provided for ease
    // of unit testing this type. See the comments at the end of this file for more
    // information.
    public AccountController(IFormsAuthentication formsAuth, IMembershipService service)
    {
        FormsAuth = formsAuth ?? new FormsAuthenticationService();
        MembershipService = service ?? new AccountMembershipService();
    } 

我可以更改现有的 AccountController 构造函数来访问我的存储库吗?

I am using the MVC AccountController that implements the ASP.NET Membership Provder.
I have a repository with all my database access in which I have added a Countries property that returns a list of countries. I want to add a country dropdown to the Register page so I need to be able to get this data from my repository and pass it to the View.
I have been using contructor injection in my other controllers but I dont know how to apply this to the existing AccountController.

        // This constructor is used by the MVC framework to instantiate the controller using
    // the default forms authentication and membership providers.

    public AccountController()
        : this(null, null)
    {
    }

    // This constructor is not used by the MVC framework but is instead provided for ease
    // of unit testing this type. See the comments at the end of this file for more
    // information.
    public AccountController(IFormsAuthentication formsAuth, IMembershipService service)
    {
        FormsAuth = formsAuth ?? new FormsAuthenticationService();
        MembershipService = service ?? new AccountMembershipService();
    } 

Can I change the existing AccountController constructor to access my repository?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

︶葆Ⅱㄣ 2024-10-27 02:31:59

在 IoC 引擎中注册服务,然后删除默认构造函数。

Register the services in your IoC engine and then remove the default constructor.

长伴 2024-10-27 02:31:59

如果您已经使用 ninject 注册了存储库,则应该能够将第三个参数添加到控制器的构造函数中。我看到你之前关于 ninject 的评论,但我没有使用 NinjectModule。如果您使用的是 MVC 3,建议您查看 nuget (http://nuget.codeplex.com) 并下载 Ninject.MVC3 包将 AppStartNinjectMvc3 类添加到您的项目中,您可以在其中使用 kernel.Bind 方法注册服务:

kernel.Bind<IThingRepository>().To<SqlThingRepository>();

希望这会有所帮助。

If you have already registered your repository with ninject, you should be able to just add a third parameter to the constructor of the controller. I saw your earlier comment about ninject, but I'm not using NinjectModule. If you're using MVC 3, would suggest that you take a look at nuget (http://nuget.codeplex.com) and download the Ninject.MVC3 packge which adds a AppStartNinjectMvc3 class to your project where you can register services with the kernel.Bind methods:

kernel.Bind<IThingRepository>().To<SqlThingRepository>();

Hope this helps.

南城旧梦 2024-10-27 02:31:59

如果您使用 MVC2,您应该看看 http://mvcstarter.codeplex.com/ 它也使用 Ninject 。就像@Johan所说,您只需将参数并将其绑定到global.asax.cs中即可。

希望有帮助!

If your using MVC2 you should take a look at http://mvcstarter.codeplex.com/ it's also using Ninject. Like @Johan said you simply have to put the parameter and bind it in the global.asax.cs.

Hope it helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文