首页 文章

Ninject工厂是否符合开放/封闭原则?

提问于
浏览
1

我正在寻找创建符合开放/封闭标准的工厂的解决方案 . 我们可以通过反射实现它非常简单(实例化实现特定接口的所有类,存在于当前程序集中,使用基于静态属性的键将它们存储在某些字典中,并根据传递给CreateInstance方法的参数返回特定实例) .

我想知道使用Ninject是否可行 . 我买了一本书“掌握依赖注入的Ninject”,其中有一章“满足现实世界的要求”与Telcom Switch的例子 . 不幸的是,IStatusCollectorFactory和实现此接口的所有工厂都违反了Open / Close原则 - 如果要添加对新类的支持,则必须更改接口 .

有什么帮助?:)

/// Interface for classes that define factory able to create currency defaltion instances. 
public interface ICurrencyDeflationFactory
{
    ICurrencyDeflation CreateInstance(string currencyCode);
}


/// <summary>
/// Interface for classes that define deflation table in specific currency.
/// </summary>
public interface ICurrencyDeflation
{
    /// <summary>
    /// Current currency code as defined in ISO 4217
    /// </summary>
    string CurrencyCode { get; }

    /// <summary>
    /// Deflation table used during conversion.
    /// </summary>
    string[,] GetDeflationTable { get; }
}

1 回答

  • 1

    感谢您的反馈意见 . 我设法实现了我想要的(在我看来:) :)这是一个潜在的解决方案:内核注册:

    kernel.Bind<ICurrencyDeflationFactory>().To<CurrencyDeflationFactory>(); 
    kernel.Bind(x => x.FromAssemblyContaining<ICurrencyDeflation>()
      .SelectAllClasses()
      .InheritedFrom<ICurrencyDeflation>()
      .BindAllInterfaces());
    

    工厂界面:

    /// <summary>
    /// Interface for classes that define factory able to create currency defaltion instances.
    /// </summary>
    public interface ICurrencyDeflationFactory
    {
        List<ICurrencyDeflation> CurrencyList { get; }
        ICurrencyDeflation CreateInstance(string currencyCode);
    }
    

    通货紧缩界面:

    /// <summary>
    /// Interface for classes that define deflation table in specific currency.
    /// </summary>
    public interface ICurrencyDeflation
    {
        /// <summary>
        /// Current currency code as stands in ISO 4217
        /// </summary>
        string CurrencyCode { get; }
    
        /// <summary>
        /// Deflation table used during conversion.
        /// </summary>
        string[,] GetDeflationTable { get; }
    }
    

    工厂混凝土:

    public class CurrencyDeflationFactory : ICurrencyDeflationFactory
    { 
        List<ICurrencyDeflation> deflationCollection;
        public CurrencyDeflationFactory(List<ICurrencyDeflation> definedDeflations)
        {
            this.deflationCollection = definedDeflations;
        }
        public List<ICurrencyDeflation> CurrencyList
        {
            get
            {
                return deflationCollection;
            }
        }
    
        public ICurrencyDeflation CreateInstance(string currencyCode)
        {
            return deflationCollection.Find(x => x.CurrencyCode.Equals(currencyCode));
        }
    }
    

    为了获得美元通货紧缩:

    var currencyDeflation = kernel.Get<ICurrencyDeflationFactory>().CreateInstance("USD");
    

    因此,我不必:
    1)改变主程序向内核注册新类
    2)新的通货紧缩只是一个实现IDeflationCurrency的新类
    3)每个实现IDeflationCurrency的类都将在运行时可用,无需更改现有代码(关闭以进行修改)
    4)如果需要,我可以在不触及现有代码的情况下添加新的通货紧缩(打开扩展名)

相关问题