Внедрение зависимости и Поставщики Членства в ASP.NET

Несколько опций:

  1. можно присоединить к AppDomain.AssemblyResolve, чтобы помочь CLR разрешить блок.
  2. Вы могли изучить изоляцию дополнений в их собственное AppDomain (см. System.AddIn пространство имен и этот веб-сайт ).

34
задан BinaryMisfit 11 October 2009 в 19:26
поделиться

2 ответа

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

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.

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() 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.

33
ответ дан 27 November 2019 в 16:55
поделиться

The simplest way to do dependency injection that I've seen (and actually the only one I've used so far...) is to have a constructor of your dependent class take the interface as a parameter, and assign it to a private field. If you want, you can also add a "default" constructor, which chains to the first one with a default value.

Simplified, it would look something like this:

public class DependentClass
{
    private IDataStore _store;

    // Use this constructor when you want strict control of the implementation
    public DependentClass(IDataStore store)
    {
         this._store = store;
    }

    // Use this constructor when you don't want to create an IDataStore instance
    // manually every time you create a DependentClass instance
    public DependentClass() : this(new DefaultDataStore()) { }
}

The concept is called "Constructor chaining", and there's a lot of articles on the web on how to do it. I find this tutorial very explanatory of the DI pattern.

2
ответ дан 27 November 2019 в 16:55
поделиться
Другие вопросы по тегам:

Похожие вопросы: