Ninject,根据循环变量选择一个类

时间:2015-07-01 18:25:10

标签: c# ninject

Visual Studio 2010,C#,Ninject版本3.0.0.0。

有一个提供字符串变量X的循环。在类中有对Ninject的引用。在循环内部,变量在kernel.Get方法中用于选择要使用的类。

我的印象是Ninject能够确定正确的类而不必使用Ninject内核。获取方法。这只是另一种依赖,这是Ninject应该帮助避免的情况。

有没有办法配置Ninject来确定基于变量X使用的正确类而不使用kernel.Get?我想在没有在课堂上使用Ninject引用的情况下完成此任务。

我有搜索stackoverflow和网络一般来回答这个问题。我本可以错过一些东西或者误解了一些东西,但我已经花了很多时间搜索。

感谢。

好的,有几个类使用这种技术。这是一个:

代码:

foreach (string nodePattern in patternList.GetPattern())
{
    numberOfPatterns++;
    tempReturn = true;
    nodeStack.LoadbatchStructs(nodeStructure);
    patternStack.LoadPattern(nodePattern);
    PatternProperty patternProp = patternStack.PopPattern();
    while (!(patternProp.Identifier == "EndOfStack"))
    {
        matchMode = int.TryParse(patternProp.PatternType, out outIntTestResult)
            ? "#"
            : patternProp.PatternType;
        int.TryParse(patternProp.PatternType, out loopCount);
        classNames.TryGetValue(matchMode, out className);
        IComparePatternAndData patternMatcher = kernel.Get<IComparePatternAndData>(className);
        tempReturn &= patternMatcher.MatchesPattern(nodeStack, patternProp.Identifier, loopCount);
        if (tempReturn == false)
        {
            falseMatches++;
            break;
        }
        else
        {
            patternProp = patternStack.PopPattern();
            //If there are no more pattern units to match, but there are more node identifiers (e.g. form identifiers or template IDs),
            //then the pattern didn't match.  So set the tempReturn to false.
            if (patternProp.Identifier == "EndOfStack" &&
                !(nodeStack.PeekStructData() == "ENDNODE"))
            {
                tempReturn = false;
            }
        }
    } //Next unit of pattern string
    if (tempReturn == true) numberOfMatches++;
} // Next pattern string

if (numberOfMatches > 0)
{
    tempReturn = true;
}
else
{
    tempReturn = false;
}

return tempReturn;

Ninject绑定。

Bind<IComparePatternAndData>().To<DataMatchesPatternCount>().Named("DataMatchesPatternCount");
Bind<IComparePatternAndData>().To<DataMatchesPatternErotem>().Named("DataMatchesPatternErotem");
Bind<IComparePatternAndData>().To<DataMatchesPatternStar>().Named("DataMatchesPatternStar");
Bind<IComparePatternAndData>().To<DataMatchesPatternPlus>().Named("DataMatchesPatternPlus");

类构造函数:

public class ValidateNodeStructure : IValidateNodeStructure
{
    private readonly INodeStructure nodeStruct;
    private readonly BatchStructStack nodeStack;
    private  INodePattern patternList;
    private readonly IPatternStack patternStack;
    private readonly IGetClassNameFromPatternType getClassNames;

    public ValidateNodeStructure(INodeStructure nodeStructure, BatchStructStack nodeStack,  IPatternStack patternStack, IGetClassNameFromPatternType getClassNames)
    {
        this.nodeStruct = nodeStructure;
        this.nodeStack = nodeStack;
        this.patternStack = patternStack;
        this.getClassNames = getClassNames;
    }

1 个答案:

答案 0 :(得分:0)

你正在使用像字典一样的ninject,但你的代码中已经有了一本字典:classNames

不是将Named添加到IComparePatternAndData的绑定中,为什么不将“name”数据添加到IComparePatternAndData接口(作为属性)并注入所有IComparePatternAndData 1}}进入IGetClassNameFromPatternType - (当然你应该更改名称然后b / c它提供不同的功能。我已将其重命名为IPatternDictionary)。

public interface IComparePatternAndData
{
    int PatternId { get; }

    // of course the actual compare method(s) go here as well...
}

public interface IPatternDictionary
{
    bool TryGetValue(int key, out IComparePatternAndData value);
}

public class PatternDictionary : Dictionary<int, IComparePatternAndData>, IPatternDictionary
{
    public PatternDictionary(IComparePatternAndData[] patterns)
    {
        foreach (IComparePatternAndData pattern in patterns)
        {
            this[pattern.PatternId] = pattern;
        }
    }
}

绑定:

Bind<IPatternDictionary>().To<PatternDictionary>();
Bind<IComparePatternAndData>().To<DataMatchesPatternCount>();
Bind<IComparePatternAndData>().To<DataMatchesPatternErotem>();
Bind<IComparePatternAndData>().To<DataMatchesPatternStar>();
Bind<IComparePatternAndData>().To<DataMatchesPatternPlus>();

用法:

IComparePatternAndData patternMatcher;
patternDictionary.TryGetValue(matchMode, out patternMatcher);

而不是:

   classNames.TryGetValue(matchMode, out className);
   IComparePatternAndData patternMatcher = kernel.Get<IComparePatternAndData>(className);