依赖注入和 ASP.Net 成员资格提供程序 [英] Dependency injection and ASP.Net Membership Providers

查看:30
本文介绍了依赖注入和 ASP.Net 成员资格提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 ASP.Net MVC 网站创建自定义成员资格提供程序.提供程序被创建为一个单独的类,作为更大库的一部分.后端数据存储需要灵活,因为它可以是 Xml 文件或 SQL 数据库.我最初的想法是为数据存储创建一个接口,并使用依赖注入将其注入到提供程序中.

I am in the process of creating a custom membership provider for an ASP.Net MVC website. The provider is being created as a separate class as part of a bigger library. There is a need for the back-end data store to be flexible as it could be an Xml File or SQL database. My initial thought was to create an interface for the data store and inject this into provider using dependency injection.

最终的结果是开发人员可以继承数据存储接口并提供更新数据所需的方法,然后由自定义成员资格提供程序使用.

The end result is required is that a developer can inherit the data store interface and provide the required methods to update the data, which will then be used by the custom membership providers.

但是由于我自己缺乏技能,我无法弄清楚在将类添加到网站时如何将其注入会员提供程序?需要做什么来将数据存储链接到提供者?在网站中启用此功能的最简单方法是什么?

However through my own lack of skill I can't figure out how to inject the class into the membership provider when adding it to the website? What needs to be done to link the data store to the provider? What would be the simplest way to enable this in the website?

推荐答案

如果您通过 Web.config 文件中的 <membership> 元素配置自定义成员资格提供程序,那么我可以看到您将遇到的依赖问题注射.

If you are configuring the custom membership providers via the <membership> element in the Web.config file, then I can see the issues you will have with dependency injection.

提供者由框架构造和管理,您没有机会拦截该构造来为 IDataStore 接口提供额外的依赖注入.

The providers are constructed and managed by the framework, and there is no opportunity for you to intercept that construction to provide additional dependency injection for the IDataStore interface.

如果我的假设是正确的,那么您可以做的是覆盖自定义提供程序中的 Initialize() 方法,并在那里进行依赖注入.您可以在提供程序配置中有一个自定义名称/值设置,它指向一个实现 IDataStore 的类型,它作为字典的一部分传递给 Initialize() 方法.

If my assumption is correct, then what you can do is override the Initialize() method in your custom provider, and do the dependency injection there. You can have a custom name/value setting in the provider configuration which points to a type that implements IDataStore, which is passed as part of a dictionary to the Initialize() method.

然后,您激活数据存储类型的实例并将其设置在适当的属性上:

Then, you activate an instance of the data store type and set it on the appropriate property:

public class MyMembershipProvider : MembershipProvider
{
    public IDataStore DataStore
    {
        get;
        set;
    }

    public override Initialize(string name, NameValueCollection config)
    {
        var dataStoreType = config["dataStoreProvider"];
        if (!String.IsNullOrEmpty(dataStoreType))
        {
            var type = Type.GetType(dataStoreType);
            DataStore = (IDataStore) Activator.CreateInstance(type);
        }
    }
}

Initialize() 将在框架构建您的提供者的实例后调用,因此这是执行任何其他设置工作(例如此类)的理想场所.

Initialize() will be called by the framework after it constructs an instance of your provider, so that is the perfect place to do any additional setup work such as this.

对于测试场景,您只需在提供者实例本身上设置数据存储属性,因为您将在测试中直接构建它.

For testing scenarios, you just set the data store property on the provider instance itself, as you will be constructing it directly in your tests.

这篇关于依赖注入和 ASP.Net 成员资格提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆