Ninject工厂模式和绑定

时间:2014-11-24 04:12:30

标签: c# dependency-injection ninject factory

我正在尝试实现Ninject.Extensions.Factory模式,我的程序告诉我我的绑定是不对的,但我无法弄清楚原因。我一直得到一个"错误激活IHashable。没有匹配的绑定可用,并且该类型不可自我绑定"异常抛出。我的代码的相关区域如下:

public interface IHashable
{
    FileInfo File { get; }

    string ComputeHash();
}

public interface IHashableFactory
{
    IHashable GetNew(FileInfo file);
}

public class MD5ChecksumProvider : IHashable
{
    private FileInfo _file;

    public FileInfo File
    {
        get { return _file; }
    }

    public MD5ChecksumProvider(FileInfo file)
    {
        if (file == null)
            throw new ArgumentNullException("file");

        _file = file;
    }

    public string ComputeHash()
    {
        // implementation
    }
}

public class AppFileProvider : IAppFileProvider
{
    private IHashableFactory _hashFactory;

    public IHashableFactory HashProvider
    {
        get { return _hashFactory; }
    }

    public AppFileProvider(IHashableFactory hashProviderFactory)
    {
        _hashFactory = hashProviderFactory;
    }

    public string GetChecksum(FileInfo fileInfo)
    {
        var hasher = _hashFactory.GetNew(fileInfo);
        return hasher.ComputeHash();
    }
}

public class BindingProviders : NinjectModule
{
    public override void Load()
    {
        Bind<IHashable>()
            .To<MD5ChecksumProvider>();
    }
}

public class BindingFactories : NinjectModule
{
    public override void Load()
    {
        Bind<IHashableFactory>()
            .ToFactory();
    }
}

// my DI container
public sealed class Container : IDisposable
{
    private bool _isDisposed;
    private IKernel _kernel;
    private BindingFactories _bindingFactories;
    private BindingObjects _bindingObjects;
    private BindingProviders _bindingProviders;

    public Container()
    {
        _isDisposed = false;

        _bindingFactories = new BindingFactories();
        _bindingObjects = new BindingObjects();
        _bindingProviders = new BindingProviders();

        _kernel = new StandardKernel(_bindingObjects, _bindingProviders, _bindingFactories);
    }

    public T Get<T>()
    {
        return _kernel.Get<T>();
    }

    public void Dispose()
    {
        // nothing worth seeing
    }

    private void Dispose(bool disposing)
    {
        // nothing worth seeing
    }
}

// the program (composition root)
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        using (var container = new Container())
        {
            var fileProvider = container.Get<IAppFileProvider>();

            foreach (var file in files)
            {
                string hash = fileProvider.GetChecksum(storePath, file); // this line throws "Error activating IHashable. No matching bindings are available, and the type is not self-bindable.""
            }
        }
    }
}

我觉得我的绑定设置正确但我必须遗漏一些明显的东西。我有什么想法从上面的代码中获得异常?

1 个答案:

答案 0 :(得分:3)

这是由Ninject.Extensions.Factory的一个功能引起的。 它将以Get开头的方法与不以IHashableFactory.GetNew开头的方法区别对待。 如果您将Create重命名为MakeGet,一切正常。

resolutionRoot.Get<IFoo>("MySpecialFoo");”功能描述为here

  

扩展的默认instace提供程序具有以下惯例:只要方法以“Get”开头,它就会尝试使用命名绑定返回实例。例如。 IFoo GetMySpecialFoo()等于

     

{{1}}


由于我认为这对用户来说并不明显,而且这方面的例外根本没有帮助,我已提交问题报告here

相关问题