依赖注入。将值分配给IENUMERABLE

时间:2011-01-12 00:32:38

标签: c# asp.net-mvc-3

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 ApplicationName { get; protected set; }
    public IFeatureRegistry EnabledFeatures { get; protected set; }
}

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

        EnabledFeatures = null;
    }
}

我是这个领域的新手。我的问题是如何将值分配给EnabledFeatures

由于 捷虹

2 个答案:

答案 0 :(得分:2)

由于您选择使用私有setter,您唯一真正的选择是使用与您拥有的几乎相同的构造函数注入,除非您为SampleTenant类提供额外的重载,因此它看起来更像这样:

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

        EnabledFeatures = enabledFeatures;
    }
}

您可以在此处详细了解构造函数与setter注入:http://misko.hevery.com/2009/02/19/constructor-injection-vs-setter-injection/

答案 1 :(得分:0)

检查此.Net Dependency注入器。它可能有助于满足您的需求。

http://ninject.org/