开发者

Dependency Injection. Assign values to IENUMERABLE

public interface IFeature
{
   string FeatureName { get; set; }
}

public interface IFeatureRegistry
{
    IEnumerable<IFeature> Features { get; set; }

    bool IsEnabled(IEnumerable<string> featurePath);
}

public interface IApplicationTenant
{
    string ApplicationName { get; }
    IFeatureRegistry EnabledFeatures { get; }
}

public abstract class AbstractApplicationTenant : IApplicationTenant
{
    public string ApplicationNam开发者_如何学编程e { get; protected set; }
    public IFeatureRegistry EnabledFeatures { get; protected set; }
}

public class SampleTenant : AbstractApplicationTenant
{
    public SampleTenant()
    {
        ApplicationName = "Sample 1";

        EnabledFeatures = null;
    }
}

I am new to this field. My question is how to assign values to EnabledFeatures?

Thanks Jeco


Since you've chosen to use private setters, your only real option is to use constructor injection which is almost identical to what you have, except you'd provide an additional overload to the SampleTenant class so it would look more like this:

public class SampleTenant : AbstractApplicationTenant
{
    public SampleTenant(IFeatureRegistry enabledFeatures)
    {
        ApplicationName = "Sample 1";

        EnabledFeatures = enabledFeatures;
    }
}

You can read more about constructor vs setter injection here: http://misko.hevery.com/2009/02/19/constructor-injection-vs-setter-injection/


Check this .Net Dependency injector out. It may help with what you need.

http://ninject.org/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜